feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
This commit is contained in:
48
pkg/errors/errors.go
Normal file
48
pkg/errors/errors.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package errors
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var (
|
||||
// 用户相关错误
|
||||
ErrUserNotFound = errors.New("用户不存在")
|
||||
ErrUsernameExists = errors.New("用户名已存在")
|
||||
ErrEmailExists = errors.New("邮箱已存在")
|
||||
ErrPhoneExists = errors.New("手机号已存在")
|
||||
ErrInvalidCredentials = errors.New("用户名或密码错误")
|
||||
ErrAccountLocked = errors.New("账号已被锁定")
|
||||
ErrAccountDisabled = errors.New("账号已被禁用")
|
||||
ErrInvalidOldPassword = errors.New("原密码错误")
|
||||
|
||||
// 角色相关错误
|
||||
ErrRoleNotFound = errors.New("角色不存在")
|
||||
ErrRoleCodeExists = errors.New("角色代码已存在")
|
||||
ErrCannotModifySystemRole = errors.New("不能修改系统角色")
|
||||
ErrCannotDeleteSystemRole = errors.New("不能删除系统角色")
|
||||
ErrRoleInUse = errors.New("角色正在使用中")
|
||||
|
||||
// 权限相关错误
|
||||
ErrPermissionNotFound = errors.New("权限不存在")
|
||||
ErrPermissionCodeExists = errors.New("权限代码已存在")
|
||||
|
||||
// 通用错误
|
||||
ErrInvalidParams = errors.New("参数错误")
|
||||
ErrUnauthorized = errors.New("未授权")
|
||||
ErrForbidden = errors.New("无权限")
|
||||
ErrInternalServerError = errors.New("服务器内部错误")
|
||||
)
|
||||
|
||||
// NewError 创建新错误
|
||||
func NewError(msg string) error {
|
||||
return errors.New(msg)
|
||||
}
|
||||
|
||||
// WrapError 包装错误
|
||||
func WrapError(err error, msg string) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("%s: %w", msg, err)
|
||||
}
|
||||
50
pkg/response/response.go
Normal file
50
pkg/response/response.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// Response 统一响应结构
|
||||
type Response struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
// Success 成功响应
|
||||
func Success(c *gin.Context, data interface{}) {
|
||||
c.JSON(http.StatusOK, Response{
|
||||
Code: 0,
|
||||
Message: "success",
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
|
||||
// Error 错误响应
|
||||
func Error(c *gin.Context, httpStatus int, message string, err error) {
|
||||
if err != nil {
|
||||
// 在开发环境下返回详细错误信息
|
||||
if gin.Mode() == gin.DebugMode {
|
||||
c.JSON(httpStatus, Response{
|
||||
Code: httpStatus,
|
||||
Message: message,
|
||||
Data: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
c.JSON(httpStatus, Response{
|
||||
Code: httpStatus,
|
||||
Message: message,
|
||||
})
|
||||
}
|
||||
|
||||
// ErrorWithCode 错误响应(带自定义错误码)
|
||||
func ErrorWithCode(c *gin.Context, code int, message string) {
|
||||
c.JSON(http.StatusOK, Response{
|
||||
Code: code,
|
||||
Message: message,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user