Skip to content

Commit cfbc7e0

Browse files
authored
Merge pull request #6 from postplanner/feature/return-raw-response
Return raw response
2 parents e4037c0 + 5e420ae commit cfbc7e0

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

src/Client.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -348,19 +348,31 @@ public function renewTokenFromRefreshToken($refreshToken = '')
348348
* @param \Psr\Http\Message\ResponseInterface $response
349349
*
350350
* @return array
351+
* @throws \LinkedIn\Exception
351352
*/
352353
public static function responseToArray($response)
353354
{
354355
$result = [];
355-
if ($contents = $response->getBody()->getContents()) {
356-
$result = \GuzzleHttp\json_decode(
357-
$contents,
358-
true
356+
$contents = '';
357+
try {
358+
if ($contents = $response->getBody()->getContents()) {
359+
$result = \GuzzleHttp\json_decode(
360+
$contents,
361+
true
362+
);
363+
} else if ($contents = $response->getHeaders()) {
364+
// Looks like when response body is empty the result might be in the headers.
365+
// Good job LinkedIn.
366+
$result = $contents;
367+
}
368+
} catch (\Exception $exception) {
369+
throw new Exception(
370+
'Malformed response contents.',
371+
'malformed-response-contents',
372+
$exception,
373+
$exception->getMessage(),
374+
$contents
359375
);
360-
} else if ($contents = $response->getHeaders()) {
361-
// Looks like when response body is empty the result might be in the headers.
362-
// Good job LinkedIn.
363-
$result = $contents;
364376
}
365377

366378
return $result;

src/Exception.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ class Exception extends \Exception
3131
*/
3232
protected $description;
3333

34+
/**
35+
* LinkedIn raw response
36+
*
37+
* @var string
38+
*/
39+
protected $rawResponse;
40+
41+
/**
42+
* LinkedIn code so we can use strings
43+
*
44+
* @var string
45+
*/
46+
protected $code;
47+
3448
/**
3549
* Exception constructor.
3650
* @param string $message
@@ -42,10 +56,14 @@ public function __construct(
4256
$message = "",
4357
$code = 0,
4458
$previousException = null,
45-
$description = ''
59+
$description = '',
60+
$rawResponse = ''
4661
) {
47-
parent::__construct($message, $code, $previousException);
62+
$this->code = $code;
63+
$parent_code = is_numeric($code) ? $code : 0;
64+
parent::__construct($message, $parent_code, $previousException);
4865
$this->description = $description;
66+
$this->rawResponse = $rawResponse;
4967
}
5068

5169
/**
@@ -58,6 +76,16 @@ public function getDescription()
5876
return $this->description;
5977
}
6078

79+
/**
80+
* Get raw LinkedIn response.
81+
*
82+
* @return string
83+
*/
84+
public function getRawResponse()
85+
{
86+
return $this->rawResponse;
87+
}
88+
6189
/**
6290
* @param RequestException $exception
6391
*
@@ -88,7 +116,7 @@ private static function extractErrorDescription($exception)
88116
$json = Client::responseToArray($response);
89117
if (isset($json['error_description'])) {
90118
return $json['error_description'];
91-
}
119+
}
92120
if (isset($json['message'])) {
93121
return $json['message'];
94122
}

0 commit comments

Comments
 (0)