初始化项目

This commit is contained in:
lianghuanjie
2024-12-05 20:51:35 +08:00
commit e2ba6924b8
30 changed files with 1560 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
package errs
import (
"fmt"
"github.com/spf13/cast"
)
type err struct {
code int
reason Reason
msg string
}
func New(code int, reason Reason, message any) error {
return err{
code: code,
reason: reason,
msg: cast.ToString(message),
}
}
// Error error
func (e err) Error() string {
return fmt.Sprintf("code=%d msg=%s", e.code, e.msg)
}
// Code return code
func (e err) Code() int {
return e.code
}
// Reason return reason
func (e err) Reason() Reason {
return e.reason
}
// Message return message
func (e err) Message() string {
return e.msg
}

View File

@@ -0,0 +1,56 @@
package errs
import (
"net/http"
)
func Success() error {
return New(http.StatusOK, ErrSucceed, "success")
}
func NotFound(reason Reason, v any) error {
return New(http.StatusNotFound, reason, v)
}
func BadRequest(reason Reason, v any) error {
return New(http.StatusBadRequest, reason, v)
}
func InternalServer(reason Reason, v any) error {
return New(http.StatusInternalServerError, reason, v)
}
// Unauthorized new Unauthorized error that is mapped to a 401 response.
func Unauthorized(reason Reason, v any) error {
return New(http.StatusUnauthorized, reason, v)
}
// Forbidden new Forbidden error that is mapped to a 403 response.
func Forbidden(reason Reason, v any) error {
return New(http.StatusForbidden, reason, v)
}
// Conflict new Conflict error that is mapped to a 409 response.
func Conflict(reason Reason, v any) error {
return New(http.StatusConflict, reason, v)
}
// ServiceUnavailable new ServiceUnavailable error that is mapped to an HTTP 503 response.
func ServiceUnavailable(reason Reason, v any) error {
return New(http.StatusServiceUnavailable, reason, v)
}
// GatewayTimeout new GatewayTimeout error that is mapped to an HTTP 504 response.
func GatewayTimeout(reason Reason, v any) error {
return New(http.StatusGatewayTimeout, reason, v)
}
// BadGateway new BadGateway error that is mapped to an HTTP 504 response.
func BadGateway(reason Reason, v any) error {
return New(http.StatusBadGateway, reason, v)
}
// ClientClosed new ClientClosed error that is mapped to an HTTP 499 response.
func ClientClosed(reason Reason, v any) error {
return New(499, reason, v)
}

View File

@@ -0,0 +1,64 @@
package errs
import (
"context"
"github.com/zeromicro/go-zero/rest/httpx"
"net/http"
"os"
"strings"
)
var debug bool
func init() {
if strings.ToLower(os.Getenv("VANS_API_DEBUG")) == "on" {
debug = true
}
httpx.SetErrorHandlerCtx(ErrorHandleCtx)
httpx.SetErrorHandler(ErrorHandle)
}
func SetDebug(d bool) {
debug = d
}
func ErrorHandle(err error) (int, any) {
return ErrorHandleCtx(context.Background(), err)
}
func ErrorHandleCtx(ctx context.Context, err error) (int, any) {
code := http.StatusBadRequest
reason := ErrInternalServer
var msg string
if ec, ok := err.(interface{ Code() int }); ok {
code = ec.Code()
}
if ec, ok := err.(interface{ Message() string }); ok {
msg = ec.Message()
} else {
msg = err.Error()
}
if ec, ok := err.(interface{ Reason() Reason }); ok {
reason = ec.Reason()
}
var errMsg string
if reason < ErrUnknownLogicError && reason != ErrSucceed {
errMsg = msg
msg = "system error"
}
body := map[string]any{
"code": reason,
"message": msg,
}
if errMsg != "" && debug {
body["err"] = errMsg
}
if ec, ok := err.(interface{ Metadata() any }); ok {
if md := ec.Metadata(); md != nil {
body["metadata"] = md
}
}
return code, body
}

View File

@@ -0,0 +1,33 @@
package errs
type Reason int
const (
// ======= 系统错误0~999 =======
ErrUnknownReason Reason = 0 // 未知错误
ErrSucceed Reason = 200 // 成功
ErrOverload Reason = 403 // 请求超载
// ======= 服务器内部错误1000~9999 =======
ErrInternalServer Reason = 1000 // 未知的服务器内部错误
ErrDatabaseOperate Reason = 1001 // 数据库错误
ErrRedisOperate Reason = 1002 // redis错误
ErrEncodePassword Reason = 1003 // 密码加密错误
ErrGenerateUUid Reason = 1004 // 生成uuid错误
ErrGenerateToken Reason = 1005 // 生成token错误
ErrGetExchangeRate Reason = 1005 // 获取汇率错误
// ======= 业务层错误10000~99999 =======
ErrUnknownLogicError Reason = 10000 // 未知的业务错误
ErrInvalidParam Reason = 10001 // 无效参数错误
ErrInvalidSignature Reason = 10002 // 无效签名错误
ErrInvalidToken Reason = 10003 // 无效的token
ErrInvoiceHasPaid Reason = 10004 // 订单已支付
ErrInvalidAppId Reason = 10005 // 应用id无效
ErrUserNotHasInviter Reason = 10006 // 用户没有邀请人
// ========= admin 业务相关错误码: 30000~39999 =========
ErrUnknownAdminError Reason = 30000 // 未知的admin错误
ErrInvalidPassword Reason = 30001 // 无效密码
ErrInvalidAccount Reason = 30002 // 无效账号
)