forked from magento/graphql-ce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorHandler.php
More file actions
102 lines (91 loc) · 2.65 KB
/
ErrorHandler.php
File metadata and controls
102 lines (91 loc) · 2.65 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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Framework\GraphQl\Query;
/**
* Class ErrorHandler
*
* @package Magento\Framework\GraphQl\Query
*/
class ErrorHandler implements ErrorHandlerInterface
{
/**
* @var \Magento\Framework\Logger\Monolog
*/
private $clientLogger;
/**
* @var \Magento\Framework\Logger\Monolog
*/
private $serverLogger;
/**
* @var array
*/
private $clientErrorCategories;
/**
* @var array
*/
private $serverErrorCategories;
/**
* @var \Magento\Framework\Logger\Monolog
*/
private $generalLogger;
/**
* ErrorHandler constructor.
*
* @param \Magento\Framework\Logger\Monolog $clientLogger
* @param \Magento\Framework\Logger\Monolog $serverLogger
* @param \Magento\Framework\Logger\Monolog $generalLogger
* @param array $clientErrorCategories
* @param array $serverErrorCategories
*
* @SuppressWarnings(PHPMD.LongVariable)
*/
public function __construct(
\Magento\Framework\Logger\Monolog $clientLogger,
\Magento\Framework\Logger\Monolog $serverLogger,
\Magento\Framework\Logger\Monolog $generalLogger,
array $clientErrorCategories = [],
array $serverErrorCategories = []
) {
$this->clientLogger = $clientLogger;
$this->serverLogger = $serverLogger;
$this->generalLogger = $generalLogger;
$this->clientErrorCategories = $clientErrorCategories;
$this->serverErrorCategories = $serverErrorCategories;
}
/**
* Handle errors
*
* @param \GraphQL\Error\Error[] $errors
* @param callable $formatter
*
* @return array
*/
public function handle(array $errors, callable $formatter):array
{
return array_map(
function (\GraphQL\Error\ClientAware $error) use ($formatter) {
$this->logError($error);
return $formatter($error);
},
$errors
);
}
/**
* @param \GraphQL\Error\ClientAware $error
*
* @return boolean
*/
private function logError(\GraphQL\Error\ClientAware $error):bool
{
if (in_array($error->getCategory(), $this->clientErrorCategories)) {
return $this->clientLogger->error($error);
} elseif (in_array($error->getCategory(), $this->serverErrorCategories)) {
return $this->serverLogger->error($error);
}
return $this->generalLogger->error($error);
}
}