You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
936 B
37 lines
936 B
package middleware
|
|
|
|
import (
|
|
"errors"
|
|
"ims/util/jwt"
|
|
"ims/util/rsp"
|
|
|
|
"gorm.io/gorm"
|
|
"zestack.dev/slim"
|
|
)
|
|
|
|
func ErrorHandler(c slim.Context, err error) {
|
|
if c.Written() {
|
|
return
|
|
}
|
|
|
|
switch {
|
|
case errors.Is(err, gorm.ErrRecordNotFound):
|
|
err = rsp.ErrRecordNotFound
|
|
case errors.Is(err, gorm.ErrDuplicatedKey):
|
|
err = rsp.ErrInternal.WithText(rsp.ErrInternal.Text() + ",数据已经存在")
|
|
case errors.Is(err, jwt.ErrInvalidToken),
|
|
errors.Is(err, jwt.ErrUnauthorized),
|
|
errors.Is(err, jwt.ErrTokenNotFound):
|
|
err = rsp.ErrUnauthorized
|
|
case errors.Is(err, jwt.ErrTokenExpired):
|
|
err = rsp.ErrUnauthorized.WithText("授权令牌已过期")
|
|
case errors.Is(err, slim.ErrNotFound):
|
|
err = rsp.ErrNotFound
|
|
case errors.Is(err, slim.ErrMethodNotAllowed):
|
|
err = rsp.ErrMethodNotAllowed
|
|
case errors.Is(err, slim.ErrUnsupportedMediaType):
|
|
err = rsp.ErrUnsupportedMediaType.WithInternal(err)
|
|
}
|
|
|
|
_ = rsp.Fail(c, err)
|
|
}
|
|
|