aboutsummaryrefslogtreecommitdiff
path: root/src/errors/example_unwrap_test.go
blob: 05c9cd466f1f2ab762a656867f9b37cec2b502bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package errors_test

import (
	"errors"
	"fmt"
	"os"
	"time"
)

// MyError2 is an error implementation that includes a time, a message, and an
// underlying error.
type MyError2 struct {
	When time.Time
	What string
	err  error
}

func (e MyError2) Error() string {
	return fmt.Sprintf("%v at %v: %v", e.What, e.When, e.err)
}

// Unwrap returns e's underlying error, or nil if there is none.
func (e MyError2) Unwrap() error {
	return e.err
}

func readConfig() error {
	if _, err := os.Open("non-existing"); err != nil {
		return MyError2{
			time.Date(1989, 3, 15, 22, 30, 0, 0, time.UTC),
			"reading config file",
			err,
		}
	}
	return nil
}

func Example_unwrap() {
	if err := readConfig(); err != nil {
		// Display the error.
		fmt.Println(err)
		// If we can retrieve the path, try to recover
		// by taking another action.
		var pe *os.PathError
		if errors.As(err, &pe) {
			restoreFile(pe.Path)
		}
	}
	// Output: reading config file at 1989-03-15 22:30:00 +0000 UTC: open non-existing: no such file or directory
}

func restoreFile(path string) {}