forked from microsoftgraph/msgraph-sdk-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionWrapper.php
More file actions
77 lines (66 loc) · 2.3 KB
/
Copy pathExceptionWrapper.php
File metadata and controls
77 lines (66 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/**
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* Licensed under the MIT License. See License in the project root
* for license information.
*
* ExceptionWrapper File
* PHP version 7
*
* @category Library
* @package Microsoft.Graph
* @copyright 2016 Microsoft Corporation
* @license https://opensource.org/licenses/MIT MIT License
* @version GIT: 0.1.0
* @link https://graph.microsoft.io/
*/
namespace Microsoft\Graph\Core;
use GuzzleHttp\Exception\BadResponseException;
/**
* Class ExceptionWrapper
*
* @category Library
* @package Microsoft.Graph
* @license https://opensource.org/licenses/MIT MIT License
* @link https://graph.microsoft.io/
*/
class ExceptionWrapper
{
/**
* Wrap Guzzle BadResponseException which returns truncated exception messages for 4xx and 5xx responses.
* Adds response body to the exception message.
*
* @param BadResponseException $ex
* @return BadResponseException containing HTTP response from Graph API
*/
public static function wrapGuzzleBadResponseException(BadResponseException $ex)
{
$response = $ex->getResponse();
// Safety check for Guzzle < 7.0
if (!$response) {
return $ex;
}
/** @see \GuzzleHttp\Exception\RequestException::create() */
if (preg_match('/^(.+: `.+ .+` resulted in a `.+ .+` response):\n/U', $ex->getMessage(), $match)) {
$message = $match[1];
$body = $response->getBody();
if (!$body->isSeekable() || !$body->isReadable()) {
return $ex;
}
$summary = $body->getContents();
$body->rewind();
if ($summary !== '') {
$message .= ":\n{$summary}\n";
//return new $ex($message, $ex->getRequest(), $ex->getResponse(), $ex, $ex->getHandlerContext());
// Better: modify internal message inside original exception object (preserves the stack trace)
(new class() extends \Exception {
public static function overwriteProtectedMessage(\Exception $ex, $message)
{
$ex->message = $message;
}
})::overwriteProtectedMessage($ex, $message);
}
}
return $ex;
}
}