adjust errors and logger (#10)

This commit is contained in:
Sarv
2025-04-01 19:41:40 +08:00
committed by GitHub
parent f31953c42b
commit 167a9ca873
49 changed files with 553 additions and 981 deletions

View File

@@ -1,151 +0,0 @@
package errors
import (
"fmt"
"net/http"
)
// 微信相关错误
// WeChatProcessNotFound 创建微信进程未找到错误
func WeChatProcessNotFound() *AppError {
return New(ErrTypeWeChat, "wechat process not found", nil, http.StatusNotFound).WithStack()
}
// WeChatKeyExtractFailed 创建微信密钥提取失败错误
func WeChatKeyExtractFailed(cause error) *AppError {
return New(ErrTypeWeChat, "failed to extract wechat key", cause, http.StatusInternalServerError).WithStack()
}
// WeChatDecryptFailed 创建微信解密失败错误
func WeChatDecryptFailed(cause error) *AppError {
return New(ErrTypeWeChat, "failed to decrypt wechat database", cause, http.StatusInternalServerError).WithStack()
}
// WeChatAccountNotSelected 创建未选择微信账号错误
func WeChatAccountNotSelected() *AppError {
return New(ErrTypeWeChat, "no wechat account selected", nil, http.StatusBadRequest).WithStack()
}
// 数据库相关错误
// DBConnectionFailed 创建数据库连接失败错误
func DBConnectionFailed(cause error) *AppError {
return New(ErrTypeDatabase, "database connection failed", cause, http.StatusInternalServerError).WithStack()
}
// DBQueryFailed 创建数据库查询失败错误
func DBQueryFailed(operation string, cause error) *AppError {
return New(ErrTypeDatabase, fmt.Sprintf("database query failed: %s", operation), cause, http.StatusInternalServerError).WithStack()
}
// DBRecordNotFound 创建数据库记录未找到错误
func DBRecordNotFound(resource string) *AppError {
return New(ErrTypeNotFound, fmt.Sprintf("record not found: %s", resource), nil, http.StatusNotFound).WithStack()
}
// 配置相关错误
// ConfigInvalid 创建配置无效错误
func ConfigInvalid(field string, cause error) *AppError {
return New(ErrTypeConfig, fmt.Sprintf("invalid configuration: %s", field), cause, http.StatusInternalServerError).WithStack()
}
// ConfigMissing 创建配置缺失错误
func ConfigMissing(field string) *AppError {
return New(ErrTypeConfig, fmt.Sprintf("missing configuration: %s", field), nil, http.StatusBadRequest).WithStack()
}
// 平台相关错误
// PlatformUnsupported 创建不支持的平台错误
func PlatformUnsupported(platform string, version int) *AppError {
return New(ErrTypeInvalidArg, fmt.Sprintf("unsupported platform: %s v%d", platform, version), nil, http.StatusBadRequest).WithStack()
}
// 文件系统错误
// FileNotFound 创建文件未找到错误
func FileNotFound(path string) *AppError {
return New(ErrTypeNotFound, fmt.Sprintf("file not found: %s", path), nil, http.StatusNotFound).WithStack()
}
// FileReadFailed 创建文件读取失败错误
func FileReadFailed(path string, cause error) *AppError {
return New(ErrTypeInternal, fmt.Sprintf("failed to read file: %s", path), cause, http.StatusInternalServerError).WithStack()
}
// FileWriteFailed 创建文件写入失败错误
func FileWriteFailed(path string, cause error) *AppError {
return New(ErrTypeInternal, fmt.Sprintf("failed to write file: %s", path), cause, http.StatusInternalServerError).WithStack()
}
// 参数验证错误
// RequiredParam 创建必需参数缺失错误
func RequiredParam(param string) *AppError {
return New(ErrTypeInvalidArg, fmt.Sprintf("required parameter missing: %s", param), nil, http.StatusBadRequest).WithStack()
}
// InvalidParam 创建参数无效错误
func InvalidParam(param string, reason string) *AppError {
message := fmt.Sprintf("invalid parameter: %s", param)
if reason != "" {
message = fmt.Sprintf("%s (%s)", message, reason)
}
return New(ErrTypeInvalidArg, message, nil, http.StatusBadRequest).WithStack()
}
// 解密相关错误
// DecryptInvalidKey 创建无效密钥格式错误
func DecryptInvalidKey(cause error) *AppError {
return New(ErrTypeWeChat, "invalid key format", cause, http.StatusBadRequest).
WithStack()
}
// DecryptCreateCipherFailed 创建无法创建加密器错误
func DecryptCreateCipherFailed(cause error) *AppError {
return New(ErrTypeWeChat, "failed to create cipher", cause, http.StatusInternalServerError).
WithStack()
}
// DecryptDecodeKeyFailed 创建无法解码十六进制密钥错误
func DecryptDecodeKeyFailed(cause error) *AppError {
return New(ErrTypeWeChat, "failed to decode hex key", cause, http.StatusBadRequest).
WithStack()
}
// DecryptWriteOutputFailed 创建无法写入输出错误
func DecryptWriteOutputFailed(cause error) *AppError {
return New(ErrTypeWeChat, "failed to write decryption output", cause, http.StatusInternalServerError).
WithStack()
}
// DecryptOperationCanceled 创建解密操作被取消错误
func DecryptOperationCanceled() *AppError {
return New(ErrTypeWeChat, "decryption operation was canceled", nil, http.StatusBadRequest).
WithStack()
}
// DecryptOpenFileFailed 创建无法打开数据库文件错误
func DecryptOpenFileFailed(path string, cause error) *AppError {
return New(ErrTypeWeChat, fmt.Sprintf("failed to open database file: %s", path), cause, http.StatusInternalServerError).
WithStack()
}
// DecryptReadFileFailed 创建无法读取数据库文件错误
func DecryptReadFileFailed(path string, cause error) *AppError {
return New(ErrTypeWeChat, fmt.Sprintf("failed to read database file: %s", path), cause, http.StatusInternalServerError).
WithStack()
}
// DecryptIncompleteRead 创建不完整的头部读取错误
func DecryptIncompleteRead(cause error) *AppError {
return New(ErrTypeWeChat, "incomplete header read during decryption", cause, http.StatusInternalServerError).
WithStack()
}
var ErrAlreadyDecrypted = New(ErrTypeWeChat, "database file is already decrypted", nil, http.StatusBadRequest)
var ErrDecryptHashVerificationFailed = New(ErrTypeWeChat, "hash verification failed during decryption", nil, http.StatusBadRequest)
var ErrDecryptIncorrectKey = New(ErrTypeWeChat, "incorrect decryption key", nil, http.StatusBadRequest)

View File

@@ -10,51 +10,29 @@ import (
"github.com/gin-gonic/gin"
)
// 定义错误类型常量
const (
ErrTypeDatabase = "database"
ErrTypeWeChat = "wechat"
ErrTypeHTTP = "http"
ErrTypeConfig = "config"
ErrTypeInvalidArg = "invalid_argument"
ErrTypeAuth = "authentication"
ErrTypePermission = "permission"
ErrTypeNotFound = "not_found"
ErrTypeValidation = "validation"
ErrTypeRateLimit = "rate_limit"
ErrTypeInternal = "internal"
)
// AppError 表示应用程序错误
type AppError struct {
Type string `json:"type"` // 错误类型
Message string `json:"message"` // 错误消息
Cause error `json:"-"` // 原始错误
Code int `json:"-"` // HTTP Code
Stack []string `json:"-"` // 错误堆栈
RequestID string `json:"request_id,omitempty"` // 请求ID用于跟踪
type Error struct {
Message string `json:"message"` // 错误消息
Cause error `json:"-"` // 原始错误
Code int `json:"-"` // HTTP Code
Stack []string `json:"-"` // 错误堆栈
}
// Error 实现 error 接口
func (e *AppError) Error() string {
func (e *Error) Error() string {
if e.Cause != nil {
return fmt.Sprintf("%s: %s: %v", e.Type, e.Message, e.Cause)
return fmt.Sprintf("%s: %v", e.Message, e.Cause)
}
return fmt.Sprintf("%s: %s", e.Type, e.Message)
return fmt.Sprintf("%s", e.Message)
}
// String 返回错误的字符串表示
func (e *AppError) String() string {
func (e *Error) String() string {
return e.Error()
}
// Unwrap 实现 errors.Unwrap 接口,用于错误链
func (e *AppError) Unwrap() error {
func (e *Error) Unwrap() error {
return e.Cause
}
// WithStack 添加堆栈信息到错误
func (e *AppError) WithStack() *AppError {
func (e *Error) WithStack() *Error {
const depth = 32
var pcs [depth]uintptr
n := runtime.Callers(2, pcs[:])
@@ -75,32 +53,29 @@ func (e *AppError) WithStack() *AppError {
return e
}
// WithRequestID 添加请求ID到错误
func (e *AppError) WithRequestID(requestID string) *AppError {
e.RequestID = requestID
return e
}
// New 创建新的应用错误
func New(errType, message string, cause error, code int) *AppError {
return &AppError{
Type: errType,
func New(cause error, code int, message string) *Error {
return &Error{
Message: message,
Cause: cause,
Code: code,
}
}
// Wrap 包装现有错误为 AppError
func Wrap(err error, errType, message string, code int) *AppError {
func Newf(cause error, code int, format string, args ...interface{}) *Error {
return &Error{
Message: fmt.Sprintf(format, args...),
Cause: cause,
Code: http.StatusInternalServerError,
}
}
func Wrap(err error, message string, code int) *Error {
if err == nil {
return nil
}
// 如果已经是 AppError保留原始类型但更新消息
if appErr, ok := err.(*AppError); ok {
return &AppError{
Type: appErr.Type,
if appErr, ok := err.(*Error); ok {
return &Error{
Message: message,
Cause: appErr.Cause,
Code: appErr.Code,
@@ -108,44 +83,15 @@ func Wrap(err error, errType, message string, code int) *AppError {
}
}
return New(errType, message, err, code)
return New(err, code, message)
}
// Is 检查错误是否为特定类型
func Is(err error, errType string) bool {
if err == nil {
return false
}
var appErr *AppError
if errors.As(err, &appErr) {
return appErr.Type == errType
}
return false
}
// GetType 获取错误类型
func GetType(err error) string {
if err == nil {
return ""
}
var appErr *AppError
if errors.As(err, &appErr) {
return appErr.Type
}
return "unknown"
}
// GetCode 获取错误的 HTTP 状态码
func GetCode(err error) int {
if err == nil {
return http.StatusOK
}
var appErr *AppError
var appErr *Error
if errors.As(err, &appErr) {
return appErr.Code
}
@@ -153,7 +99,6 @@ func GetCode(err error) int {
return http.StatusInternalServerError
}
// RootCause 获取错误链中的根本原因
func RootCause(err error) error {
for err != nil {
unwrapped := errors.Unwrap(err)
@@ -165,81 +110,11 @@ func RootCause(err error) error {
return err
}
// ErrInvalidArg 无效参数错误
func ErrInvalidArg(param string) *AppError {
return New(ErrTypeInvalidArg, fmt.Sprintf("invalid arg: %s", param), nil, http.StatusBadRequest).WithStack()
}
// Database 创建数据库错误
func Database(message string, cause error) *AppError {
return New(ErrTypeDatabase, message, cause, http.StatusInternalServerError).WithStack()
}
// WeChat 创建微信相关错误
func WeChat(message string, cause error) *AppError {
return New(ErrTypeWeChat, message, cause, http.StatusInternalServerError).WithStack()
}
// HTTP 创建HTTP服务错误
func HTTP(message string, cause error) *AppError {
return New(ErrTypeHTTP, message, cause, http.StatusInternalServerError).WithStack()
}
// Config 创建配置错误
func Config(message string, cause error) *AppError {
return New(ErrTypeConfig, message, cause, http.StatusInternalServerError).WithStack()
}
// NotFound 创建资源不存在错误
func NotFound(resource string, cause error) *AppError {
message := fmt.Sprintf("resource not found: %s", resource)
return New(ErrTypeNotFound, message, cause, http.StatusNotFound).WithStack()
}
// Unauthorized 创建未授权错误
func Unauthorized(message string, cause error) *AppError {
return New(ErrTypeAuth, message, cause, http.StatusUnauthorized).WithStack()
}
// Forbidden 创建权限不足错误
func Forbidden(message string, cause error) *AppError {
return New(ErrTypePermission, message, cause, http.StatusForbidden).WithStack()
}
// Validation 创建数据验证错误
func Validation(message string, cause error) *AppError {
return New(ErrTypeValidation, message, cause, http.StatusBadRequest).WithStack()
}
// RateLimit 创建请求频率限制错误
func RateLimit(message string, cause error) *AppError {
return New(ErrTypeRateLimit, message, cause, http.StatusTooManyRequests).WithStack()
}
// Internal 创建内部服务器错误
func Internal(message string, cause error) *AppError {
return New(ErrTypeInternal, message, cause, http.StatusInternalServerError).WithStack()
}
// Err 在HTTP响应中返回错误
func Err(c *gin.Context, err error) {
// 获取请求ID如果有
requestID := c.GetString("RequestID")
if appErr, ok := err.(*AppError); ok {
if requestID != "" {
appErr.RequestID = requestID
}
if appErr, ok := err.(*Error); ok {
c.JSON(appErr.Code, appErr)
return
}
// 未知错误
unknownErr := &AppError{
Type: "unknown",
Message: err.Error(),
Code: http.StatusInternalServerError,
RequestID: requestID,
}
c.JSON(http.StatusInternalServerError, unknownErr)
c.JSON(http.StatusInternalServerError, err.Error())
}

View File

@@ -1,165 +0,0 @@
package errors
import (
"fmt"
"net/http"
"testing"
)
func TestErrorCreation(t *testing.T) {
// 测试创建基本错误
err := New("test", "test message", nil, http.StatusBadRequest)
if err.Type != "test" || err.Message != "test message" || err.Code != http.StatusBadRequest {
t.Errorf("New() created incorrect error: %v", err)
}
// 测试创建带原因的错误
cause := fmt.Errorf("original error")
err = New("test", "test with cause", cause, http.StatusInternalServerError)
if err.Cause != cause {
t.Errorf("New() did not set cause correctly: %v", err)
}
// 测试错误消息格式
expected := "test: test with cause: original error"
if err.Error() != expected {
t.Errorf("Error() = %q, want %q", err.Error(), expected)
}
}
func TestErrorWrapping(t *testing.T) {
// 测试包装普通错误
original := fmt.Errorf("original error")
wrapped := Wrap(original, "wrapped", "wrapped message", http.StatusBadRequest)
if wrapped.Type != "wrapped" || wrapped.Message != "wrapped message" {
t.Errorf("Wrap() created incorrect error: %v", wrapped)
}
if wrapped.Cause != original {
t.Errorf("Wrap() did not set cause correctly")
}
// 测试包装 AppError
appErr := New("app", "app error", nil, http.StatusNotFound)
rewrapped := Wrap(appErr, "ignored", "new message", http.StatusBadRequest)
if rewrapped.Type != "app" {
t.Errorf("Wrap() did not preserve original AppError type: got %s, want %s",
rewrapped.Type, appErr.Type)
}
if rewrapped.Message != "new message" {
t.Errorf("Wrap() did not update message: got %s, want %s",
rewrapped.Message, "new message")
}
if rewrapped.Code != appErr.Code {
t.Errorf("Wrap() did not preserve original status code: got %d, want %d",
rewrapped.Code, appErr.Code)
}
}
func TestErrorTypeChecking(t *testing.T) {
// 创建不同类型的错误
dbErr := Database("db error", nil)
httpErr := HTTP("http error", nil)
// 测试 Is 函数
if !Is(dbErr, ErrTypeDatabase) {
t.Errorf("Is() failed to identify database error")
}
if Is(dbErr, ErrTypeHTTP) {
t.Errorf("Is() incorrectly identified database error as HTTP error")
}
if !Is(httpErr, ErrTypeHTTP) {
t.Errorf("Is() failed to identify HTTP error")
}
// 测试 GetType 函数
if GetType(dbErr) != ErrTypeDatabase {
t.Errorf("GetType() returned incorrect type: got %s, want %s",
GetType(dbErr), ErrTypeDatabase)
}
if GetType(httpErr) != ErrTypeHTTP {
t.Errorf("GetType() returned incorrect type: got %s, want %s",
GetType(httpErr), ErrTypeHTTP)
}
// 测试普通错误
stdErr := fmt.Errorf("standard error")
if GetType(stdErr) != "unknown" {
t.Errorf("GetType() for standard error should return 'unknown', got %s",
GetType(stdErr))
}
}
func TestErrorUnwrapping(t *testing.T) {
// 创建嵌套错误
innermost := fmt.Errorf("innermost error")
inner := Wrap(innermost, "inner", "inner error", http.StatusBadRequest)
outer := Wrap(inner, "outer", "outer error", http.StatusInternalServerError)
// 测试 Unwrap
if unwrapped := outer.Unwrap(); unwrapped != inner.Cause {
t.Errorf("Unwrap() did not return correct inner error")
}
// 测试 RootCause
if root := RootCause(outer); root != innermost {
t.Errorf("RootCause() did not return innermost error")
}
}
func TestErrorHelperFunctions(t *testing.T) {
// 测试辅助函数
invalidArg := ErrInvalidArg("username")
if invalidArg.Type != ErrTypeInvalidArg {
t.Errorf("ErrInvalidArg() created error with wrong type: %s", invalidArg.Type)
}
dbErr := Database("query failed", nil)
if dbErr.Type != ErrTypeDatabase {
t.Errorf("Database() created error with wrong type: %s", dbErr.Type)
}
notFound := NotFound("user", nil)
if notFound.Type != ErrTypeNotFound || notFound.Code != http.StatusNotFound {
t.Errorf("NotFound() created error with wrong type or code: %s, %d",
notFound.Type, notFound.Code)
}
}
func TestErrorUtilityFunctions(t *testing.T) {
// 测试 JoinErrors
err1 := fmt.Errorf("error 1")
err2 := fmt.Errorf("error 2")
// 单个错误
if joined := JoinErrors(err1); joined != err1 {
t.Errorf("JoinErrors() with single error should return that error")
}
// 多个错误
joined := JoinErrors(err1, err2)
if joined == nil {
t.Errorf("JoinErrors() returned nil for multiple errors")
}
// nil 错误
if joined := JoinErrors(nil, nil); joined != nil {
t.Errorf("JoinErrors() with all nil should return nil")
}
// 测试 WrapIfErr
if wrapped := WrapIfErr(nil, "test", "message", http.StatusOK); wrapped != nil {
t.Errorf("WrapIfErr() with nil should return nil")
}
if wrapped := WrapIfErr(err1, "test", "message", http.StatusBadRequest); wrapped == nil {
t.Errorf("WrapIfErr() with non-nil error should return non-nil")
}
}

View File

@@ -0,0 +1,11 @@
package errors
import "net/http"
func InvalidArg(arg string) error {
return Newf(nil, http.StatusBadRequest, "invalid argument: %s", arg)
}
func HTTPShutDown(cause error) error {
return Newf(cause, http.StatusInternalServerError, "http server shut down")
}

View File

@@ -1,12 +1,11 @@
package errors
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
)
// ErrorHandlerMiddleware 是一个 Gin 中间件,用于统一处理请求过程中的错误
@@ -40,21 +39,18 @@ func RecoveryMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
defer func() {
if r := recover(); r != nil {
// 获取请求 ID
requestID, _ := c.Get("RequestID")
requestIDStr, _ := requestID.(string)
// 创建内部服务器错误
var err *AppError
var err *Error
switch v := r.(type) {
case error:
err = Internal("panic recovered", v).WithRequestID(requestIDStr)
err = New(v, http.StatusInternalServerError, "panic recovered")
default:
err = Internal(fmt.Sprintf("panic recovered: %v", r), nil).WithRequestID(requestIDStr)
err = Newf(nil, http.StatusInternalServerError, "panic recovered: %v", r)
}
// 记录错误日志
log.Errorf("PANIC RECOVERED: %v\n", err)
log.Err(err).Msg("PANIC RECOVERED")
// 返回 500 错误
c.JSON(http.StatusInternalServerError, err)

View File

@@ -0,0 +1,23 @@
package errors
import "net/http"
func OpenFileFailed(path string, cause error) *Error {
return Newf(cause, http.StatusInternalServerError, "failed to open file: %s", path).WithStack()
}
func StatFileFailed(path string, cause error) *Error {
return Newf(cause, http.StatusInternalServerError, "failed to stat file: %s", path).WithStack()
}
func ReadFileFailed(path string, cause error) *Error {
return Newf(cause, http.StatusInternalServerError, "failed to read file: %s", path).WithStack()
}
func IncompleteRead(cause error) *Error {
return New(cause, http.StatusInternalServerError, "incomplete header read during decryption").WithStack()
}
func WriteOutputFailed(cause error) *Error {
return New(cause, http.StatusInternalServerError, "failed to write output").WithStack()
}

View File

@@ -1,131 +0,0 @@
package errors
import (
stderrors "errors"
"fmt"
"strings"
)
// WrapIfErr 如果 err 不为 nil则包装错误并返回否则返回 nil
func WrapIfErr(err error, errType, message string, code int) error {
if err == nil {
return nil
}
return Wrap(err, errType, message, code)
}
// JoinErrors 将多个错误合并为一个错误
// 如果只有一个错误不为 nil则返回该错误
// 如果有多个错误不为 nil则创建一个包含所有错误信息的新错误
func JoinErrors(errs ...error) error {
var nonNilErrs []error
for _, err := range errs {
if err != nil {
nonNilErrs = append(nonNilErrs, err)
}
}
if len(nonNilErrs) == 0 {
return nil
}
if len(nonNilErrs) == 1 {
return nonNilErrs[0]
}
// 合并多个错误
var messages []string
for _, err := range nonNilErrs {
messages = append(messages, err.Error())
}
return Internal(
fmt.Sprintf("multiple errors occurred: %s", strings.Join(messages, "; ")),
nonNilErrs[0],
)
}
// IsNil 检查错误是否为 nil
func IsNil(err error) bool {
return err == nil
}
// IsNotNil 检查错误是否不为 nil
func IsNotNil(err error) bool {
return err != nil
}
// IsType 检查错误是否为指定类型
func IsType(err error, errType string) bool {
return Is(err, errType)
}
// HasCause 检查错误是否包含指定的原因
func HasCause(err error, cause error) bool {
if err == nil || cause == nil {
return false
}
var appErr *AppError
if stderrors.As(err, &appErr) {
if appErr.Cause == cause {
return true
}
return HasCause(appErr.Cause, cause)
}
return err == cause
}
// AsAppError 将错误转换为 AppError 类型
func AsAppError(err error) (*AppError, bool) {
var appErr *AppError
if stderrors.As(err, &appErr) {
return appErr, true
}
return nil, false
}
// FormatErrorChain 格式化错误链,便于调试
func FormatErrorChain(err error) string {
if err == nil {
return "<nil>"
}
var result strings.Builder
result.WriteString(err.Error())
// 获取 AppError 类型的堆栈信息
var appErr *AppError
if stderrors.As(err, &appErr) && len(appErr.Stack) > 0 {
result.WriteString("\nStack Trace:\n")
for _, frame := range appErr.Stack {
result.WriteString(" ")
result.WriteString(frame)
result.WriteString("\n")
}
}
// 递归处理错误链
cause := stderrors.Unwrap(err)
if cause != nil {
result.WriteString("\nCaused by: ")
result.WriteString(FormatErrorChain(cause))
}
return result.String()
}
// GetErrorDetails 返回错误的详细信息包括类型、消息、HTTP状态码和请求ID
func GetErrorDetails(err error) (errType string, message string, code int, requestID string) {
if err == nil {
return "", "", 0, ""
}
var appErr *AppError
if stderrors.As(err, &appErr) {
return appErr.Type, appErr.Message, appErr.Code, appErr.RequestID
}
return "unknown", err.Error(), 500, ""
}

View File

@@ -0,0 +1,65 @@
package errors
import "net/http"
var (
ErrAlreadyDecrypted = New(nil, http.StatusBadRequest, "database file is already decrypted")
ErrDecryptHashVerificationFailed = New(nil, http.StatusBadRequest, "hash verification failed during decryption")
ErrDecryptIncorrectKey = New(nil, http.StatusBadRequest, "incorrect decryption key")
ErrDecryptOperationCanceled = New(nil, http.StatusBadRequest, "decryption operation was canceled")
ErrNoMemoryRegionsFound = New(nil, http.StatusBadRequest, "no memory regions found")
ErrReadMemoryTimeout = New(nil, http.StatusInternalServerError, "read memory timeout")
ErrWeChatOffline = New(nil, http.StatusBadRequest, "WeChat is offline")
ErrSIPEnabled = New(nil, http.StatusBadRequest, "SIP is enabled")
ErrValidatorNotSet = New(nil, http.StatusBadRequest, "validator not set")
ErrNoValidKey = New(nil, http.StatusBadRequest, "no valid key found")
ErrWeChatDLLNotFound = New(nil, http.StatusBadRequest, "WeChatWin.dll module not found")
)
func PlatformUnsupported(platform string, version int) *Error {
return Newf(nil, http.StatusBadRequest, "unsupported platform: %s v%d", platform, version).WithStack()
}
func DecryptCreateCipherFailed(cause error) *Error {
return New(cause, http.StatusInternalServerError, "failed to create cipher").WithStack()
}
func DecodeKeyFailed(cause error) *Error {
return New(cause, http.StatusBadRequest, "failed to decode hex key").WithStack()
}
func CreatePipeFileFailed(cause error) *Error {
return New(cause, http.StatusInternalServerError, "failed to create pipe file").WithStack()
}
func OpenPipeFileFailed(cause error) *Error {
return New(cause, http.StatusInternalServerError, "failed to open pipe file").WithStack()
}
func ReadPipeFileFailed(cause error) *Error {
return New(cause, http.StatusInternalServerError, "failed to read from pipe file").WithStack()
}
func RunCmdFailed(cause error) *Error {
return New(cause, http.StatusInternalServerError, "failed to run command").WithStack()
}
func ReadMemoryFailed(cause error) *Error {
return New(cause, http.StatusInternalServerError, "failed to read memory").WithStack()
}
func OpenProcessFailed(cause error) *Error {
return New(cause, http.StatusInternalServerError, "failed to open process").WithStack()
}
func WeChatAccountNotFound(name string) *Error {
return Newf(nil, http.StatusBadRequest, "WeChat account not found: %s", name).WithStack()
}
func WeChatAccountNotOnline(name string) *Error {
return Newf(nil, http.StatusBadRequest, "WeChat account is not online: %s", name).WithStack()
}
func RefreshProcessStatusFailed(cause error) *Error {
return New(cause, http.StatusInternalServerError, "failed to refresh process status").WithStack()
}

View File

@@ -0,0 +1,62 @@
package errors
import (
"net/http"
"time"
)
var (
ErrTalkerEmpty = New(nil, http.StatusBadRequest, "talker empty").WithStack()
ErrKeyEmpty = New(nil, http.StatusBadRequest, "key empty").WithStack()
ErrMediaNotFound = New(nil, http.StatusNotFound, "media not found").WithStack()
ErrKeyLengthMust32 = New(nil, http.StatusBadRequest, "key length must be 32 bytes").WithStack()
)
// 数据库初始化相关错误
func DBFileNotFound(path, pattern string, cause error) *Error {
return Newf(cause, http.StatusNotFound, "db file not found %s: %s", path, pattern).WithStack()
}
func DBConnectFailed(path string, cause error) *Error {
return Newf(cause, http.StatusInternalServerError, "db connect failed: %s", path).WithStack()
}
func DBInitFailed(cause error) *Error {
return New(cause, http.StatusInternalServerError, "db init failed").WithStack()
}
func TalkerNotFound(talker string) *Error {
return Newf(nil, http.StatusNotFound, "talker not found: %s", talker).WithStack()
}
func DBCloseFailed(cause error) *Error {
return New(cause, http.StatusInternalServerError, "db close failed").WithStack()
}
func QueryFailed(query string, cause error) *Error {
return Newf(cause, http.StatusInternalServerError, "query failed: %s", query).WithStack()
}
func ScanRowFailed(cause error) *Error {
return New(cause, http.StatusInternalServerError, "scan row failed").WithStack()
}
func TimeRangeNotFound(start, end time.Time) *Error {
return Newf(nil, http.StatusNotFound, "time range not found: %s - %s", start, end).WithStack()
}
func MediaTypeUnsupported(_type string) *Error {
return Newf(nil, http.StatusBadRequest, "unsupported media type: %s", _type).WithStack()
}
func ChatRoomNotFound(key string) *Error {
return Newf(nil, http.StatusNotFound, "chat room not found: %s", key).WithStack()
}
func ContactNotFound(key string) *Error {
return Newf(nil, http.StatusNotFound, "contact not found: %s", key).WithStack()
}
func InitCacheFailed(cause error) *Error {
return New(cause, http.StatusInternalServerError, "init cache failed").WithStack()
}