@@ -24,6 +24,7 @@ func NewGenericRequestError(operation string, err error) *GenericRequestError {
2424
2525// APIError represents an error response from the API
2626type APIError struct {
27+ Name string `json:"name"`
2728 AdditionalContext string `json:"additionalContext,omitempty"`
2829 ExtraMessages []string `json:"-"`
2930 Details any `json:"details,omitempty"`
@@ -79,7 +80,7 @@ func (e APIError) Error() string {
7980}
8081
8182func NewAPIErrorWithResponse (operation string , res * resty.Response , additionalContext * string ) error {
82- errorMessage , details := TryParseErrorBody (res )
83+ errorMessage , details , errorName := TryParseErrorBody (res )
8384 reqId := util .TryExtractReqId (res )
8485
8586 if res == nil {
@@ -88,6 +89,7 @@ func NewAPIErrorWithResponse(operation string, res *resty.Response, additionalCo
8889
8990 apiError := & APIError {
9091 Operation : operation ,
92+ Name : errorName ,
9193 Method : res .Request .Method ,
9294 URL : res .Request .URL ,
9395 StatusCode : res .StatusCode (),
@@ -134,26 +136,27 @@ type errorResponse struct {
134136 Message string `json:"message"`
135137 Details any `json:"details"`
136138 ReqId string `json:"reqId"`
139+ Name string `json:"error"`
137140}
138141
139142/*
140143Instead of changing the signature of the sdk function - let's just keep a one local to this codebase
141144*/
142- func TryParseErrorBody (res * resty.Response ) (string , any ) {
145+ func TryParseErrorBody (res * resty.Response ) (string , any , string ) {
143146 var details any
144147
145148 if res == nil || ! res .IsError () {
146- return "" , details
149+ return "" , details , ""
147150 }
148151
149152 body := res .String ()
150153 if body == "" {
151- return "" , details
154+ return "" , details , ""
152155 }
153156
154157 // stringify zod body entirely
155158 if res .StatusCode () == 422 {
156- return body , details
159+ return body , details , ""
157160 }
158161
159162 // now we have a string, we need to try to parse it as json
@@ -162,30 +165,30 @@ func TryParseErrorBody(res *resty.Response) (string, any) {
162165 err := json .Unmarshal ([]byte (body ), & errorResponse )
163166
164167 if err != nil {
165- return "" , details
168+ return "" , details , ""
166169 }
167170
168171 // Check if details is empty and return nil if so
169172 if errorResponse .Details != nil {
170173 switch v := errorResponse .Details .(type ) {
171174 case []any :
172175 if len (v ) == 0 {
173- return errorResponse .Message , nil
176+ return errorResponse .Message , nil , errorResponse . Name
174177 }
175178 case []string :
176179 if len (v ) == 0 {
177- return errorResponse .Message , nil
180+ return errorResponse .Message , nil , errorResponse . Name
178181 }
179182 case map [string ]any :
180183 if len (v ) == 0 {
181- return errorResponse .Message , nil
184+ return errorResponse .Message , nil , errorResponse . Name
182185 }
183186 case string :
184187 if v == "" {
185- return errorResponse .Message , nil
188+ return errorResponse .Message , nil , errorResponse . Name
186189 }
187190 }
188191 }
189192
190- return errorResponse .Message , errorResponse .Details
193+ return errorResponse .Message , errorResponse .Details , errorResponse . Name
191194}
0 commit comments