@@ -16,16 +16,12 @@ class ApiException extends \Exception
1616
1717 public static function createFromResponse ($ response )
1818 {
19- $ object = static ::parseResponseBody ($ response );
19+ $ error = static ::parseResponseBody ($ response );
2020
2121 $ field = null ;
22- if (! empty ($ object ->field )) {
23- $ field = $ object ->field ;
24- }
25- // die(var_dump($object));
2622
2723 return new self (
28- "Error executing API call ( {$ object -> message })} " ,
24+ "Error executing API call ( {$ error })} " ,
2925 $ response ->getStatusCode (),
3026 $ field
3127 );
@@ -34,13 +30,28 @@ public static function createFromResponse($response)
3430 protected static function parseResponseBody ($ response )
3531 {
3632 $ body = (string ) $ response ->getBody ();
37-
33+ $ error = "" ;
3834 $ object = @json_decode ($ body );
3935
36+ if (property_exists ($ object , "message " ))
37+ $ error = $ object ->message ;
38+
39+ if (property_exists ($ object , "errors " ))
40+ {
41+ $ properties = array_keys (get_object_vars ($ object ->errors ));
42+
43+ if (is_array ($ properties ))
44+ $ error_array = $ object ->errors ->{$ properties [0 ]};
45+
46+ if (is_array ($ error_array ))
47+ $ error = $ error_array [0 ];
48+
49+ }
50+
4051 if (json_last_error () !== JSON_ERROR_NONE ) {
4152 throw new self ("Unable to decode response: ' {$ body }'. " );
4253 }
4354
44- return $ object ;
55+ return $ error ;
4556 }
4657}
0 commit comments