ai-css/library/modelprovider/errorswrap/errors.go

44 lines
794 B
Go

package errorswrap
import (
"errors"
"fmt"
)
type Errors struct {
Code ErrorCode `json:"code"`
Msg string `json:"msg"`
}
func (e *Errors) Error() string {
return fmt.Sprintf("error code:%s,msg:%s", e.Code, e.Msg)
}
func NewError(code ErrorCode) error {
return &Errors{Code: code}
}
type ErrorCode string
const (
ErrorUnknown ErrorCode = "provider_unknown"
ErrorProviderApiUrlInvalid ErrorCode = "provider_api_url_invalid"
ErrorProviderApiKeyInvalid ErrorCode = "provider_api_key_invalid"
)
func ErrorIsCode(err error, code ErrorCode) bool {
var e *Errors
if errors.As(err, &e) {
return e.Code == code
}
return false
}
func GetErrorCode(err error) ErrorCode {
var e *Errors
if errors.As(err, &e) && e != nil {
return e.Code
}
return ErrorUnknown
}