mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-18 08:44:25 +08:00
add comments
This commit is contained in:
@@ -2,6 +2,7 @@ package errs
|
|||||||
|
|
||||||
import "net/http"
|
import "net/http"
|
||||||
|
|
||||||
|
// Error codes related to accounts
|
||||||
var (
|
var (
|
||||||
ErrAccountIdInvalid = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 0, http.StatusBadRequest, "account id is invalid")
|
ErrAccountIdInvalid = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 0, http.StatusBadRequest, "account id is invalid")
|
||||||
ErrAccountNotFound = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 1, http.StatusBadRequest, "account not found")
|
ErrAccountNotFound = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 1, http.StatusBadRequest, "account not found")
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Error codes related to database
|
||||||
var (
|
var (
|
||||||
ErrDatabaseTypeInvalid = NewSystemError(SYSTEM_SUBCATEGORY_DATABASE, 0, http.StatusInternalServerError, "database type is invalid")
|
ErrDatabaseTypeInvalid = NewSystemError(SYSTEM_SUBCATEGORY_DATABASE, 0, http.StatusInternalServerError, "database type is invalid")
|
||||||
ErrDatabaseHostInvalid = NewSystemError(SYSTEM_SUBCATEGORY_DATABASE, 1, http.StatusInternalServerError, "database host is invalid")
|
ErrDatabaseHostInvalid = NewSystemError(SYSTEM_SUBCATEGORY_DATABASE, 1, http.StatusInternalServerError, "database host is invalid")
|
||||||
|
|||||||
@@ -1,15 +1,23 @@
|
|||||||
package errs
|
package errs
|
||||||
|
|
||||||
|
// ErrorCategory represents error category
|
||||||
type ErrorCategory int
|
type ErrorCategory int
|
||||||
|
|
||||||
|
// Error categories
|
||||||
const (
|
const (
|
||||||
CATEGORY_SYSTEM ErrorCategory = 1
|
CATEGORY_SYSTEM ErrorCategory = 1
|
||||||
CATEGORY_NORMAL ErrorCategory = 2
|
CATEGORY_NORMAL ErrorCategory = 2
|
||||||
|
)
|
||||||
|
|
||||||
|
// Sub categories of system error
|
||||||
|
const (
|
||||||
SYSTEM_SUBCATEGORY_DEFAULT = 0
|
SYSTEM_SUBCATEGORY_DEFAULT = 0
|
||||||
SYSTEM_SUBCATEGORY_SETTING = 1
|
SYSTEM_SUBCATEGORY_SETTING = 1
|
||||||
SYSTEM_SUBCATEGORY_DATABASE = 2
|
SYSTEM_SUBCATEGORY_DATABASE = 2
|
||||||
|
)
|
||||||
|
|
||||||
|
// Sub categories of normal error
|
||||||
|
const (
|
||||||
NORMAL_SUBCATEGORY_GLOBAL = 0
|
NORMAL_SUBCATEGORY_GLOBAL = 0
|
||||||
NORMAL_SUBCATEGORY_USER = 1
|
NORMAL_SUBCATEGORY_USER = 1
|
||||||
NORMAL_SUBCATEGORY_TOKEN = 2
|
NORMAL_SUBCATEGORY_TOKEN = 2
|
||||||
@@ -20,6 +28,7 @@ const (
|
|||||||
NORMAL_SUBCATEGORY_TAG = 7
|
NORMAL_SUBCATEGORY_TAG = 7
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Error represents the specific error returned to user
|
||||||
type Error struct {
|
type Error struct {
|
||||||
Category ErrorCategory
|
Category ErrorCategory
|
||||||
SubCategory int
|
SubCategory int
|
||||||
@@ -29,14 +38,17 @@ type Error struct {
|
|||||||
BaseError []error
|
BaseError []error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Error returns the error message
|
||||||
func (err *Error) Error() string {
|
func (err *Error) Error() string {
|
||||||
return err.Message
|
return err.Message
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Code returns the error code
|
||||||
func (err *Error) Code() int {
|
func (err *Error) Code() int {
|
||||||
return int(err.Category)*100000 + err.SubCategory*1000 + err.Index
|
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 {
|
func New(category ErrorCategory, subCategory int, index int, httpStatusCode int, message string, baseError ...error) *Error {
|
||||||
return &Error{
|
return &Error{
|
||||||
Category: category,
|
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 {
|
func NewSystemError(subCategory int, index int, httpStatusCode int, message string) *Error {
|
||||||
return New(CATEGORY_SYSTEM, subCategory, index, httpStatusCode, message)
|
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 {
|
func NewNormalError(subCategory int, index int, httpStatusCode int, message string) *Error {
|
||||||
return New(CATEGORY_NORMAL, subCategory, index, httpStatusCode, message)
|
return New(CATEGORY_NORMAL, subCategory, index, httpStatusCode, message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewIncompleteOrIncorrectSubmissionError returns a new incomplete or incorrect submission error instance
|
||||||
func NewIncompleteOrIncorrectSubmissionError(err error) *Error {
|
func NewIncompleteOrIncorrectSubmissionError(err error) *Error {
|
||||||
return New(ErrIncompleteOrIncorrectSubmission.Category,
|
return New(ErrIncompleteOrIncorrectSubmission.Category,
|
||||||
ErrIncompleteOrIncorrectSubmission.SubCategory,
|
ErrIncompleteOrIncorrectSubmission.SubCategory,
|
||||||
@@ -64,6 +79,8 @@ func NewIncompleteOrIncorrectSubmissionError(err error) *Error {
|
|||||||
ErrIncompleteOrIncorrectSubmission.Message, err)
|
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 {
|
func Or(err error, defaultErr *Error) *Error {
|
||||||
if finalError, ok := err.(*Error); ok {
|
if finalError, ok := err.(*Error); ok {
|
||||||
return finalError
|
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 {
|
func IsCustomError(err error) bool {
|
||||||
_, ok := err.(*Error)
|
_, ok := err.(*Error)
|
||||||
return ok
|
return ok
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// General error codes
|
||||||
var (
|
var (
|
||||||
ErrIncompleteOrIncorrectSubmission = NewNormalError(NORMAL_SUBCATEGORY_GLOBAL, 0, http.StatusBadRequest, "incomplete or incorrect submission")
|
ErrIncompleteOrIncorrectSubmission = NewNormalError(NORMAL_SUBCATEGORY_GLOBAL, 0, http.StatusBadRequest, "incomplete or incorrect submission")
|
||||||
ErrOperationFailed = NewNormalError(NORMAL_SUBCATEGORY_GLOBAL, 1, http.StatusInternalServerError, "operation failed")
|
ErrOperationFailed = NewNormalError(NORMAL_SUBCATEGORY_GLOBAL, 1, http.StatusInternalServerError, "operation failed")
|
||||||
@@ -16,50 +17,62 @@ var (
|
|||||||
ErrPageCountInvalid = NewNormalError(NORMAL_SUBCATEGORY_GLOBAL, 7, http.StatusBadRequest, "page count is invalid")
|
ErrPageCountInvalid = NewNormalError(NORMAL_SUBCATEGORY_GLOBAL, 7, http.StatusBadRequest, "page count is invalid")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// GetParameterInvalidMessage returns specific error message for invalid parameter error
|
||||||
func GetParameterInvalidMessage(field string) string {
|
func GetParameterInvalidMessage(field string) string {
|
||||||
return fmt.Sprintf("parameter \"%s\" is invalid", field)
|
return fmt.Sprintf("parameter \"%s\" is invalid", field)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetParameterIsRequiredMessage returns specific error message for missing parameter error
|
||||||
func GetParameterIsRequiredMessage(field string) string {
|
func GetParameterIsRequiredMessage(field string) string {
|
||||||
return fmt.Sprintf("parameter \"%s\" is required", field)
|
return fmt.Sprintf("parameter \"%s\" is required", field)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetParameterMustLessThanMessage returns specific error message for parameter too large error
|
||||||
func GetParameterMustLessThanMessage(field string, param string) string {
|
func GetParameterMustLessThanMessage(field string, param string) string {
|
||||||
return fmt.Sprintf("parameter \"%s\" must be less than %s", field, param)
|
return fmt.Sprintf("parameter \"%s\" must be less than %s", field, param)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetParameterMustLessThanCharsMessage returns specific error message for parameter too long error
|
||||||
func GetParameterMustLessThanCharsMessage(field string, param string) string {
|
func GetParameterMustLessThanCharsMessage(field string, param string) string {
|
||||||
return fmt.Sprintf("parameter \"%s\" must be less than %s characters", field, param)
|
return fmt.Sprintf("parameter \"%s\" must be less than %s characters", field, param)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetParameterMustMoreThanMessage returns specific error message for parameter too small error
|
||||||
func GetParameterMustMoreThanMessage(field string, param string) string {
|
func GetParameterMustMoreThanMessage(field string, param string) string {
|
||||||
return fmt.Sprintf("parameter \"%s\" must be more than %s", field, param)
|
return fmt.Sprintf("parameter \"%s\" must be more than %s", field, param)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetParameterMustMoreThanCharsMessage returns specific error message for parameter too short error
|
||||||
func GetParameterMustMoreThanCharsMessage(field string, param string) string {
|
func GetParameterMustMoreThanCharsMessage(field string, param string) string {
|
||||||
return fmt.Sprintf("parameter \"%s\" must be more than %s characters", field, param)
|
return fmt.Sprintf("parameter \"%s\" must be more than %s characters", field, param)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetParameterLengthNotEqualMessage returns specific error message for parameter length wrong error
|
||||||
func GetParameterLengthNotEqualMessage(field string, param string) string {
|
func GetParameterLengthNotEqualMessage(field string, param string) string {
|
||||||
return fmt.Sprintf("parameter \"%s\" length is not equal to %s", field, param)
|
return fmt.Sprintf("parameter \"%s\" length is not equal to %s", field, param)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetParameterNotBeBlankMessage returns specific error message for blank parameter error
|
||||||
func GetParameterNotBeBlankMessage(field string) string {
|
func GetParameterNotBeBlankMessage(field string) string {
|
||||||
return fmt.Sprintf("parameter \"%s\" cannot be blank", field)
|
return fmt.Sprintf("parameter \"%s\" cannot be blank", field)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetParameterInvalidUsernameMessage returns specific error message for invalid username parameter error
|
||||||
func GetParameterInvalidUsernameMessage(field string) string {
|
func GetParameterInvalidUsernameMessage(field string) string {
|
||||||
return fmt.Sprintf("parameter \"%s\" is invalid username format", field)
|
return fmt.Sprintf("parameter \"%s\" is invalid username format", field)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetParameterInvalidEmailMessage returns specific error message for invalid email parameter error
|
||||||
func GetParameterInvalidEmailMessage(field string) string {
|
func GetParameterInvalidEmailMessage(field string) string {
|
||||||
return fmt.Sprintf("parameter \"%s\" is invalid email format", field)
|
return fmt.Sprintf("parameter \"%s\" is invalid email format", field)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetParameterInvalidCurrencyMessage returns specific error message for invalid currency parameter error
|
||||||
func GetParameterInvalidCurrencyMessage(field string) string {
|
func GetParameterInvalidCurrencyMessage(field string) string {
|
||||||
return fmt.Sprintf("parameter \"%s\" is invalid currency", field)
|
return fmt.Sprintf("parameter \"%s\" is invalid currency", field)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetParameterInvalidHexRGBColorMessage returns specific error message for invalid hex rgb color parameter error
|
||||||
func GetParameterInvalidHexRGBColorMessage(field string) string {
|
func GetParameterInvalidHexRGBColorMessage(field string) string {
|
||||||
return fmt.Sprintf("parameter \"%s\" is invalid color", field)
|
return fmt.Sprintf("parameter \"%s\" is invalid color", field)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package errs
|
|||||||
|
|
||||||
import "net/http"
|
import "net/http"
|
||||||
|
|
||||||
|
// Error codes related to settings
|
||||||
var (
|
var (
|
||||||
ErrInvalidProtocol = NewSystemError(SYSTEM_SUBCATEGORY_SETTING, 0, http.StatusInternalServerError, "invalid server protocol")
|
ErrInvalidProtocol = NewSystemError(SYSTEM_SUBCATEGORY_SETTING, 0, http.StatusInternalServerError, "invalid server protocol")
|
||||||
ErrInvalidLogMode = NewSystemError(SYSTEM_SUBCATEGORY_SETTING, 1, http.StatusInternalServerError, "invalid log mode")
|
ErrInvalidLogMode = NewSystemError(SYSTEM_SUBCATEGORY_SETTING, 1, http.StatusInternalServerError, "invalid log mode")
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package errs
|
|||||||
|
|
||||||
import "net/http"
|
import "net/http"
|
||||||
|
|
||||||
|
// Error codes related to transaction categories
|
||||||
var (
|
var (
|
||||||
ErrSystemError = NewSystemError(SYSTEM_SUBCATEGORY_DEFAULT, 0, http.StatusInternalServerError, "system error")
|
ErrSystemError = NewSystemError(SYSTEM_SUBCATEGORY_DEFAULT, 0, http.StatusInternalServerError, "system error")
|
||||||
ErrApiNotFound = NewSystemError(SYSTEM_SUBCATEGORY_DEFAULT, 1, http.StatusNotFound, "api not found")
|
ErrApiNotFound = NewSystemError(SYSTEM_SUBCATEGORY_DEFAULT, 1, http.StatusNotFound, "api not found")
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Error codes related to tokens
|
||||||
var (
|
var (
|
||||||
ErrTokenGenerating = NewNormalError(NORMAL_SUBCATEGORY_TOKEN, 0, http.StatusInternalServerError, "failed to generate token")
|
ErrTokenGenerating = NewNormalError(NORMAL_SUBCATEGORY_TOKEN, 0, http.StatusInternalServerError, "failed to generate token")
|
||||||
ErrUnauthorizedAccess = NewNormalError(NORMAL_SUBCATEGORY_TOKEN, 1, http.StatusUnauthorized, "unauthorized access")
|
ErrUnauthorizedAccess = NewNormalError(NORMAL_SUBCATEGORY_TOKEN, 1, http.StatusUnauthorized, "unauthorized access")
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package errs
|
|||||||
|
|
||||||
import "net/http"
|
import "net/http"
|
||||||
|
|
||||||
|
// Error codes related to transaction
|
||||||
var (
|
var (
|
||||||
ErrTransactionIdInvalid = NewNormalError(NORMAL_SUBCATEGORY_TRANSACTION, 0, http.StatusBadRequest, "transaction id is invalid")
|
ErrTransactionIdInvalid = NewNormalError(NORMAL_SUBCATEGORY_TRANSACTION, 0, http.StatusBadRequest, "transaction id is invalid")
|
||||||
ErrTransactionNotFound = NewNormalError(NORMAL_SUBCATEGORY_TRANSACTION, 1, http.StatusBadRequest, "transaction not found")
|
ErrTransactionNotFound = NewNormalError(NORMAL_SUBCATEGORY_TRANSACTION, 1, http.StatusBadRequest, "transaction not found")
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package errs
|
|||||||
|
|
||||||
import "net/http"
|
import "net/http"
|
||||||
|
|
||||||
|
// Error codes related to transaction categories
|
||||||
var (
|
var (
|
||||||
ErrTransactionCategoryIdInvalid = NewNormalError(NORMAL_SUBCATEGORY_CATEGORY, 0, http.StatusBadRequest, "transaction category id is invalid")
|
ErrTransactionCategoryIdInvalid = NewNormalError(NORMAL_SUBCATEGORY_CATEGORY, 0, http.StatusBadRequest, "transaction category id is invalid")
|
||||||
ErrTransactionCategoryNotFound = NewNormalError(NORMAL_SUBCATEGORY_CATEGORY, 1, http.StatusBadRequest, "transaction category not found")
|
ErrTransactionCategoryNotFound = NewNormalError(NORMAL_SUBCATEGORY_CATEGORY, 1, http.StatusBadRequest, "transaction category not found")
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package errs
|
|||||||
|
|
||||||
import "net/http"
|
import "net/http"
|
||||||
|
|
||||||
|
// Error codes related to transaction tags
|
||||||
var (
|
var (
|
||||||
ErrTransactionTagIdInvalid = NewNormalError(NORMAL_SUBCATEGORY_TAG, 0, http.StatusBadRequest, "transaction tag id is invalid")
|
ErrTransactionTagIdInvalid = NewNormalError(NORMAL_SUBCATEGORY_TAG, 0, http.StatusBadRequest, "transaction tag id is invalid")
|
||||||
ErrTransactionTagNotFound = NewNormalError(NORMAL_SUBCATEGORY_TAG, 1, http.StatusBadRequest, "transaction tag not found")
|
ErrTransactionTagNotFound = NewNormalError(NORMAL_SUBCATEGORY_TAG, 1, http.StatusBadRequest, "transaction tag not found")
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package errs
|
|||||||
|
|
||||||
import "net/http"
|
import "net/http"
|
||||||
|
|
||||||
|
// Error codes related to two factor authorization
|
||||||
var (
|
var (
|
||||||
ErrPasscodeInvalid = NewNormalError(NORMAL_SUBCATEGORY_TWOFACTOR, 0, http.StatusUnauthorized, "passcode is invalid")
|
ErrPasscodeInvalid = NewNormalError(NORMAL_SUBCATEGORY_TWOFACTOR, 0, http.StatusUnauthorized, "passcode is invalid")
|
||||||
ErrTwoFactorRecoveryCodeInvalid = NewNormalError(NORMAL_SUBCATEGORY_TWOFACTOR, 1, http.StatusUnauthorized, "two factor backup code is invalid")
|
ErrTwoFactorRecoveryCodeInvalid = NewNormalError(NORMAL_SUBCATEGORY_TWOFACTOR, 1, http.StatusUnauthorized, "two factor backup code is invalid")
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Error codes related to users
|
||||||
var (
|
var (
|
||||||
ErrLoginNameInvalid = NewNormalError(NORMAL_SUBCATEGORY_USER, 0, http.StatusUnauthorized, "login name is invalid")
|
ErrLoginNameInvalid = NewNormalError(NORMAL_SUBCATEGORY_USER, 0, http.StatusUnauthorized, "login name is invalid")
|
||||||
ErrLoginNameOrPasswordInvalid = NewNormalError(NORMAL_SUBCATEGORY_USER, 1, http.StatusUnauthorized, "login name or password is invalid")
|
ErrLoginNameOrPasswordInvalid = NewNormalError(NORMAL_SUBCATEGORY_USER, 1, http.StatusUnauthorized, "login name or password is invalid")
|
||||||
|
|||||||
Reference in New Issue
Block a user