Skip to content

Commit

Permalink
Compatible with Azure Openai's 2023-07-01-preview version API interfa…
Browse files Browse the repository at this point in the history
…ce about the error information returned by the intercepted interface
  • Loading branch information
ZeroDeng01 committed Jul 24, 2023
1 parent 1153eb2 commit 0a5fa89
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
24 changes: 19 additions & 5 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ import (

// APIError provides error information returned by the OpenAI API.
type APIError struct {
Code any `json:"code,omitempty"`
Message string `json:"message"`
Param *string `json:"param,omitempty"`
Type string `json:"type"`
HTTPStatusCode int `json:"-"`
Code any `json:"code,omitempty"`
Message string `json:"message"`
Param *string `json:"param,omitempty"`
Type string `json:"type"`
HTTPStatusCode int `json:"-"`
Innererror Innererror `json:"innererror,omitempty"`
}

// Azure Content filtering.
type Innererror struct {
Code string `json:"code,omitempty"`
ContentFilterResults ContentFilterResults `json:"content_filter_result,omitempty"`
}

// RequestError provides informations about generic request errors.
Expand Down Expand Up @@ -61,6 +68,13 @@ func (e *APIError) UnmarshalJSON(data []byte) (err error) {
}
}

if _, ok := rawMap["innererror"]; ok {
err = json.Unmarshal(rawMap["innererror"], &e.Innererror)
if err != nil {
return
}
}

// optional fields
if _, ok := rawMap["param"]; ok {
err = json.Unmarshal(rawMap["param"], &e.Param)
Expand Down
22 changes: 22 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ func TestAPIErrorUnmarshalJSON(t *testing.T) {
assertAPIErrorMessage(t, apiErr, "")
},
},
{
name: "parse succeeds when the innererror is being (Azure Openai)",
response: `{"message": "","type": null,"param": "","code": "","status": 0,"innererror": {}}`,
hasError: false,
checkFunc: func(t *testing.T, apiErr APIError) {
assertAPIErrorInnererror(t, apiErr, Innererror{})
},
},
{
name: "parse succeeds when the innererror is not being (Azure Openai)",
response: `{"message": "","type": null,"param": "","code": "","status": 0}`,
hasError: false,
checkFunc: func(t *testing.T, apiErr APIError) {
assertAPIErrorInnererror(t, apiErr, Innererror{})
},
},
{
name: "parse failed when the message is object",
response: `{"message":{},"type":"invalid_request_error","param":null,"code":null}`,
Expand Down Expand Up @@ -152,6 +168,12 @@ func assertAPIErrorMessage(t *testing.T, apiErr APIError, expected string) {
}
}

func assertAPIErrorInnererror(t *testing.T, apiErr APIError, expected Innererror) {
if apiErr.Innererror != expected {
t.Errorf("Unexpected APIError Innererror: %v; expected code: %s; ", apiErr, expected.Code)
}
}

func assertAPIErrorCode(t *testing.T, apiErr APIError, expected interface{}) {
switch v := apiErr.Code.(type) {
case int:
Expand Down

0 comments on commit 0a5fa89

Please sign in to comment.