Revert "Revert "V0.4.0 imporve error response""

This reverts commit 9744f45e86.
This commit is contained in:
yaziming
2019-09-04 20:41:37 +08:00
parent bb4ec17125
commit 6dc687192e
51 changed files with 1373 additions and 416 deletions

55
backend/errors/errors.go Normal file
View File

@@ -0,0 +1,55 @@
package errors
import (
"fmt"
"net/http"
)
type Scope int
const (
ScopeSystem Scope = 1
ScopeBusiness Scope = 2
)
type OPError struct {
HttpCode int
Message string
Code int
Scope Scope
}
func (O OPError) Error() string {
var scope string
switch O.Scope {
case ScopeSystem:
scope = "system"
break
case ScopeBusiness:
scope = "business"
}
return fmt.Sprintf("%s error: [%d]%s.", scope, O.Code, O.Message)
}
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: scope,
HttpCode: httpCode,
}
}
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)
}