-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_helpers.go
46 lines (44 loc) · 1.1 KB
/
http_helpers.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
package errdefs
import (
"net/http"
)
// FromStatusCode creates an errdef error, based on the provided HTTP status-code
func FromStatusCode(err error, statusCode int) error {
if err == nil {
return nil
}
switch statusCode {
case http.StatusNotFound:
err = NotFound(err)
case http.StatusBadRequest:
err = InvalidParameter(err)
case http.StatusConflict:
err = Conflict(err)
case http.StatusUnauthorized:
err = Unauthorized(err)
case http.StatusServiceUnavailable:
err = Unavailable(err)
case http.StatusForbidden:
err = Forbidden(err)
case http.StatusNotModified:
err = NotModified(err)
case http.StatusNotImplemented:
err = NotImplemented(err)
case http.StatusInternalServerError:
if !IsSystem(err) && !IsUnknown(err) && !IsDataLoss(err) && !IsDeadline(err) && !IsCancelled(err) {
err = System(err)
}
default:
switch {
case statusCode >= 200 && statusCode < 400:
// it's a client error
case statusCode >= 400 && statusCode < 500:
err = InvalidParameter(err)
case statusCode >= 500 && statusCode < 600:
err = System(err)
default:
err = Unknown(err)
}
}
return err
}