Debian Codegraph @

example_copyerror_test.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_test
17
18 import (
19 "context"
20 "errors"
21 "fmt"
22
23 "oras.land/oras-go/v2"
24 "oras.land/oras-go/v2/content/memory"
25 )
26
27 // ExampleCopyError demonstrates how to check CopyError returned from
28 // copy operations.
29 func ExampleCopyError() {
30 src := memory.New()
31 dst := memory.New()
32 ctx := context.Background()
33
34 // Try to copy a non-existent reference, which is expected to fail
35 nonExistentRef := "non-existent-reference"
36 _, err := oras.Copy(ctx, src, nonExistentRef, dst, "", oras.DefaultCopyOptions)
37 if err != nil {
38 // Check if the error is a CopyError and print its details
39 var copyErr *oras.CopyError
40 if errors.As(err, &copyErr) {
41 fmt.Println("copyErr.Origin:", copyErr.Origin)
42 fmt.Println("copyErr.Op:", copyErr.Op)
43 fmt.Println("copyErr.Err:", copyErr.Err)
44 fmt.Println("copyErr.Error():", copyErr.Error())
45 return
46 }
47
48 fmt.Println("err is not a CopyError:", err)
49 return
50 }
51
52 fmt.Println("Copy succeeded unexpectedly")
53
54 // Output:
55 // copyErr.Origin: source
56 // copyErr.Op: Resolve
57 // copyErr.Err: non-existent-reference: not found
58 // copyErr.Error(): failed to perform "Resolve" on source: non-existent-reference: not found
59 }