Files
crawlab/backend/errors/errors.go
yaziming 81f6cf021f Backend:
improve
     - AuthMiddleware 注入当前用户的信息
     - 增加Context服务支持快捷获取当前登录者信息
     - 重构Login/GetMe接口逻辑避免重复的数据库查询
     - 规范化error信息声明(向下兼容,旧代码可逐渐迁移规范化)
     - 修正部分不符合规范的代码
2019-08-31 21:26:56 +08:00

44 lines
692 B
Go

package errors
import "fmt"
type Scope int
const (
ScopeSystem Scope = 1
ScopeBusiness Scope = 2
)
type OPError struct {
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 : %d -> %s.", scope, O.Code, O.Message)
}
func NewSystemOPError(code int, message string) *OPError {
return &OPError{
Message: message,
Code: code,
Scope: ScopeSystem,
}
}
func NewBusinessError(code int, message string) *OPError {
return &OPError{
Message: message,
Code: code,
Scope: ScopeBusiness,
}
}