feat: 新增项目公共工具包,包含日志、错误、数据库、缓存、公共proto

This commit is contained in:
fish
2026-03-28 18:14:28 +08:00
parent 27da6939f4
commit 2f55987222
6 changed files with 211 additions and 0 deletions

View 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)
}