-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
63 lines (54 loc) · 1.28 KB
/
errors.go
File metadata and controls
63 lines (54 loc) · 1.28 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
package acgo
import (
"errors"
"fmt"
)
// APIError represents an error response from the Docker-compatible API.
type APIError struct {
StatusCode int
Message string
}
func (e *APIError) Error() string {
return fmt.Sprintf("api error (status %d): %s", e.StatusCode, e.Message)
}
// NotFoundError indicates the requested resource was not found.
type NotFoundError struct {
Resource string
ID string
}
func (e *NotFoundError) Error() string {
return fmt.Sprintf("%s %q not found", e.Resource, e.ID)
}
// ConflictError indicates a resource state conflict.
type ConflictError struct {
Resource string
ID string
Message string
}
func (e *ConflictError) Error() string {
return fmt.Sprintf("%s %q conflict: %s", e.Resource, e.ID, e.Message)
}
// IsNotFound returns true if the error is a NotFoundError or a 404 APIError.
func IsNotFound(err error) bool {
var nf *NotFoundError
if errors.As(err, &nf) {
return true
}
var ae *APIError
if errors.As(err, &ae) {
return ae.StatusCode == 404
}
return false
}
// IsConflict returns true if the error is a ConflictError or a 409 APIError.
func IsConflict(err error) bool {
var cf *ConflictError
if errors.As(err, &cf) {
return true
}
var ae *APIError
if errors.As(err, &ae) {
return ae.StatusCode == 409
}
return false
}