mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-14 06:57:35 +08:00
add comments
This commit is contained in:
@@ -1,15 +1,23 @@
|
||||
package errs
|
||||
|
||||
// ErrorCategory represents error category
|
||||
type ErrorCategory int
|
||||
|
||||
// Error categories
|
||||
const (
|
||||
CATEGORY_SYSTEM ErrorCategory = 1
|
||||
CATEGORY_NORMAL ErrorCategory = 2
|
||||
)
|
||||
|
||||
// Sub categories of system error
|
||||
const (
|
||||
SYSTEM_SUBCATEGORY_DEFAULT = 0
|
||||
SYSTEM_SUBCATEGORY_SETTING = 1
|
||||
SYSTEM_SUBCATEGORY_DATABASE = 2
|
||||
)
|
||||
|
||||
// Sub categories of normal error
|
||||
const (
|
||||
NORMAL_SUBCATEGORY_GLOBAL = 0
|
||||
NORMAL_SUBCATEGORY_USER = 1
|
||||
NORMAL_SUBCATEGORY_TOKEN = 2
|
||||
@@ -20,6 +28,7 @@ const (
|
||||
NORMAL_SUBCATEGORY_TAG = 7
|
||||
)
|
||||
|
||||
// Error represents the specific error returned to user
|
||||
type Error struct {
|
||||
Category ErrorCategory
|
||||
SubCategory int
|
||||
@@ -29,14 +38,17 @@ type Error struct {
|
||||
BaseError []error
|
||||
}
|
||||
|
||||
// Error returns the error message
|
||||
func (err *Error) Error() string {
|
||||
return err.Message
|
||||
}
|
||||
|
||||
// Code returns the error code
|
||||
func (err *Error) Code() int {
|
||||
return int(err.Category)*100000 + err.SubCategory*1000 + err.Index
|
||||
}
|
||||
|
||||
// New returns a new error instance
|
||||
func New(category ErrorCategory, subCategory int, index int, httpStatusCode int, message string, baseError ...error) *Error {
|
||||
return &Error{
|
||||
Category: category,
|
||||
@@ -48,14 +60,17 @@ func New(category ErrorCategory, subCategory int, index int, httpStatusCode int,
|
||||
}
|
||||
}
|
||||
|
||||
// NewSystemError returns a new system error instance
|
||||
func NewSystemError(subCategory int, index int, httpStatusCode int, message string) *Error {
|
||||
return New(CATEGORY_SYSTEM, subCategory, index, httpStatusCode, message)
|
||||
}
|
||||
|
||||
// NewNormalError returns a new normal error instance
|
||||
func NewNormalError(subCategory int, index int, httpStatusCode int, message string) *Error {
|
||||
return New(CATEGORY_NORMAL, subCategory, index, httpStatusCode, message)
|
||||
}
|
||||
|
||||
// NewIncompleteOrIncorrectSubmissionError returns a new incomplete or incorrect submission error instance
|
||||
func NewIncompleteOrIncorrectSubmissionError(err error) *Error {
|
||||
return New(ErrIncompleteOrIncorrectSubmission.Category,
|
||||
ErrIncompleteOrIncorrectSubmission.SubCategory,
|
||||
@@ -64,6 +79,8 @@ func NewIncompleteOrIncorrectSubmissionError(err error) *Error {
|
||||
ErrIncompleteOrIncorrectSubmission.Message, err)
|
||||
}
|
||||
|
||||
// Or would return the error from err parameter if the this error is defined in this project,
|
||||
// or return the default error
|
||||
func Or(err error, defaultErr *Error) *Error {
|
||||
if finalError, ok := err.(*Error); ok {
|
||||
return finalError
|
||||
@@ -72,6 +89,7 @@ func Or(err error, defaultErr *Error) *Error {
|
||||
}
|
||||
}
|
||||
|
||||
// IsCustomError returns whether this error is defined in this project
|
||||
func IsCustomError(err error) bool {
|
||||
_, ok := err.(*Error)
|
||||
return ok
|
||||
|
||||
Reference in New Issue
Block a user