feat: support ollama claude format
This commit is contained in:
@@ -75,7 +75,7 @@ const (
|
||||
type NewAPIError struct {
|
||||
Err error
|
||||
RelayError any
|
||||
ErrorType ErrorType
|
||||
errorType ErrorType
|
||||
errorCode ErrorCode
|
||||
StatusCode int
|
||||
}
|
||||
@@ -87,6 +87,13 @@ func (e *NewAPIError) GetErrorCode() ErrorCode {
|
||||
return e.errorCode
|
||||
}
|
||||
|
||||
func (e *NewAPIError) GetErrorType() ErrorType {
|
||||
if e == nil {
|
||||
return ""
|
||||
}
|
||||
return e.errorType
|
||||
}
|
||||
|
||||
func (e *NewAPIError) Error() string {
|
||||
if e == nil {
|
||||
return ""
|
||||
@@ -103,7 +110,7 @@ func (e *NewAPIError) SetMessage(message string) {
|
||||
}
|
||||
|
||||
func (e *NewAPIError) ToOpenAIError() OpenAIError {
|
||||
switch e.ErrorType {
|
||||
switch e.errorType {
|
||||
case ErrorTypeOpenAIError:
|
||||
if openAIError, ok := e.RelayError.(OpenAIError); ok {
|
||||
return openAIError
|
||||
@@ -120,14 +127,14 @@ func (e *NewAPIError) ToOpenAIError() OpenAIError {
|
||||
}
|
||||
return OpenAIError{
|
||||
Message: e.Error(),
|
||||
Type: string(e.ErrorType),
|
||||
Type: string(e.errorType),
|
||||
Param: "",
|
||||
Code: e.errorCode,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *NewAPIError) ToClaudeError() ClaudeError {
|
||||
switch e.ErrorType {
|
||||
switch e.errorType {
|
||||
case ErrorTypeOpenAIError:
|
||||
openAIError := e.RelayError.(OpenAIError)
|
||||
return ClaudeError{
|
||||
@@ -139,7 +146,7 @@ func (e *NewAPIError) ToClaudeError() ClaudeError {
|
||||
default:
|
||||
return ClaudeError{
|
||||
Message: e.Error(),
|
||||
Type: string(e.ErrorType),
|
||||
Type: string(e.errorType),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -148,7 +155,7 @@ func NewError(err error, errorCode ErrorCode) *NewAPIError {
|
||||
return &NewAPIError{
|
||||
Err: err,
|
||||
RelayError: nil,
|
||||
ErrorType: ErrorTypeNewAPIError,
|
||||
errorType: ErrorTypeNewAPIError,
|
||||
StatusCode: http.StatusInternalServerError,
|
||||
errorCode: errorCode,
|
||||
}
|
||||
@@ -162,6 +169,13 @@ func NewOpenAIError(err error, errorCode ErrorCode, statusCode int) *NewAPIError
|
||||
return WithOpenAIError(openaiError, statusCode)
|
||||
}
|
||||
|
||||
func InitOpenAIError(errorCode ErrorCode, statusCode int) *NewAPIError {
|
||||
openaiError := OpenAIError{
|
||||
Type: string(errorCode),
|
||||
}
|
||||
return WithOpenAIError(openaiError, statusCode)
|
||||
}
|
||||
|
||||
func NewErrorWithStatusCode(err error, errorCode ErrorCode, statusCode int) *NewAPIError {
|
||||
return &NewAPIError{
|
||||
Err: err,
|
||||
@@ -169,7 +183,7 @@ func NewErrorWithStatusCode(err error, errorCode ErrorCode, statusCode int) *New
|
||||
Message: err.Error(),
|
||||
Type: string(errorCode),
|
||||
},
|
||||
ErrorType: ErrorTypeNewAPIError,
|
||||
errorType: ErrorTypeNewAPIError,
|
||||
StatusCode: statusCode,
|
||||
errorCode: errorCode,
|
||||
}
|
||||
@@ -182,7 +196,7 @@ func WithOpenAIError(openAIError OpenAIError, statusCode int) *NewAPIError {
|
||||
}
|
||||
return &NewAPIError{
|
||||
RelayError: openAIError,
|
||||
ErrorType: ErrorTypeOpenAIError,
|
||||
errorType: ErrorTypeOpenAIError,
|
||||
StatusCode: statusCode,
|
||||
Err: errors.New(openAIError.Message),
|
||||
errorCode: ErrorCode(code),
|
||||
@@ -192,7 +206,7 @@ func WithOpenAIError(openAIError OpenAIError, statusCode int) *NewAPIError {
|
||||
func WithClaudeError(claudeError ClaudeError, statusCode int) *NewAPIError {
|
||||
return &NewAPIError{
|
||||
RelayError: claudeError,
|
||||
ErrorType: ErrorTypeClaudeError,
|
||||
errorType: ErrorTypeClaudeError,
|
||||
StatusCode: statusCode,
|
||||
Err: errors.New(claudeError.Message),
|
||||
errorCode: ErrorCode(claudeError.Type),
|
||||
@@ -211,5 +225,5 @@ func IsLocalError(err *NewAPIError) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
return err.ErrorType == ErrorTypeNewAPIError
|
||||
return err.errorType == ErrorTypeNewAPIError
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user