feat(backend): add some feat for error message system

errors support custom http code and support return invalid validated
error
This commit is contained in:
yaziming
2019-09-03 01:16:36 +08:00
parent cb3d7263b2
commit 71c83251e5
3 changed files with 80 additions and 36 deletions

View File

@@ -1,8 +1,13 @@
package constants
import "crawlab/errors"
import (
"crawlab/errors"
"net/http"
)
var (
ErrorMongoError = errors.NewSystemOPError(1001, "system error:[mongo]%s", http.StatusInternalServerError)
//users
ErrorUserNotFound = errors.NewBusinessError(10001, "user not found.")
ErrorUserNotFound = errors.NewBusinessError(10001, "user not found.", http.StatusUnauthorized)
ErrorUsernameOrPasswordInvalid = errors.NewBusinessError(11001, "username or password invalid", http.StatusUnauthorized)
)

View File

@@ -1,6 +1,9 @@
package errors
import "fmt"
import (
"fmt"
"net/http"
)
type Scope int
@@ -10,9 +13,10 @@ const (
)
type OPError struct {
Message string
Code int
Scope Scope
HttpCode int
Message string
Code int
Scope Scope
}
func (O OPError) Error() string {
@@ -24,20 +28,28 @@ func (O OPError) Error() string {
case ScopeBusiness:
scope = "business"
}
return fmt.Sprintf("%s : %d -> %s.", scope, O.Code, O.Message)
return fmt.Sprintf("%s error: [%d]%s.", scope, O.Code, O.Message)
}
func NewSystemOPError(code int, message string) *OPError {
func NewSystemOPError(code int, message string, httpCodes ...int) *OPError {
httpCode := http.StatusOK
if len(httpCodes) > 0 {
httpCode = httpCodes[0]
}
return NewOpError(code, message, ScopeSystem, httpCode)
}
func NewOpError(code int, message string, scope Scope, httpCode int) *OPError {
return &OPError{
Message: message,
Code: code,
Scope: ScopeSystem,
Message: message,
Code: code,
Scope: scope,
HttpCode: httpCode,
}
}
func NewBusinessError(code int, message string) *OPError {
return &OPError{
Message: message,
Code: code,
Scope: ScopeBusiness,
func NewBusinessError(code int, message string, httpCodes ...int) *OPError {
httpCode := http.StatusOK
if len(httpCodes) > 0 {
httpCode = httpCodes[0]
}
return NewOpError(code, message, ScopeBusiness, httpCode)
}

View File

@@ -8,6 +8,7 @@ import (
"github.com/apex/log"
"github.com/gin-gonic/gin"
errors2 "github.com/pkg/errors"
"gopkg.in/go-playground/validator.v9"
"net/http"
"runtime/debug"
)
@@ -27,9 +28,12 @@ func (c *Context) User() *model.User {
}
return user
}
func (c *Context) Success(data interface{}, meta interface{}) {
if meta == nil {
func (c *Context) Success(data interface{}, metas ...interface{}) {
var meta interface{}
if len(metas) == 0 {
meta = gin.H{}
} else {
meta = metas[0]
}
if data == nil {
data = gin.H{}
@@ -38,33 +42,56 @@ func (c *Context) Success(data interface{}, meta interface{}) {
"status": "ok",
"message": "success",
"data": data,
"meta": meta,
"error": "",
})
}
func (c *Context) Failed(err error, variables ...interface{}) {
c.failed(err, http.StatusOK, variables...)
}
func (c *Context) failed(err error, httpCode int, variables ...interface{}) {
errStr := err.Error()
if len(variables) > 0 {
errStr = fmt.Sprintf(errStr, variables...)
}
log.Errorf("handle error:" + errStr)
debug.PrintStack()
causeError := errors2.Cause(err)
switch causeError.(type) {
case errors.OPError:
opError := causeError.(errors.OPError)
c.AbortWithStatusJSON(opError.HttpCode, gin.H{
"status": "ok",
"message": "error",
"error": errStr,
})
break
case validator.ValidationErrors:
validatorErrors := causeError.(validator.ValidationErrors)
//firstError := validatorErrors[0].(validator.FieldError)
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
"status": "ok",
"message": "error",
"error": validatorErrors.Error(),
})
break
default:
fmt.Println("deprecated....")
c.AbortWithStatusJSON(httpCode, gin.H{
"status": "ok",
"message": "error",
"error": errStr,
})
}
}
func (c *Context) FailedWithError(err error, httpCode ...int) {
var code = 200
if len(httpCode) > 0 {
code = httpCode[0]
}
log.Errorf("handle error:" + err.Error())
debug.PrintStack()
switch errors2.Cause(err).(type) {
case errors.OPError:
c.AbortWithStatusJSON(code, gin.H{
"status": "ok",
"message": "error",
"error": err.Error(),
})
break
default:
fmt.Println("deprecated....")
c.AbortWithStatusJSON(code, gin.H{
"status": "ok",
"message": "error",
"error": err.Error(),
})
}
c.failed(err, code)
}