package response // 统一响应结构 type Response struct { Code int `json:"code"` // 0=成功,非0=失败 Msg string `json:"msg,omitempty"` // 提示信息 Data interface{} `json:"data,omitempty"` // 具体数据 Error string `json:"error,omitempty"` // 错误详情 } // 错误码定义(结合你的业务场景) const ( CodeOK = 0 CodeParamError = 1001 // 参数错误 CodeNotFound = 1002 // 资源未找到 CodeDBError = 1003 // 数据库操作失败 // 更精细的数据库错误码(用于区分连接/查询/元数据/事务等) CodeDBConnError = 1101 // 数据库连接失败 CodeDBQueryError = 1102 // 数据库查询/执行失败 CodeDBMetaError = 1103 // 数据库元数据查询失败 CodeAuthError = 1004 // 鉴权失败 CodePermission = 1005 // 权限不足 CodeConflict = 1006 // 资源冲突 CodeTimeout = 1007 // 超时 CodeInternalError = 1099 // 服务器内部错误 // 连接池相关 CodeConnPoolFull = 2001 // 连接池已满 CodeConnPoolClosed = 2002 // 连接池已关闭 CodeConnNotExist = 2003 // 连接不存在 CodeConnAlreadyExist = 2004 // 连接已存在 // 存储相关 CodeGroupNotExist = 3001 // 分组不存在 CodeGroupExist = 3002 // 分组已存在 CodeSaveFailed = 3003 // 保存失败 CodeLoadFailed = 3004 // 加载失败 CodeDeleteFailed = 3005 // 删除失败 // 脚本 (Script) 相关 CodeScriptNotExist = 4001 // 脚本不存在 CodeScriptExist = 4002 // 脚本已存在 CodeScriptSaveErr = 4003 // 脚本保存失败 CodeScriptLoadErr = 4004 // 脚本加载失败 CodeScriptDeleteErr = 4005 // 脚本删除失败 // 其他可扩展 ) // AppError 支持自定义错误 type AppError struct { Code int Msg string } func (e *AppError) Error() string { return e.Msg } func NewAppError(code int, msg string) *AppError { return &AppError{Code: code, Msg: msg} } // 常用错误变量(可按需扩展) var ( ErrParam = NewAppError(CodeParamError, "参数错误") ErrNotFound = NewAppError(CodeNotFound, "资源未找到") ErrDB = NewAppError(CodeDBError, "数据库操作失败") // 细化数据库错误 ErrDBConn = NewAppError(CodeDBConnError, "数据库连接失败") ErrDBQuery = NewAppError(CodeDBQueryError, "数据库查询失败") ErrDBMeta = NewAppError(CodeDBMetaError, "数据库元数据查询失败") ErrAuth = NewAppError(CodeAuthError, "鉴权失败") ErrPermission = NewAppError(CodePermission, "权限不足") ErrConflict = NewAppError(CodeConflict, "资源冲突") ErrTimeout = NewAppError(CodeTimeout, "请求超时") ErrInternal = NewAppError(CodeInternalError, "服务器内部错误") // 连接池相关 ErrConnPoolFull = NewAppError(CodeConnPoolFull, "连接池已满") ErrConnPoolClosed = NewAppError(CodeConnPoolClosed, "连接池已关闭") ErrConnNotExist = NewAppError(CodeConnNotExist, "连接不存在") ErrConnAlreadyExist = NewAppError(CodeConnAlreadyExist, "连接已存在") // 存储相关 ErrGroupNotExist = NewAppError(CodeGroupNotExist, "分组不存在") ErrGroupExist = NewAppError(CodeGroupExist, "分组已存在") ErrSaveFailed = NewAppError(CodeSaveFailed, "保存失败") ErrLoadFailed = NewAppError(CodeLoadFailed, "加载失败") ErrDeleteFailed = NewAppError(CodeDeleteFailed, "删除失败") // 脚本 (Script) 相关 ErrScriptNotExist = NewAppError(CodeScriptNotExist, "脚本不存在") ErrScriptExist = NewAppError(CodeScriptExist, "脚本已存在") ErrScriptSaveErr = NewAppError(CodeScriptSaveErr, "脚本保存失败") ErrScriptLoadErr = NewAppError(CodeScriptLoadErr, "脚本加载失败") ErrScriptDeleteErr = NewAppError(CodeScriptDeleteErr, "脚本删除失败") ) // 响应辅助函数 func Success(data interface{}) Response { return Response{Code: CodeOK, Msg: "操作成功", Data: data} } func SuccessWithMsg(msg string, data interface{}) Response { return Response{Code: CodeOK, Msg: msg, Data: data} } func Error(msg string) Response { return Response{Code: CodeInternalError, Msg: msg} } func ErrorWithDetail(msg string, err error) Response { errorDetail := "" if err != nil { errorDetail = err.Error() } return Response{Code: CodeInternalError, Msg: msg, Error: errorDetail} } func ErrorWithCode(code int, msg string) Response { return Response{Code: code, Msg: msg} } func ErrorWithCodeAndDetail(code int, msg string, err error) Response { errorDetail := "" if err != nil { errorDetail = err.Error() } return Response{Code: code, Msg: msg, Error: errorDetail} } func ParamError(msg string) Response { return Response{Code: CodeParamError, Msg: msg} } func ParamErrorWithDetail(msg string, err error) Response { errorDetail := "" if err != nil { errorDetail = err.Error() } return Response{Code: CodeParamError, Msg: msg, Error: errorDetail} } func NotFound(msg string) Response { return Response{Code: CodeNotFound, Msg: msg} } func NotFoundWithDetail(msg string, err error) Response { errorDetail := "" if err != nil { errorDetail = err.Error() } return Response{Code: CodeNotFound, Msg: msg, Error: errorDetail} } func DBError(msg string) Response { return Response{Code: CodeDBError, Msg: msg} } func DBErrorWithDetail(msg string, err error) Response { errorDetail := "" if err != nil { errorDetail = err.Error() } return Response{Code: CodeDBError, Msg: msg, Error: errorDetail} } func GroupNotExistError(msg string) Response { return Response{Code: CodeGroupNotExist, Msg: msg} } func GroupNotExistErrorWithDetail(msg string, err error) Response { errorDetail := "" if err != nil { errorDetail = err.Error() } return Response{Code: CodeGroupNotExist, Msg: msg, Error: errorDetail} }