-
Notifications
You must be signed in to change notification settings - Fork 907
/
smart.go
60 lines (49 loc) · 1.45 KB
/
smart.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
package response
import (
"database/sql"
"errors"
"net/http"
"os"
"github.com/canonical/lxd/shared/api"
)
var httpResponseErrors = map[int][]error{
http.StatusNotFound: {os.ErrNotExist, sql.ErrNoRows},
http.StatusForbidden: {os.ErrPermission},
}
// SmartError returns the right error message based on err.
// It uses the stdlib errors package to unwrap the error and find the cause.
func SmartError(err error) Response {
if err == nil {
return EmptySyncResponse
}
statusCode, found := api.StatusErrorMatch(err)
if found {
return &errorResponse{statusCode, err.Error()}
}
for httpStatusCode, checkErrs := range httpResponseErrors {
for _, checkErr := range checkErrs {
if errors.Is(err, checkErr) {
if err != checkErr {
// If the error has been wrapped return the top-level error message.
return &errorResponse{httpStatusCode, err.Error()}
}
// If the error hasn't been wrapped, replace the error message with the generic
// HTTP status text.
return &errorResponse{httpStatusCode, http.StatusText(httpStatusCode)}
}
}
}
return &errorResponse{http.StatusInternalServerError, err.Error()}
}
// IsNotFoundError returns true if the error is considered a Not Found error.
func IsNotFoundError(err error) bool {
if api.StatusErrorCheck(err, http.StatusNotFound) {
return true
}
for _, checkErr := range httpResponseErrors[http.StatusNotFound] {
if errors.Is(err, checkErr) {
return true
}
}
return false
}