-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
42 lines (36 loc) · 1.05 KB
/
errors.go
File metadata and controls
42 lines (36 loc) · 1.05 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
// Package sirv provides a Go SDK for the Sirv REST API.
package sirv
import "fmt"
// APIError represents an error returned by the Sirv API.
type APIError struct {
StatusCode int `json:"statusCode"`
ErrorCode string `json:"error,omitempty"`
Message string `json:"message"`
}
// Error implements the error interface.
func (e *APIError) Error() string {
if e.ErrorCode != "" {
return fmt.Sprintf("sirv: %s (status %d, code %s)", e.Message, e.StatusCode, e.ErrorCode)
}
return fmt.Sprintf("sirv: %s (status %d)", e.Message, e.StatusCode)
}
// NewAPIError creates a new API error.
func NewAPIError(statusCode int, message string, errorCode string) *APIError {
return &APIError{
StatusCode: statusCode,
Message: message,
ErrorCode: errorCode,
}
}
// IsAPIError checks if an error is an APIError.
func IsAPIError(err error) bool {
_, ok := err.(*APIError)
return ok
}
// GetAPIError returns the APIError if err is one, otherwise nil.
func GetAPIError(err error) *APIError {
if apiErr, ok := err.(*APIError); ok {
return apiErr
}
return nil
}