mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-14 06:57:35 +08:00
support periodically cleaning up expired tokens
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
package errs
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ErrorCategory represents error category
|
||||
type ErrorCategory int32
|
||||
|
||||
@@ -16,6 +20,7 @@ const (
|
||||
SystemSubcategoryDatabase = 2
|
||||
SystemSubcategoryMail = 3
|
||||
SystemSubcategoryLogging = 4
|
||||
SystemSubcategoryCron = 5
|
||||
)
|
||||
|
||||
// Sub categories of normal error
|
||||
@@ -44,6 +49,10 @@ type Error struct {
|
||||
Context any
|
||||
}
|
||||
|
||||
type MultiErrors struct {
|
||||
errors []error
|
||||
}
|
||||
|
||||
// Error returns the error message
|
||||
func (err *Error) Error() string {
|
||||
return err.Message
|
||||
@@ -66,6 +75,34 @@ func New(category ErrorCategory, subCategory int32, index int32, httpStatusCode
|
||||
}
|
||||
}
|
||||
|
||||
// Error returns the error message
|
||||
func (err *MultiErrors) Error() string {
|
||||
if len(err.errors) == 1 {
|
||||
return err.errors[0].Error()
|
||||
}
|
||||
|
||||
var ret strings.Builder
|
||||
var lastErrorChar byte
|
||||
|
||||
ret.WriteString("multi errors: ")
|
||||
|
||||
for i := 0; i < len(err.errors); i++ {
|
||||
if i > 0 {
|
||||
if lastErrorChar == '.' {
|
||||
ret.WriteString(" ")
|
||||
} else {
|
||||
ret.WriteString(", ")
|
||||
}
|
||||
}
|
||||
|
||||
errorContent := err.errors[i].Error()
|
||||
lastErrorChar = errorContent[len(errorContent)-1]
|
||||
ret.WriteString(errorContent)
|
||||
}
|
||||
|
||||
return ret.String()
|
||||
}
|
||||
|
||||
// NewSystemError returns a new system error instance
|
||||
func NewSystemError(subCategory int32, index int32, httpStatusCode int, message string) *Error {
|
||||
return New(CATEGORY_SYSTEM, subCategory, index, httpStatusCode, message)
|
||||
@@ -107,6 +144,21 @@ func NewErrorWithContext(baseError *Error, context any) *Error {
|
||||
}
|
||||
}
|
||||
|
||||
// NewMultiErrorOrNil returns a new multi error instance
|
||||
func NewMultiErrorOrNil(errors ...error) error {
|
||||
count := len(errors)
|
||||
|
||||
if count < 1 {
|
||||
return nil
|
||||
} else if count == 1 {
|
||||
return errors[0]
|
||||
}
|
||||
|
||||
return &MultiErrors{
|
||||
errors: errors,
|
||||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
|
||||
Reference in New Issue
Block a user