diff --git a/error.go b/error.go index f68e92875..efab27646 100644 --- a/error.go +++ b/error.go @@ -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. @@ -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) diff --git a/error_test.go b/error_test.go index e2309abd7..a69330acb 100644 --- a/error_test.go +++ b/error_test.go @@ -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}`, @@ -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: