-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
85 lines (71 loc) · 2.54 KB
/
Copy patherrors.go
File metadata and controls
85 lines (71 loc) · 2.54 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
package vsax
import (
"errors"
"fmt"
)
// Sentinel errors for common cases. Typed errors below wrap these so callers
// can match with errors.Is as well as type-assert for extra detail.
var (
ErrNotFound = errors.New("resource not found")
ErrUnauthorized = errors.New("unauthorized")
ErrForbidden = errors.New("forbidden")
ErrBadRequest = errors.New("bad request")
ErrServer = errors.New("server error")
)
// Error is the base API error carrying the HTTP status, server message, and
// kind (a short machine-readable type tag).
type Error struct {
Code int // HTTP status code
Message string // Meta.ErrorMessage from the response, or HTTP status text
Kind string // short tag: auth, forbidden, not_found, validation, server, api
}
func (e Error) Error() string {
return fmt.Sprintf("vsax: %s (code: %d)", e.Message, e.Code)
}
// APIError represents an unclassified API error.
type APIError struct{ Base Error }
func (e *APIError) Error() string { return e.Base.Error() }
func (e *APIError) Unwrap() error { return ErrServer }
// AuthError represents a 401 response.
type AuthError struct{ Base Error }
func (e *AuthError) Error() string {
if e.Base.Message != "" {
return "vsax: authentication failed - " + e.Base.Message
}
return "vsax: authentication failed - invalid or expired token"
}
func (e *AuthError) Unwrap() error { return ErrUnauthorized }
// ForbiddenError represents a 403 response (authenticated but not permitted).
type ForbiddenError struct{ Base Error }
func (e *ForbiddenError) Error() string {
if e.Base.Message != "" {
return "vsax: forbidden - " + e.Base.Message
}
return "vsax: forbidden - insufficient permissions"
}
func (e *ForbiddenError) Unwrap() error { return ErrForbidden }
// NotFoundError represents a 404 response.
type NotFoundError struct {
Base Error
ResourceType string
ResourceID string
}
func (e *NotFoundError) Error() string {
if e.ResourceType != "" && e.ResourceID != "" {
return fmt.Sprintf("vsax: %s %q not found", e.ResourceType, e.ResourceID)
}
if e.Base.Message != "" {
return "vsax: not found - " + e.Base.Message
}
return "vsax: resource not found"
}
func (e *NotFoundError) Unwrap() error { return ErrNotFound }
// ValidationError represents a 400 response (bad request / validation failure).
type ValidationError struct{ Base Error }
func (e *ValidationError) Error() string {
if e.Base.Message != "" {
return "vsax: validation error - " + e.Base.Message
}
return "vsax: validation error"
}
func (e *ValidationError) Unwrap() error { return ErrBadRequest }