feat: 新增项目公共工具包,包含日志、错误、数据库、缓存、公共proto
This commit is contained in:
46
backend/shared/errors/errors.go
Normal file
46
backend/shared/errors/errors.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package errors
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type AppError struct {
|
||||
Code string
|
||||
Message string
|
||||
Err error
|
||||
}
|
||||
|
||||
func (e *AppError) Error() string {
|
||||
if e.Err != nil {
|
||||
return fmt.Sprintf("%s: %s (code: %s)", e.Message, e.Err.Error(), e.Code)
|
||||
}
|
||||
return fmt.Sprintf("%s (code: %s)", e.Message, e.Code)
|
||||
}
|
||||
|
||||
func (e *AppError) Unwrap() error {
|
||||
return e.Err
|
||||
}
|
||||
|
||||
func NewAppError(code, message string, err error) *AppError {
|
||||
return &AppError{
|
||||
Code: code,
|
||||
Message: message,
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
|
||||
func NewBadRequestError(message string, err error) *AppError {
|
||||
return NewAppError("BAD_REQUEST", message, err)
|
||||
}
|
||||
|
||||
func NewInternalError(message string, err error) *AppError {
|
||||
return NewAppError("INTERNAL_ERROR", message, err)
|
||||
}
|
||||
|
||||
func NewNotFoundError(message string, err error) *AppError {
|
||||
return NewAppError("NOT_FOUND", message, err)
|
||||
}
|
||||
|
||||
func NewUnauthorizedError(message string, err error) *AppError {
|
||||
return NewAppError("UNAUTHORIZED", message, err)
|
||||
}
|
||||
Reference in New Issue
Block a user