feat: 实现错误码常量 + WS 错误发送工具
- 新增 internal/errors/codes.go - 定义 10 个错误码常量(与接口文档一致) - 提供 SendWSError 工具函数
This commit is contained in:
33
backend/internal/errors/codes.go
Normal file
33
backend/internal/errors/codes.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package errors
|
||||
|
||||
import "github.com/hhs/camtalk/internal/models"
|
||||
|
||||
// 错误码常量,与 docs/03-接口文档.md 保持一致。
|
||||
const (
|
||||
CodeInvalidMessage = "INVALID_MESSAGE"
|
||||
CodeSessionNotFound = "SESSION_NOT_FOUND"
|
||||
CodeRateLimited = "RATE_LIMITED"
|
||||
CodeImageTooLarge = "IMAGE_TOO_LARGE"
|
||||
CodeAudioTooShort = "AUDIO_TOO_SHORT"
|
||||
CodeLLMTimeout = "LLM_TIMEOUT"
|
||||
CodeLLMError = "LLM_ERROR"
|
||||
CodeSTTError = "STT_ERROR"
|
||||
CodeTTSError = "TTS_ERROR"
|
||||
CodeInternalError = "INTERNAL_ERROR"
|
||||
)
|
||||
|
||||
// Sender 定义发送 WS 错误消息的接口,便于测试 mock。
|
||||
type Sender interface {
|
||||
SendError(code, requestID, message string)
|
||||
}
|
||||
|
||||
// SendWSError 向客户端发送 error 消息。
|
||||
// sender 是一个具有 sendJSON 方法的对象,这里用接口抽象。
|
||||
func SendWSError(sender interface{ SendJSON(v any) error }, code, requestID string, err error) {
|
||||
_ = sender.SendJSON(models.WsError{
|
||||
Type: "error",
|
||||
Code: code,
|
||||
RequestID: requestID,
|
||||
Message: err.Error(),
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user