mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-21 02:04:26 +08:00
add core/error/util files
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
package errs
|
||||
|
||||
type ErrorCategory int
|
||||
|
||||
const (
|
||||
CATEGORY_SYSTEM ErrorCategory = 1
|
||||
CATEGORY_NORMAL ErrorCategory = 2
|
||||
|
||||
SYSTEM_SUBCATEGORY_DEFAULT = 0
|
||||
SYSTEM_SUBCATEGORY_SETTING = 1
|
||||
SYSTEM_SUBCATEGORY_DATABASE = 2
|
||||
|
||||
NORMAL_SUBCATEGORY_GLOBAL = 0
|
||||
NORMAL_SUBCATEGORY_USER = 1
|
||||
NORMAL_SUBCATEGORY_TOKEN = 2
|
||||
NORMAL_SUBCATEGORY_TWOFACTOR = 3
|
||||
NORMAL_SUBCATEGORY_ACCOUNT = 4
|
||||
NORMAL_SUBCATEGORY_JOURNAL = 5
|
||||
NORMAL_SUBCATEGORY_CATEGORY = 6
|
||||
NORMAL_SUBCATEGORY_TAG = 7
|
||||
)
|
||||
|
||||
type Error struct {
|
||||
Category ErrorCategory
|
||||
SubCategory int
|
||||
Index int
|
||||
HttpStatusCode int
|
||||
Message string
|
||||
BaseError []error
|
||||
}
|
||||
|
||||
func (err *Error) Error() string {
|
||||
return err.Message
|
||||
}
|
||||
|
||||
func (err *Error) Code() int {
|
||||
return int(err.Category)*100000 + err.SubCategory*1000 + err.Index
|
||||
}
|
||||
|
||||
func New(category ErrorCategory, subCategory int, index int, httpStatusCode int, message string, baseError ...error) *Error {
|
||||
return &Error{
|
||||
Category: category,
|
||||
SubCategory: subCategory,
|
||||
Index: index,
|
||||
HttpStatusCode: httpStatusCode,
|
||||
Message: message,
|
||||
BaseError: baseError,
|
||||
}
|
||||
}
|
||||
|
||||
func NewSystemError(subCategory int, index int, httpStatusCode int, message string) *Error {
|
||||
return New(CATEGORY_SYSTEM, subCategory, index, httpStatusCode, message)
|
||||
}
|
||||
|
||||
func NewNormalError(subCategory int, index int, httpStatusCode int, message string) *Error {
|
||||
return New(CATEGORY_NORMAL, subCategory, index, httpStatusCode, message)
|
||||
}
|
||||
|
||||
func NewIncompleteOrIncorrectSubmissionError(err error) *Error {
|
||||
return New(ErrIncompleteOrIncorrectSubmission.Category,
|
||||
ErrIncompleteOrIncorrectSubmission.SubCategory,
|
||||
ErrIncompleteOrIncorrectSubmission.Index,
|
||||
ErrIncompleteOrIncorrectSubmission.HttpStatusCode,
|
||||
ErrIncompleteOrIncorrectSubmission.Message, err)
|
||||
}
|
||||
|
||||
func Or(err error, defaultErr *Error) *Error {
|
||||
if finalError, ok := err.(*Error); ok {
|
||||
return finalError
|
||||
} else {
|
||||
return defaultErr
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user