f1a6e16258
A package only needs one "import" comment to enforce, so keeping one in the go.doc. It should be noted that even with that; in most cases, go will ignore these comments (if go modules are used, even in "vendor" mode). Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
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
|
|
}
|