51 lines
1.5 KiB
Go
51 lines
1.5 KiB
Go
|
package common
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"github.com/matchsystems/werr"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
ErrNotImplemented = errors.New("not implemented yet")
|
||
|
|
||
|
ErrCanceled = errors.New("canceled")
|
||
|
ErrDeadlineExceeded = errors.New("deadline exceeded")
|
||
|
ErrRemoteServiceFailed = errors.New("remote service failed")
|
||
|
|
||
|
ErrInvalidAction = errors.New("invalid action")
|
||
|
ErrInvalidArgument = errors.New("invalid argument")
|
||
|
ErrOutOfRange = errors.New("out of range")
|
||
|
|
||
|
ErrPermissionDenied = errors.New("permission denied")
|
||
|
ErrUnauthenticated = errors.New("unauthenticated")
|
||
|
|
||
|
ErrEntityExists = errors.New("entity already exists")
|
||
|
ErrEntityNotFound = errors.New("entity not found")
|
||
|
ErrEntityOutdated = errors.New("entity outdated")
|
||
|
)
|
||
|
|
||
|
func GetHttpCode(err error) int {
|
||
|
err = werr.UnwrapAll(err)
|
||
|
switch {
|
||
|
case errors.Is(err, ErrNotImplemented):
|
||
|
return http.StatusNotImplemented
|
||
|
case errors.Is(err, ErrCanceled), errors.Is(err, ErrDeadlineExceeded):
|
||
|
return http.StatusRequestTimeout
|
||
|
case errors.Is(err, ErrRemoteServiceFailed):
|
||
|
return http.StatusServiceUnavailable
|
||
|
case errors.Is(err, ErrInvalidAction), errors.Is(err, ErrInvalidArgument), errors.Is(err, ErrOutOfRange):
|
||
|
return http.StatusBadRequest
|
||
|
case errors.Is(err, ErrPermissionDenied):
|
||
|
return http.StatusForbidden
|
||
|
case errors.Is(err, ErrUnauthenticated):
|
||
|
return http.StatusUnauthorized
|
||
|
case errors.Is(err, ErrEntityExists):
|
||
|
return http.StatusConflict
|
||
|
case errors.Is(err, ErrEntityNotFound):
|
||
|
return http.StatusNotFound
|
||
|
default:
|
||
|
return http.StatusInternalServerError
|
||
|
}
|
||
|
}
|