mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-21 10:14:26 +08:00
add core/error/util files
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
package errs
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrDatabaseTypeInvalid = NewSystemError(SYSTEM_SUBCATEGORY_DATABASE, 0, http.StatusInternalServerError, "database type is invalid")
|
||||
ErrDatabaseHostInvalid = NewSystemError(SYSTEM_SUBCATEGORY_DATABASE, 1, http.StatusInternalServerError, "database host is invalid")
|
||||
ErrDatabaseIsNull = NewSystemError(SYSTEM_SUBCATEGORY_DATABASE, 2, http.StatusInternalServerError, "database cannot be null")
|
||||
)
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package errs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrIncompleteOrIncorrectSubmission = NewNormalError(NORMAL_SUBCATEGORY_GLOBAL, 0, http.StatusBadRequest, "incomplete or incorrect submission")
|
||||
ErrOperationFailed = NewNormalError(NORMAL_SUBCATEGORY_GLOBAL, 1, http.StatusInternalServerError, "operation failed")
|
||||
ErrRequestIdInvalid = NewNormalError(NORMAL_SUBCATEGORY_GLOBAL, 2, http.StatusInternalServerError, "request id is invalid")
|
||||
ErrCiphertextInvalid = NewNormalError(NORMAL_SUBCATEGORY_GLOBAL, 3, http.StatusInternalServerError, "ciphertext is invalid")
|
||||
ErrNothingWillBeUpdated = NewNormalError(NORMAL_SUBCATEGORY_GLOBAL, 4, http.StatusBadRequest, "nothing will be updated")
|
||||
)
|
||||
|
||||
func GetParameterInvalidMessage(field string) string {
|
||||
return fmt.Sprintf("parameter \"%s\" is invalid", field)
|
||||
}
|
||||
|
||||
func GetParameterIsRequiredMessage(field string) string {
|
||||
return fmt.Sprintf("parameter \"%s\" is required", field)
|
||||
}
|
||||
|
||||
func GetParameterMustLessThanMessage(field string, param string) string {
|
||||
return fmt.Sprintf("parameter \"%s\" must be less than %s", field, param)
|
||||
}
|
||||
|
||||
func GetParameterMustMoreThanMessage(field string, param string) string {
|
||||
return fmt.Sprintf("parameter \"%s\" must be more than %s", field, param)
|
||||
}
|
||||
|
||||
func GetParameterLengthNotEqualMessage(field string, param string) string {
|
||||
return fmt.Sprintf("parameter \"%s\" length is not equal to %s", field, param)
|
||||
}
|
||||
|
||||
func GetParameterNotBeBlankMessage(field string) string {
|
||||
return fmt.Sprintf("parameter \"%s\" cannot be blank", field)
|
||||
}
|
||||
|
||||
func GetParameterInvalidUsernameMessage(field string) string {
|
||||
return fmt.Sprintf("parameter \"%s\" is invalid username format", field)
|
||||
}
|
||||
|
||||
func GetParameterInvalidEmailMessage(field string) string {
|
||||
return fmt.Sprintf("parameter \"%s\" is invalid email format", field)
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package errs
|
||||
|
||||
import "net/http"
|
||||
|
||||
var (
|
||||
ErrInvalidProtocol = NewSystemError(SYSTEM_SUBCATEGORY_SETTING, 0, http.StatusInternalServerError, "invalid server protocol")
|
||||
ErrInvalidLogMode = NewSystemError(SYSTEM_SUBCATEGORY_SETTING, 1, http.StatusInternalServerError, "invalid log mode")
|
||||
ErrGettingLocalAddress = NewSystemError(SYSTEM_SUBCATEGORY_SETTING, 2, http.StatusInternalServerError, "failed to get local address")
|
||||
ErrInvalidUuidMode = NewSystemError(SYSTEM_SUBCATEGORY_SETTING, 3, http.StatusInternalServerError, "invalid uuid mode")
|
||||
)
|
||||
@@ -0,0 +1,9 @@
|
||||
package errs
|
||||
|
||||
import "net/http"
|
||||
|
||||
var (
|
||||
ErrSystemError = NewSystemError(SYSTEM_SUBCATEGORY_DEFAULT, 0, http.StatusInternalServerError, "system error")
|
||||
ErrApiNotFound = NewSystemError(SYSTEM_SUBCATEGORY_DEFAULT, 1, http.StatusNotFound, "api not found")
|
||||
ErrMethodNotAllowed = NewSystemError(SYSTEM_SUBCATEGORY_DEFAULT, 2, http.StatusMethodNotAllowed, "method not allowed")
|
||||
)
|
||||
@@ -0,0 +1,18 @@
|
||||
package errs
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrTokenGenerating = NewNormalError(NORMAL_SUBCATEGORY_TOKEN, 0, http.StatusInternalServerError, "failed to generate token")
|
||||
ErrUnauthorizedAccess = NewNormalError(NORMAL_SUBCATEGORY_TOKEN, 1, http.StatusUnauthorized, "unauthorized access")
|
||||
ErrTokenExpired = NewNormalError(NORMAL_SUBCATEGORY_TOKEN, 2, http.StatusUnauthorized, "token is expired")
|
||||
ErrInvalidToken = NewNormalError(NORMAL_SUBCATEGORY_TOKEN, 3, http.StatusUnauthorized, "token is invalid")
|
||||
ErrInvalidUserTokenId = NewNormalError(NORMAL_SUBCATEGORY_TOKEN, 4, http.StatusUnauthorized, "user token id is invalid")
|
||||
ErrInvalidTokenId = NewNormalError(NORMAL_SUBCATEGORY_TOKEN, 5, http.StatusUnauthorized, "token id is invalid")
|
||||
ErrTokenRecordNotFound = NewNormalError(NORMAL_SUBCATEGORY_TOKEN, 6, http.StatusUnauthorized, "token is not found")
|
||||
ErrInvalidTokenType = NewNormalError(NORMAL_SUBCATEGORY_TOKEN, 7, http.StatusUnauthorized, "token type is invalid")
|
||||
ErrTokenRequire2FA = NewNormalError(NORMAL_SUBCATEGORY_TOKEN, 8, http.StatusUnauthorized, "token requires two factor authorization")
|
||||
ErrTokenNotRequire2FA = NewNormalError(NORMAL_SUBCATEGORY_TOKEN, 9, http.StatusUnauthorized, "token does not require two factor authorization")
|
||||
)
|
||||
@@ -0,0 +1,11 @@
|
||||
package errs
|
||||
|
||||
import "net/http"
|
||||
|
||||
var (
|
||||
ErrPasscodeInvalid = NewNormalError(NORMAL_SUBCATEGORY_TWOFACTOR, 0, http.StatusUnauthorized, "passcode is invalid")
|
||||
ErrTwoFactorRecoveryCodeInvalid = NewNormalError(NORMAL_SUBCATEGORY_TWOFACTOR, 1, http.StatusUnauthorized, "two factor recovery code is invalid")
|
||||
ErrTwoFactorKeyIsNotEnabled = NewNormalError(NORMAL_SUBCATEGORY_TWOFACTOR, 2, http.StatusBadRequest, "two factor key is not enabled")
|
||||
ErrTwoFactorKeyAlreadyEnabled = NewNormalError(NORMAL_SUBCATEGORY_TWOFACTOR, 3, http.StatusBadRequest, "two factor key has already been enabled")
|
||||
ErrTwoFactorRecoveryCodeNotExist = NewNormalError(NORMAL_SUBCATEGORY_TWOFACTOR, 4, http.StatusUnauthorized, "two factor recovery code does not exist")
|
||||
)
|
||||
@@ -0,0 +1,18 @@
|
||||
package errs
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrUserIdInvalid = NewNormalError(NORMAL_SUBCATEGORY_USER, 0, http.StatusBadRequest, "user id is invalid")
|
||||
ErrUsernameIsEmpty = NewNormalError(NORMAL_SUBCATEGORY_USER, 1, http.StatusBadRequest, "username is empty")
|
||||
ErrEmailIsEmpty = NewNormalError(NORMAL_SUBCATEGORY_USER, 2, http.StatusBadRequest, "email is empty")
|
||||
ErrPasswordIsEmpty = NewNormalError(NORMAL_SUBCATEGORY_USER, 3, http.StatusBadRequest, "password is empty")
|
||||
ErrUserNotFound = NewNormalError(NORMAL_SUBCATEGORY_USER, 4, http.StatusBadRequest, "user not found")
|
||||
ErrUserPasswordWrong = NewNormalError(NORMAL_SUBCATEGORY_USER, 5, http.StatusBadRequest, "password is wrong")
|
||||
ErrUsernameAlreadyExists = NewNormalError(NORMAL_SUBCATEGORY_USER, 6, http.StatusBadRequest, "username already exists")
|
||||
ErrUserEmailAlreadyExists = NewNormalError(NORMAL_SUBCATEGORY_USER, 7, http.StatusBadRequest, "email already exists")
|
||||
ErrLoginNameOrPasswordInvalid = NewNormalError(NORMAL_SUBCATEGORY_USER, 8, http.StatusUnauthorized, "login name or password is invalid")
|
||||
ErrLoginNameOrPasswordWrong = NewNormalError(NORMAL_SUBCATEGORY_USER, 9, http.StatusUnauthorized, "login name or password is wrong")
|
||||
)
|
||||
Reference in New Issue
Block a user