22
33namespace Overblog \GraphQLBundle \Error ;
44
5+ use GraphQL \Error \ClientAware ;
6+ use GraphQL \Error \Debug ;
57use GraphQL \Error \Error as GraphQLError ;
68use GraphQL \Error \FormattedError ;
79use GraphQL \Error \UserError as GraphQLUserError ;
810use GraphQL \Executor \ExecutionResult ;
9- use Psr \ Log \ LoggerInterface ;
10- use Psr \ Log \ LogLevel ;
11- use Psr \ Log \ NullLogger ;
11+ use Overblog \ GraphQLBundle \ Event \ ErrorFormattingEvent ;
12+ use Overblog \ GraphQLBundle \ Event \ Events ;
13+ use Symfony \ Component \ EventDispatcher \ EventDispatcherInterface ;
1214
1315class ErrorHandler
1416{
1517 const DEFAULT_ERROR_MESSAGE = 'Internal server Error ' ;
1618 const DEFAULT_USER_WARNING_CLASS = UserWarning::class;
1719 const DEFAULT_USER_ERROR_CLASS = UserError::class;
18- /** callable */
19- const DEFAULT_ERROR_FORMATTER = [FormattedError::class, 'createFromException ' ];
2020
21- /** @var LoggerInterface */
22- private $ logger ;
21+ /** @var EventDispatcherInterface */
22+ private $ dispatcher ;
2323
2424 /** @var string */
2525 private $ internalErrorMessage ;
@@ -33,19 +33,16 @@ class ErrorHandler
3333 /** @var string */
3434 private $ userErrorClass = self ::DEFAULT_USER_ERROR_CLASS ;
3535
36- /** @var callable|null */
37- private $ errorFormatter ;
38-
3936 /** @var bool */
4037 private $ mapExceptionsToParent ;
4138
4239 public function __construct (
40+ EventDispatcherInterface $ dispatcher ,
4341 $ internalErrorMessage = null ,
44- LoggerInterface $ logger = null ,
4542 array $ exceptionMap = [],
4643 $ mapExceptionsToParent = false
4744 ) {
48- $ this ->logger = ( null === $ logger ) ? new NullLogger () : $ logger ;
45+ $ this ->dispatcher = $ dispatcher ;
4946 if (empty ($ internalErrorMessage )) {
5047 $ internalErrorMessage = self ::DEFAULT_ERROR_MESSAGE ;
5148 }
@@ -68,43 +65,32 @@ public function setUserErrorClass($userErrorClass)
6865 return $ this ;
6966 }
7067
71- public function setErrorFormatter (callable $ errorFormatter = null )
72- {
73- $ this ->errorFormatter = $ errorFormatter ;
74-
75- return $ this ;
76- }
77-
78- /**
79- * @param \Exception|\Error $exception
80- * @param string $errorLevel
81- */
82- public function logException ($ exception , $ errorLevel = LogLevel::ERROR )
83- {
84- $ message = sprintf (
85- '%s: %s[%d] (caught exception) at %s line %s. ' ,
86- get_class ($ exception ),
87- $ exception ->getMessage (),
88- $ exception ->getCode (),
89- $ exception ->getFile (),
90- $ exception ->getLine ()
91- );
92-
93- $ this ->logger ->$ errorLevel ($ message , ['exception ' => $ exception ]);
94- }
95-
96- public function handleErrors (ExecutionResult $ executionResult , $ throwRawException = false )
68+ public function handleErrors (ExecutionResult $ executionResult , $ throwRawException = false , $ debug = false )
9769 {
98- $ errorFormatter = $ this ->errorFormatter ? $ this -> errorFormatter : self :: DEFAULT_ERROR_FORMATTER ;
70+ $ errorFormatter = $ this ->createErrorFormatter ( $ debug ) ;
9971 $ executionResult ->setErrorFormatter ($ errorFormatter );
100- FormattedError::setInternalErrorMessage ($ this ->internalErrorMessage );
10172 $ exceptions = $ this ->treatExceptions ($ executionResult ->errors , $ throwRawException );
10273 $ executionResult ->errors = $ exceptions ['errors ' ];
10374 if (!empty ($ exceptions ['extensions ' ]['warnings ' ])) {
10475 $ executionResult ->extensions ['warnings ' ] = array_map ($ errorFormatter , $ exceptions ['extensions ' ]['warnings ' ]);
10576 }
10677 }
10778
79+ private function createErrorFormatter ($ debug = false )
80+ {
81+ $ debugMode = false ;
82+ if ($ debug ) {
83+ $ debugMode = Debug::INCLUDE_TRACE | Debug::INCLUDE_DEBUG_MESSAGE ;
84+ }
85+
86+ return function (GraphQLError $ error ) use ($ debugMode ) {
87+ $ event = new ErrorFormattingEvent ($ error , FormattedError::createFromException ($ error , $ debugMode , $ this ->internalErrorMessage ));
88+ $ this ->dispatcher ->dispatch (Events::ERROR_FORMATTING , $ event );
89+
90+ return $ event ->getFormattedError ()->getArrayCopy ();
91+ };
92+ }
93+
10894 /**
10995 * @param GraphQLError[] $errors
11096 * @param bool $throwRawException
@@ -145,18 +131,12 @@ private function treatExceptions(array $errors, $throwRawException)
145131 // user error
146132 if ($ rawException instanceof $ this ->userErrorClass ) {
147133 $ treatedExceptions ['errors ' ][] = $ errorWithConvertedException ;
148- if ($ rawException ->getPrevious ()) {
149- $ this ->logException ($ rawException ->getPrevious ());
150- }
151134 continue ;
152135 }
153136
154137 // user warning
155138 if ($ rawException instanceof $ this ->userWarningClass ) {
156139 $ treatedExceptions ['extensions ' ]['warnings ' ][] = $ errorWithConvertedException ;
157- if ($ rawException ->getPrevious ()) {
158- $ this ->logException ($ rawException ->getPrevious (), LogLevel::WARNING );
159- }
160140 continue ;
161141 }
162142
@@ -165,8 +145,6 @@ private function treatExceptions(array $errors, $throwRawException)
165145 throw $ rawException ;
166146 }
167147
168- $ this ->logException ($ rawException , LogLevel::CRITICAL );
169-
170148 $ treatedExceptions ['errors ' ][] = $ errorWithConvertedException ;
171149 }
172150
@@ -208,8 +186,8 @@ private function flattenErrors(array $errors)
208186 */
209187 private function convertException ($ rawException = null )
210188 {
211- if (null === $ rawException ) {
212- return ;
189+ if (null === $ rawException || $ rawException instanceof ClientAware ) {
190+ return $ rawException ;
213191 }
214192
215193 $ errorClass = $ this ->findErrorClass ($ rawException );
0 commit comments