Skip to content

Commit

Permalink
Merge pull request #5 from RanchoCooper/feature/improve
Browse files Browse the repository at this point in the history
Feature/improve
  • Loading branch information
RanchoCooper authored Jan 27, 2022
2 parents 8d07dc9 + 0b68665 commit c2dbd10
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 27 deletions.
33 changes: 24 additions & 9 deletions api/error_code/common_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,29 @@ package error_code
* @date 2022/1/5
*/

// basic error code
var (
Success = NewError(0, "success")
ServerError = NewError(1000, "server internal error")
InvalidParams = NewError(10001, "invalid params")
NotFound = NewError(10002, "record not found")
UnauthorizedAuthNotExist = NewError(10003, "unauthorized, auth not exists")
UnauthorizedTokenError = NewError(10004, "unauthorized, token invalid")
UnauthorizedTokenTimeout = NewError(10005, "unauthorized, token timeout")
UnauthorizedTokenGenerate = NewError(10006, "unauthorized,token generate failed")
TooManyRequests = NewError(10007, "too many requests")
Success = NewError(0, "success")
ServerError = NewError(1000, "server internal error")
InvalidParams = NewError(10001, "invalid params")
NotFound = NewError(10002, "record not found")
)

// auth error code
var (
UnauthorizedAuthNotExist = NewError(20001, "unauthorized, auth not exists")
UnauthorizedTokenError = NewError(20002, "unauthorized, token invalid")
UnauthorizedTokenTimeout = NewError(20003, "unauthorized, token timeout")
UnauthorizedTokenGenerate = NewError(20004, "unauthorized,token generate failed")
)

// internal error code
var (
CopyError = NewError(30001, "copy obj error")
JSONError = NewError(30002, "copy obj error")
)

// other error code
var (
TooManyRequests = NewError(50001, "too many requests")
)
11 changes: 4 additions & 7 deletions api/error_code/error_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,18 @@ func NewError(code int, msg string) *Error {
}
}

func (e *Error) Error() string {
return fmt.Sprintf("err_code: %d, err_msg: %s", e.Code, e.Msg)
func (e Error) Error() string {
return fmt.Sprintf("err_code: %d, err_msg: %s, details: %v", e.Code, e.Msg, e.Details)
}

func (e *Error) Msgf(args []interface{}) string {
func (e Error) Msgf(args []interface{}) string {
return fmt.Sprintf(e.Msg, args...)
}

func (e *Error) WithDetails(details ...string) *Error {
newError := *e
newError.Details = []string{}

for _, d := range details {
newError.Details = append(newError.Details, d)
}
newError.Details = append(newError.Details, details...)

return &newError
}
Expand Down
10 changes: 0 additions & 10 deletions api/error_code/internal_error.go

This file was deleted.

3 changes: 2 additions & 1 deletion api/http/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ func BindAndValid(c *gin.Context, obj interface{}, binder func(interface{}) erro
fieldName := tmpKey[len(tmpKey)-1]
t := reflect.TypeOf(obj)
k := t.Kind()
if k == reflect.Ptr {
for k == reflect.Ptr {
t = t.Elem()
k = t.Kind()
}
field, exists := t.FieldByName(fieldName)
var tag reflect.StructTag
Expand Down

0 comments on commit c2dbd10

Please sign in to comment.