xybot/base/response.go
2025-04-17 14:59:55 +08:00

42 lines
781 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package base
import (
"fmt"
)
// BaseResponse[T any]
// @description: 基础返回结构
type Response[T any] struct {
Success bool `json:"Success"`
Code int `json:"Code"`
Message string `json:"Message"`
Data T `json:"Data"`
}
// IsSuccess
// @description: 是否成功
// @receiver r
// @return bool
func (r Response[T]) IsSuccess() bool {
return r.Code == 0
}
// CheckError
// @description: 处理错误状态
// @receiver r
// @return error
func (r Response[T]) CheckError(err error) error {
if err != nil {
return err
}
if errMsg, ok := codeMap[r.Code]; ok {
return fmt.Errorf("[%d] %s - %s", r.Code, errMsg, r.Message)
}
return nil
}
// EmptyResponse
// @description: 空响应用于只需要状态码的API
type EmptyResponse struct{}