-
Notifications
You must be signed in to change notification settings - Fork 18.7k
/
http_helpers.go
47 lines (45 loc) · 1.13 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
47
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:
return NotFound(err)
case http.StatusBadRequest:
return InvalidParameter(err)
case http.StatusConflict:
return Conflict(err)
case http.StatusUnauthorized:
return Unauthorized(err)
case http.StatusServiceUnavailable:
return Unavailable(err)
case http.StatusForbidden:
return Forbidden(err)
case http.StatusNotModified:
return NotModified(err)
case http.StatusNotImplemented:
return NotImplemented(err)
case http.StatusInternalServerError:
if IsCancelled(err) || IsSystem(err) || IsUnknown(err) || IsDataLoss(err) || IsDeadline(err) {
return err
}
return System(err)
default:
switch {
case statusCode >= 200 && statusCode < 400:
// it's a client error
return err
case statusCode >= 400 && statusCode < 500:
return InvalidParameter(err)
case statusCode >= 500 && statusCode < 600:
return System(err)
default:
return Unknown(err)
}
}
}