-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patherr_status_code.go
41 lines (36 loc) · 1.15 KB
/
err_status_code.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
package errors
import (
"errors"
"net/http"
)
// StatusCoder is implemented by any value that has a StatusCode method.
// The method is used to map the type or classification of error to the
// HTTP status code for the response from server.
type StatusCoder interface {
StatusCode() int
}
// StatusCode attempts to determine the HTTP status code which is
// suitable for the error response. If the error does not implement
// StatusCoder or if it lacks type/classification info,
// http.StatusInternalServerError is returned. This applies for `nil`
// error as well and this case should be guarded with a nil check at the
// caller side.
func StatusCode(err error) int {
if err == nil {
return http.StatusInternalServerError
}
if e, ok := err.(StatusCoder); ok && e.StatusCode() != 0 {
return e.StatusCode()
}
return StatusCode(errors.Unwrap(err))
}
// StatusCode attempts to determine the HTTP status code which is
// suitable for the error Kind.
func (e *Error) StatusCode() int {
if e.Kind == Unknown {
// Return 0 because when the error kind is not present(Unknown), try and
// delegate to the errors down the chain.
return 0
}
return e.Kind.Status
}