Repository navigation
Expand file tree
/
Copy patherrors.go
More file actions
104 lines (91 loc) · 2.53 KB
/
Copy patherrors.go
File metadata and controls
104 lines (91 loc) · 2.53 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package pkg
import (
"errors"
"fmt"
"net/http"
)
// ArtifactKind is the file an [InstallError] is about.
type ArtifactKind int
const (
ArtifactRecipe ArtifactKind = iota + 1
ArtifactPackage
ArtifactSignature
)
func (k ArtifactKind) String() string {
switch k {
case ArtifactRecipe:
return "recipe"
case ArtifactPackage:
return "package"
case ArtifactSignature:
return "signature"
}
return "artifact"
}
// InstallError reports a package that could not be fetched or installed.
// Error gives the full detail, URL included, and is meant for logs; the
// fields let a caller build a shorter message for the user. Err is the cause,
// to be matched with errors.Is: [ErrNotFound], [ErrForbidden],
// [ErrAuthorizationRequired], [ErrUnexpectedStatus], [ErrRecipeMismatch], the
// [Verifier]'s error, or whatever the transport or the recipe parser returned.
type InstallError struct {
Name string // requested package name, always set
Package *Package // nil until the version is resolved
Edition string // the resolved edition, empty for a local install
Artifact ArtifactKind // the file that failed
URL string // where it was fetched from, or the local path
StatusCode int // 0 when there was no HTTP response
Err error
}
func (e *InstallError) Error() string {
what := e.Name
if e.Package != nil {
what += " " + e.Package.Version
}
return fmt.Sprintf("install %s: %s %s: %v", what, e.Artifact, e.URL, e.Err)
}
func (e *InstallError) Unwrap() error {
return e.Err
}
func newInstallError(name string, pkg *Package, edition string, kind ArtifactKind, location string, err error) *InstallError {
e := &InstallError{
Name: name,
Package: pkg,
Edition: edition,
Artifact: kind,
URL: location,
Err: err,
}
var se *statusError
if errors.As(err, &se) {
e.StatusCode = se.code
}
return e
}
// statusError is a non-200 response, unwrapping to the sentinel its status
// maps to.
type statusError struct {
code int
status string
err error
}
func newStatusError(resp *http.Response) *statusError {
se := &statusError{code: resp.StatusCode, status: resp.Status}
switch resp.StatusCode {
case http.StatusNotFound:
se.err = ErrNotFound
case http.StatusUnauthorized:
se.err = ErrAuthorizationRequired
case http.StatusForbidden:
se.err = ErrForbidden
default:
se.err = ErrUnexpectedStatus
}
return se
}
func (e *statusError) Error() string {
return fmt.Sprintf("%v (%s)", e.err, e.status)
}
func (e *statusError) Unwrap() error {
return e.err
}