Debian Codegraph @

copyerror.go

raw
1 /*
2 Copyright The ORAS Authors.
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 */
15
16 package oras
17
18 import "fmt"
19
20 // CopyErrorOrigin defines the source of a copy error.
21 type CopyErrorOrigin int
22
23 const (
24 // CopyErrorOriginSource indicates the error occurred at the source side.
25 CopyErrorOriginSource CopyErrorOrigin = 1
26
27 // CopyErrorOriginDestination indicates the error occurred at the destination side.
28 CopyErrorOriginDestination CopyErrorOrigin = 2
29 )
30
31 // String returns the string representation of the CopyErrorOrigin.
32 func (o CopyErrorOrigin) String() string {
33 switch o {
34 case CopyErrorOriginSource:
35 return "source"
36 case CopyErrorOriginDestination:
37 return "destination"
38 default:
39 return "unknown"
40 }
41 }
42
43 // CopyError represents an error encountered during a copy operation.
44 type CopyError struct {
45 // Op is the operation that caused the error.
46 Op string
47 // Origin indicates the source of the error.
48 Origin CopyErrorOrigin
49 // Err is the underlying error.
50 Err error
51 }
52
53 // newCopyError creates a new CopyError.
54 func newCopyError(op string, origin CopyErrorOrigin, err error) error {
55 if err == nil {
56 return nil
57 }
58 return &CopyError{
59 Op: op,
60 Origin: origin,
61 Err: err,
62 }
63 }
64
65 // Error implements the error interface for CopyError.
66 func (e *CopyError) Error() string {
67 switch e.Origin {
68 case CopyErrorOriginSource, CopyErrorOriginDestination:
69 return fmt.Sprintf("failed to perform %q on %s: %v", e.Op, e.Origin, e.Err)
70 default:
71 return fmt.Sprintf("failed to perform %q: %v", e.Op, e.Err)
72 }
73 }
74
75 // Unwrap implements the errors.Unwrap interface for CopyError.
76 func (e *CopyError) Unwrap() error {
77 return e.Err
78 }