@@ -190,62 +190,62 @@ public function waitRaw(ResponsePromise $promise): Response
190190
191191 $ responseContent = $ response ->getBody ()->getContents ();
192192 $ errorObject = Validator::make (\json_decode ($ responseContent ))->object ();
193- $ errorParts = [];
193+ $ messageParts = [];
194194
195- // https://datatracker.ietf.org/doc/html/rfc7807
195+ // https://datatracker.ietf.org/doc/html/rfc7807#section-3.1 type
196196 $ errorType = $ errorObject ->property ('type ' )->string ()->notEmpty ()->nullable (true );
197197 if ($ errorType !== null ) {
198- $ errorParts [] = 'Error type: " ' .$ errorType .'" ' ;
198+ $ messageParts [] = 'Error type: " ' .$ errorType .'" ' ;
199199 }
200200
201- $ errorMessage =
202- $ errorObject
203- ->property ('title ' )
204- ->string ()
205- ->notEmpty ()
206- ->nullable (true )
207- ??
208- $ errorObject
209- ->property ('message ' )
210- ->string ()
211- ->notEmpty ()
212- ->nullable (true )
213- ;
201+ $ errorMessage = $ errorObject ->property ('message ' )->string ()->notEmpty ()->nullable (true );
214202 if ($ errorMessage !== null ) {
215- $ errorParts [] = $ errorMessage ;
203+ $ messageParts [] = $ errorMessage ;
216204 }
217205
218- if (count ($ errorParts ) !== 0 ) {
219- $ errorStatus = $ errorObject ->property ('status ' )->int ()->nullable (true );
220- if ($ errorStatus !== null ) {
221- $ errorParts [] = 'status ( ' .$ errorStatus .') ' ;
206+ // https://datatracker.ietf.org/doc/html/rfc7807#section-3.1 status
207+ $ errorStatus = $ errorObject ->property ('status ' )->int ()->nullable (true ) ?? $ response ->getStatusCode ();
208+ // https://datatracker.ietf.org/doc/html/rfc7807#section-3.1 instance
209+ $ errorInstance = $ errorObject ->property ('instance ' )->string ()->notEmpty ()->nullable (true ) ?? $ promise ->request ->url ();
210+ // https://datatracker.ietf.org/doc/html/rfc7807#section-3.1 title
211+ $ errorTitle = $ errorObject ->property ('title ' )->string ()->notEmpty ()->nullable (true );
212+ // https://datatracker.ietf.org/doc/html/rfc7807#section-3.1 detail
213+ $ errorDetail = $ errorObject ->property ('detail ' )->string ()->notEmpty ()->nullable (true );
214+
215+ if (count ($ messageParts ) === 0 ) {
216+ if ($ errorTitle !== null ) {
217+ $ messageParts [] = 'Error title: " ' .$ errorTitle .'" ' ;
222218 }
223- $ errorInstance = $ errorObject ->property ('instance ' )->string ()->notEmpty ()->nullable (true );
224- if ($ errorInstance !== null ) {
225- $ errorParts [] = 'instance: " ' .$ errorInstance .'" ' ;
219+ if ($ errorDetail !== null ) {
220+ $ messageParts [] = 'Error detail: " ' .$ errorDetail .'" ' ;
226221 }
222+ }
223+
224+ if (count ($ messageParts ) !== 0 ) {
225+ $ messageParts [] = 'status ( ' . $ errorStatus . ') ' ;
226+ $ messageParts [] = 'instance: " ' . $ errorInstance . '" ' ;
227227
228- $ message = implode (' ' , $ errorParts );
228+ $ message = implode (' ' , $ messageParts );
229229 }
230230
231231 $ response = $ response ->withBody ((new HttpFactory ())->createStream ($ responseContent ));
232232 $ response = new Response ($ promise ->request , $ response );
233233
234234 match ($ response ->getStatusCode ()) {
235- HttpCodes::HTTP_BAD_REQUEST => throw new BadRequestApiException ($ message , $ promise ->request , $ response , $ exception ),
236- HttpCodes::HTTP_UNAUTHORIZED => throw new UnauthorizedApiException ($ message , $ promise ->request , $ response , $ exception ),
237- HttpCodes::HTTP_FORBIDDEN => throw new ForbiddenApiException ($ message , $ promise ->request , $ response , $ exception ),
238- HttpCodes::HTTP_NOT_FOUND => throw new NotFoundApiException ($ message , $ promise ->request , $ response , $ exception ),
239- HttpCodes::HTTP_CONFLICT => throw new ConflictApiException ($ message , $ promise ->request , $ response , $ exception ),
240- HttpCodes::HTTP_GONE => throw new GoneApiException ($ message , $ promise ->request , $ response , $ exception ),
241- HttpCodes::HTTP_INTERNAL_SERVER_ERROR => throw new InternalServerErrorApiException ($ message , $ promise ->request , $ response , $ exception ),
242- default => throw new ResponseApiException ($ message , $ promise ->request , $ response , $ exception ),
235+ HttpCodes::HTTP_BAD_REQUEST => throw new BadRequestApiException ($ message , $ errorStatus , $ errorInstance , $ errorType , $ errorTitle , $ errorDetail , $ promise ->request , $ response , $ exception ),
236+ HttpCodes::HTTP_UNAUTHORIZED => throw new UnauthorizedApiException ($ message , $ errorStatus , $ errorInstance , $ errorType , $ errorTitle , $ errorDetail , $ promise ->request , $ response , $ exception ),
237+ HttpCodes::HTTP_FORBIDDEN => throw new ForbiddenApiException ($ message , $ errorStatus , $ errorInstance , $ errorType , $ errorTitle , $ errorDetail , $ promise ->request , $ response , $ exception ),
238+ HttpCodes::HTTP_NOT_FOUND => throw new NotFoundApiException ($ message , $ errorStatus , $ errorInstance , $ errorType , $ errorTitle , $ errorDetail , $ promise ->request , $ response , $ exception ),
239+ HttpCodes::HTTP_CONFLICT => throw new ConflictApiException ($ message , $ errorStatus , $ errorInstance , $ errorType , $ errorTitle , $ errorDetail , $ promise ->request , $ response , $ exception ),
240+ HttpCodes::HTTP_GONE => throw new GoneApiException ($ message , $ errorStatus , $ errorInstance , $ errorType , $ errorTitle , $ errorDetail , $ promise ->request , $ response , $ exception ),
241+ HttpCodes::HTTP_INTERNAL_SERVER_ERROR => throw new InternalServerErrorApiException ($ message , $ errorStatus , $ errorInstance , $ errorType , $ errorTitle , $ errorDetail , $ promise ->request , $ response , $ exception ),
242+ default => throw new ResponseApiException ($ message , $ errorStatus , $ errorInstance , $ errorType , $ errorTitle , $ errorDetail , $ promise ->request , $ response , $ exception ),
243243 };
244244 }
245245
246- throw new ApiException ($ message , $ promise ->request , null , $ exception );
246+ throw new ApiException ($ message , 0 , $ promise ->request -> url () , null , null , null , $ promise -> request , $ response , $ exception );
247247 } catch (TransferException $ exception ) {
248- throw new ApiException ($ exception ->getMessage (), $ promise ->request , null , $ exception );
248+ throw new ApiException ($ exception ->getMessage (), 0 , $ promise -> request -> url (), null , null , null , $ promise ->request , null , $ exception );
249249 }
250250
251251 $ this ->deprecationsLogger ?->logDeprecation($ promise ->apiName , $ promise ->request , $ response );
0 commit comments