File tree Expand file tree Collapse file tree 2 files changed +37
-1
lines changed Expand file tree Collapse file tree 2 files changed +37
-1
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,11 @@ class ApiException extends UKFastException
10
10
11
11
protected $ response ;
12
12
13
+ /**
14
+ * @var string
15
+ */
16
+ protected $ requestId = null ;
17
+
13
18
public function __construct ($ response )
14
19
{
15
20
$ response ->getBody ()->rewind ();
@@ -32,6 +37,10 @@ public function __construct($response)
32
37
$ this ->message = $ message ;
33
38
}
34
39
40
+ if (!empty ($ response ->getHeader ('Request-ID ' )[0 ])) {
41
+ $ this ->requestId = $ response ->getHeader ('Request-ID ' )[0 ];
42
+ }
43
+
35
44
$ this ->response = $ response ;
36
45
}
37
46
@@ -59,6 +68,11 @@ public function getResponse()
59
68
return $ this ->response ;
60
69
}
61
70
71
+ public function getRequestId ()
72
+ {
73
+ return $ this ->requestId ;
74
+ }
75
+
62
76
private function getErrorsFromBody ($ body )
63
77
{
64
78
$ errors = [];
Original file line number Diff line number Diff line change @@ -14,7 +14,10 @@ class ApiExceptionTest extends TestCase
14
14
*/
15
15
public function constructs_from_standard_api_error ()
16
16
{
17
- $ response = new Response (500 , [], json_encode ([
17
+ $ headers = [
18
+ 'Request-ID ' => 'test-request-id ' ,
19
+ ];
20
+ $ response = new Response (500 , $ headers , json_encode ([
18
21
'errors ' => [
19
22
[
20
23
'detail ' => 'Test '
@@ -26,6 +29,7 @@ public function constructs_from_standard_api_error()
26
29
27
30
$ this ->assertEquals (1 , count ($ exception ->getErrors ()));
28
31
$ this ->assertEquals ('Test ' , $ exception ->getErrors ()[0 ]->detail );
32
+ $ this ->assertEquals ('test-request-id ' , $ exception ->getRequestId ());
29
33
}
30
34
31
35
/**
@@ -54,4 +58,22 @@ public function throws_invalid_json_exception_when_given_bad_json()
54
58
$ this ->expectException (InvalidJsonException::class);
55
59
$ exception = new ApiException ($ response );
56
60
}
61
+
62
+ /**
63
+ * @test
64
+ */
65
+ public function request_id_is_set_to_null_if_none_is_returned ()
66
+ {
67
+ $ response = new Response (500 , [], json_encode ([
68
+ 'errors ' => [
69
+ [
70
+ 'detail ' => 'Test '
71
+ ]
72
+ ]
73
+ ]));
74
+
75
+ $ exception = new ApiException ($ response );
76
+
77
+ $ this ->assertNull ($ exception ->getRequestId ());
78
+ }
57
79
}
You can’t perform that action at this time.
0 commit comments