add comments

This commit is contained in:
MaysWind
2020-12-22 23:39:36 +08:00
parent e66f0c7f48
commit bb10498893
12 changed files with 41 additions and 0 deletions
+1
View File
@@ -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")
+1
View File
@@ -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")
+18
View File
@@ -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
+13
View File
@@ -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)
}
+1
View File
@@ -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")
+1
View File
@@ -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")
+1
View File
@@ -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")
+1
View File
@@ -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")
+1
View File
@@ -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")
+1
View File
@@ -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")
+1
View File
@@ -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")
+1
View File
@@ -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")