-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
72 lines (65 loc) · 1.58 KB
/
errors.go
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
package setting
import (
"errors"
)
const (
// SettingNotFound is the status code returned when a setting is not found.
SettingNotFound = 404
)
var (
// ErrParseConnectionString is returned when a connection string cannot be parsed.
ErrParseConnectionString = errors.New("failed to parse connection string")
)
// settingError represents an error returned from the App Configuration
// REST API.
type settingError struct {
Type string `json:"type"`
Title string `json:"title"`
Name string `json:"name"`
Detail string `json:"detail"`
Status int `json:"status"`
StatusCode int
}
// Error returns the detail from the settingError.
func (e settingError) Error() string {
return e.Detail
}
// newSettingError creates a new settingError with the provided key and status code.
func newSettingError(statusCode int) settingError {
var detail string
switch statusCode {
case 400:
detail = "bad request"
case 401:
detail = "not authorized"
case 403:
detail = "forbidden"
case 404:
detail = "not found"
case 500:
detail = "internal server error"
case 502:
detail = "bad gateway"
case 503:
detail = "service unavailable"
case 504:
detail = "gateway timeout"
default:
detail = "unknown error"
}
return settingError{
Detail: detail,
StatusCode: statusCode,
}
}
// isSettingNotFound checks if the provided error is a settingError with
// the status code 404.
func isSettingNotFound(err error) bool {
var settingErr settingError
if errors.As(err, &settingErr) {
if settingErr.StatusCode == SettingNotFound {
return true
}
}
return false
}