diff --git a/pkg/errs/account.go b/pkg/errs/account.go index bb8131a4..4f65a4fb 100644 --- a/pkg/errs/account.go +++ b/pkg/errs/account.go @@ -2,6 +2,7 @@ package errs import "net/http" +// Error codes related to accounts var ( ErrAccountIdInvalid = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 0, http.StatusBadRequest, "account id is invalid") ErrAccountNotFound = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 1, http.StatusBadRequest, "account not found") diff --git a/pkg/errs/database.go b/pkg/errs/database.go index 170257a2..4705046a 100644 --- a/pkg/errs/database.go +++ b/pkg/errs/database.go @@ -4,6 +4,7 @@ import ( "net/http" ) +// Error codes related to database 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") diff --git a/pkg/errs/error.go b/pkg/errs/error.go index 63742895..569c1ec9 100644 --- a/pkg/errs/error.go +++ b/pkg/errs/error.go @@ -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 diff --git a/pkg/errs/global.go b/pkg/errs/global.go index 28b69d53..ea538bf9 100644 --- a/pkg/errs/global.go +++ b/pkg/errs/global.go @@ -5,6 +5,7 @@ import ( "net/http" ) +// General error codes var ( ErrIncompleteOrIncorrectSubmission = NewNormalError(NORMAL_SUBCATEGORY_GLOBAL, 0, http.StatusBadRequest, "incomplete or incorrect submission") 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") ) +// GetParameterInvalidMessage returns specific error message for invalid parameter error func GetParameterInvalidMessage(field string) string { return fmt.Sprintf("parameter \"%s\" is invalid", field) } +// GetParameterIsRequiredMessage returns specific error message for missing parameter error func GetParameterIsRequiredMessage(field string) string { 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 { 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 { 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 { 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 { 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 { 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 { return fmt.Sprintf("parameter \"%s\" cannot be blank", field) } +// GetParameterInvalidUsernameMessage returns specific error message for invalid username parameter error func GetParameterInvalidUsernameMessage(field string) string { 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 { 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 { 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 { return fmt.Sprintf("parameter \"%s\" is invalid color", field) } diff --git a/pkg/errs/setting.go b/pkg/errs/setting.go index 17d3d876..f047498b 100644 --- a/pkg/errs/setting.go +++ b/pkg/errs/setting.go @@ -2,6 +2,7 @@ package errs import "net/http" +// Error codes related to settings var ( ErrInvalidProtocol = NewSystemError(SYSTEM_SUBCATEGORY_SETTING, 0, http.StatusInternalServerError, "invalid server protocol") ErrInvalidLogMode = NewSystemError(SYSTEM_SUBCATEGORY_SETTING, 1, http.StatusInternalServerError, "invalid log mode") diff --git a/pkg/errs/system.go b/pkg/errs/system.go index 2b4fdccd..c575863b 100644 --- a/pkg/errs/system.go +++ b/pkg/errs/system.go @@ -2,6 +2,7 @@ package errs import "net/http" +// Error codes related to transaction categories var ( ErrSystemError = NewSystemError(SYSTEM_SUBCATEGORY_DEFAULT, 0, http.StatusInternalServerError, "system error") ErrApiNotFound = NewSystemError(SYSTEM_SUBCATEGORY_DEFAULT, 1, http.StatusNotFound, "api not found") diff --git a/pkg/errs/token.go b/pkg/errs/token.go index 50f1d0ee..2a4e18ac 100644 --- a/pkg/errs/token.go +++ b/pkg/errs/token.go @@ -4,6 +4,7 @@ import ( "net/http" ) +// Error codes related to tokens var ( ErrTokenGenerating = NewNormalError(NORMAL_SUBCATEGORY_TOKEN, 0, http.StatusInternalServerError, "failed to generate token") ErrUnauthorizedAccess = NewNormalError(NORMAL_SUBCATEGORY_TOKEN, 1, http.StatusUnauthorized, "unauthorized access") diff --git a/pkg/errs/transaction.go b/pkg/errs/transaction.go index c39333b0..d9d18338 100644 --- a/pkg/errs/transaction.go +++ b/pkg/errs/transaction.go @@ -2,6 +2,7 @@ package errs import "net/http" +// Error codes related to transaction var ( ErrTransactionIdInvalid = NewNormalError(NORMAL_SUBCATEGORY_TRANSACTION, 0, http.StatusBadRequest, "transaction id is invalid") ErrTransactionNotFound = NewNormalError(NORMAL_SUBCATEGORY_TRANSACTION, 1, http.StatusBadRequest, "transaction not found") diff --git a/pkg/errs/transaction_category.go b/pkg/errs/transaction_category.go index 3f6293b4..147515b0 100644 --- a/pkg/errs/transaction_category.go +++ b/pkg/errs/transaction_category.go @@ -2,6 +2,7 @@ package errs import "net/http" +// Error codes related to transaction categories var ( 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") diff --git a/pkg/errs/transaction_tag.go b/pkg/errs/transaction_tag.go index ae3f615c..47460fc2 100644 --- a/pkg/errs/transaction_tag.go +++ b/pkg/errs/transaction_tag.go @@ -2,6 +2,7 @@ package errs import "net/http" +// Error codes related to transaction tags var ( 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") diff --git a/pkg/errs/twofactor_authorization.go b/pkg/errs/twofactor_authorization.go index f4b4ff46..c49197a7 100644 --- a/pkg/errs/twofactor_authorization.go +++ b/pkg/errs/twofactor_authorization.go @@ -2,6 +2,7 @@ package errs import "net/http" +// Error codes related to two factor authorization var ( 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") diff --git a/pkg/errs/user.go b/pkg/errs/user.go index d82cd7a9..2c6f3b03 100644 --- a/pkg/errs/user.go +++ b/pkg/errs/user.go @@ -4,6 +4,7 @@ import ( "net/http" ) +// Error codes related to users var ( 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")