mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-06 04:15:46 +08:00
Merge branch 'errcode' of github.com:OpenIMSDK/Open-IM-Server into errcode
This commit is contained in:
commit
c33dab0e30
@ -1,10 +1,15 @@
|
||||
package apiresp
|
||||
|
||||
import (
|
||||
"OpenIM/pkg/errs"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type apiResponse struct {
|
||||
ErrCode int `json:"errCode"`
|
||||
ErrMsg string `json:"errMsg"`
|
||||
ErrDlt string `json:"errDlt"`
|
||||
Data any `json:"data"`
|
||||
Data any `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
func apiSuccess(data any) *apiResponse {
|
||||
@ -14,5 +19,13 @@ func apiSuccess(data any) *apiResponse {
|
||||
}
|
||||
|
||||
func apiError(err error) *apiResponse {
|
||||
return &apiResponse{ErrCode: 10000, ErrMsg: err.Error()}
|
||||
unwrap := errs.Unwrap(err)
|
||||
var dlt string
|
||||
if unwrap != err {
|
||||
dlt = fmt.Sprintf("%+v", dlt)
|
||||
}
|
||||
if codeErr, ok := unwrap.(errs.CodeError); ok {
|
||||
return &apiResponse{ErrCode: codeErr.Code(), ErrMsg: codeErr.Msg(), ErrDlt: dlt}
|
||||
}
|
||||
return &apiResponse{ErrCode: errs.ServerInternalError, ErrMsg: err.Error(), ErrDlt: dlt}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
"OpenIM/pkg/errs"
|
||||
"context"
|
||||
"fmt"
|
||||
mysqlDriver "github.com/go-sql-driver/mysql"
|
||||
"gorm.io/driver/mysql"
|
||||
"strings"
|
||||
"time"
|
||||
@ -65,9 +66,24 @@ func newMysqlGormDB() (*gorm.DB, error) {
|
||||
// gorm mysql
|
||||
func NewGormDB() (*gorm.DB, error) {
|
||||
specialerror.AddReplace(gorm.ErrRecordNotFound, errs.ErrRecordNotFound)
|
||||
specialerror.AddErrHandler(replaceDuplicateKey)
|
||||
return newMysqlGormDB()
|
||||
}
|
||||
|
||||
func replaceDuplicateKey(err error) errs.CodeError {
|
||||
if IsMysqlDuplicateKey(err) {
|
||||
return errs.ErrDuplicateKey
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func IsMysqlDuplicateKey(err error) bool {
|
||||
if mysqlErr, ok := err.(*mysqlDriver.MySQLError); ok {
|
||||
return mysqlErr.Number == 1062
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type Writer struct{}
|
||||
|
||||
func (w Writer) Printf(format string, args ...interface{}) {
|
||||
|
@ -40,6 +40,8 @@ const (
|
||||
CallbackError = 80000
|
||||
)
|
||||
|
||||
const DuplicateKeyError = 12345
|
||||
|
||||
// 账号错误码
|
||||
const (
|
||||
UserIDNotFoundError = 91001 //UserID不存在 或未注册
|
||||
|
@ -41,6 +41,7 @@ var (
|
||||
ErrTokenNotExist = NewCodeError(TokenNotExistError, "TokenNotExistError") //在redis中不存在
|
||||
ErrTokenDifferentPlatformID = NewCodeError(TokenDifferentPlatformIDError, "TokenDifferentPlatformIDError")
|
||||
ErrTokenDifferentUserID = NewCodeError(TokenDifferentUserIDError, "TokenDifferentUserIDError")
|
||||
ErrDuplicateKey = NewCodeError(DuplicateKeyError, "DuplicateKeyError")
|
||||
|
||||
ErrMessageHasReadDisable = NewCodeError(MessageHasReadDisable, "MessageHasReadDisable")
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user