Skip to content

Commit

Permalink
Merge pull request go-eagle#70 from luoxiaohei/master
Browse files Browse the repository at this point in the history
feat(errcode): allow custom HTTP status code of `errcode.Error`
  • Loading branch information
qloog authored Sep 17, 2022
2 parents 2617f67 + 86a006e commit efe7371
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions pkg/errcode/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package errcode
import (
"fmt"
"net/http"
"sync"
)

// Error 返回错误码和消息的结构体
Expand All @@ -14,6 +15,7 @@ type Error struct {
}

var errorCodes = map[int]struct{}{}
var toStatus sync.Map

// NewError create a error
func NewError(code int, msg string) *Error {
Expand Down Expand Up @@ -58,25 +60,15 @@ func (e *Error) WithDetails(details ...string) *Error {
return &newError
}

// SetHTTPStatusCode set a specific http status code to err
func SetHTTPStatusCode(err *Error, status int) {
toStatus.Store(err.Code(), status)
}

// ToHTTPStatusCode convert custom error code to http status code and avoid return unknown status code.
func ToHTTPStatusCode(code int) int {
switch code {
case Success.Code():
return http.StatusOK
case ErrInternalServer.Code():
return http.StatusInternalServerError
case ErrInvalidParam.Code():
return http.StatusBadRequest
case ErrToken.Code():
fallthrough
case ErrInvalidToken.Code():
fallthrough
case ErrTokenTimeout.Code():
return http.StatusUnauthorized
case ErrTooManyRequests.Code():
return http.StatusTooManyRequests
case ErrServiceUnavailable.Code():
return http.StatusServiceUnavailable
if status, ok := toStatus.Load(code); ok {
return status.(int)
}

return http.StatusInternalServerError
Expand Down Expand Up @@ -110,3 +102,23 @@ func DecodeErr(err error) (int, string) {

return ErrInternalServer.Code(), err.Error()
}

func initToStatus() {
for code, status := range map[int]int{
Success.Code(): http.StatusOK,
ErrInternalServer.Code(): http.StatusInternalServerError,
ErrNotFound.Code(): http.StatusNotFound,
ErrInvalidParam.Code(): http.StatusBadRequest,
ErrToken.Code(): http.StatusUnauthorized,
ErrInvalidToken.Code(): http.StatusUnauthorized,
ErrTokenTimeout.Code(): http.StatusUnauthorized,
ErrTooManyRequests.Code(): http.StatusTooManyRequests,
ErrServiceUnavailable.Code(): http.StatusServiceUnavailable,
} {
toStatus.Store(code, status)
}
}

func init() {
initToStatus()
}

0 comments on commit efe7371

Please sign in to comment.