File: errors.go

package info (click to toggle)
restic 0.9.4%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 26,636 kB
  • sloc: sh: 1,460; makefile: 46; python: 35
file content (53 lines) | stat: -rw-r--r-- 1,235 bytes parent folder | download | duplicates (2)
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
package errors

import (
	"net/url"

	"github.com/pkg/errors"
)

// New creates a new error based on message. Wrapped so that this package does
// not appear in the stack trace.
var New = errors.New

// Errorf creates an error based on a format string and values. Wrapped so that
// this package does not appear in the stack trace.
var Errorf = errors.Errorf

// Wrap wraps an error retrieved from outside of restic. Wrapped so that this
// package does not appear in the stack trace.
var Wrap = errors.Wrap

// Wrapf returns an error annotating err with the format specifier. If err is
// nil, Wrapf returns nil.
var Wrapf = errors.Wrapf

// WithMessage annotates err with a new message. If err is nil, WithMessage
// returns nil.
var WithMessage = errors.WithMessage

// Cause returns the cause of an error. It will also unwrap certain errors,
// e.g. *url.Error returned by the net/http client.
func Cause(err error) error {
	type Causer interface {
		Cause() error
	}

	for {
		// unwrap *url.Error
		if urlErr, ok := err.(*url.Error); ok {
			err = urlErr.Err
			continue
		}

		// if err is a Causer, return the cause for this error.
		if c, ok := err.(Causer); ok {
			err = c.Cause()
			continue
		}

		break
	}

	return err
}