forked from Brunojoe1994/-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphQLException.php
More file actions
executable file
·112 lines (103 loc) · 3.57 KB
/
GraphQLException.php
File metadata and controls
executable file
·112 lines (103 loc) · 3.57 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
declare(strict_types=1);
namespace Strata\Data\Exception;
use Symfony\Contracts\HttpClient\ResponseInterface;
class GraphQLException extends HttpException
{
private string $lastQuery;
/**
* HttpException
*
* Outputs key HTTP request and response data with exception, data can also be accessed via getters
*
* @param $message
* @param string $uri
* @param string $method
* @param array $options
* @param ResponseInterface $response
* @param array $errorData
* @param array $responseData
* @param ?string $lastQuery
* @param \Exception|null $previous
*/
public function __construct(string $message, string $uri, string $method, array $options, ResponseInterface $response, array $errorData = [], array $responseData = [], \Exception $previous = null, ?string $lastQuery = null)
{
if (null !== $lastQuery) {
$this->setLastQuery($lastQuery);
}
parent::__construct($message, $uri, $method, $options, $response, $errorData, $responseData, $previous);
}
/**
* @return string
*/
public function getLastQuery(): string
{
return $this->lastQuery;
}
/**
* @param string $lastQuery
*/
public function setLastQuery(string $lastQuery): void
{
$this->lastQuery = $lastQuery;
}
/**
* Return exception message from error data
*
* @param array $errorData
* @return string
*/
public function getMessageFromErrorData(array $errorData): string
{
$errorContent = [];
foreach ($errorData as $error) {
if (!isset($error['message'])) {
continue;
}
$content = $error['message'];
if (isset($error['locations'])) {
foreach ($error['locations'] as $location) {
$content = rtrim($content, '.');
$content .= sprintf(' on line %d, column %d', $location['line'], $location['column']);
}
}
if (isset($error['path']) && is_array($error['path'])) {
$content .= sprintf(', path: %s', implode(' > ', $error['path']));
}
$errorContent[] = $content;
}
if (count($errorContent) > 0) {
return ' GraphQL errors: ' . implode('. ', $errorContent) . '.';
}
return '';
}
/**
* Return errors formatted as an expanded multiline string
*
* @param array $errors
* @return string
* @see http://spec.graphql.org/draft/#sec-Errors
*/
public function expandErrorValues(array $errors): string
{
$content = PHP_EOL;
foreach ($errors as $error) {
if (!isset($error['message'])) {
continue;
}
$content .= 'GraphQL error: ' . $error['message'] . PHP_EOL;
if (isset($error['locations'])) {
foreach ($error['locations'] as $location) {
$content .= sprintf(self::INDENT . 'Location: line %d, column %d', $location['line'], $location['column']) . PHP_EOL;
}
}
if (isset($error['path']) && is_array($error['path'])) {
$content .= self::INDENT . sprintf('Path: %s', implode(' > ', $error['path']));
}
if (isset($error['extensions']) && is_array($error['extensions'])) {
$content .= self::INDENT . 'Extensions: ' . $this->expandArrayValues($error['extensions'], self::INDENT . self::INDENT);
}
}
return $content . PHP_EOL;
}
}