@@ -26,13 +26,17 @@ class ErrorHandler
2626 /** @var string */
2727 private $ internalErrorMessage ;
2828
29- public function __construct ($ internalErrorMessage = null , LoggerInterface $ logger = null )
29+ /** @var array */
30+ private $ exceptionMap ;
31+
32+ public function __construct ($ internalErrorMessage = null , LoggerInterface $ logger = null , array $ exceptionMap = [])
3033 {
3134 $ this ->logger = (null === $ logger ) ? new NullLogger () : $ logger ;
3235 if (empty ($ internalErrorMessage )) {
3336 $ internalErrorMessage = self ::DEFAULT_ERROR_MESSAGE ;
3437 }
3538 $ this ->internalErrorMessage = $ internalErrorMessage ;
39+ $ this ->exceptionMap = $ exceptionMap ;
3640 }
3741
3842 /**
@@ -54,7 +58,7 @@ protected function treatExceptions(array $errors, $throwRawException)
5458
5559 /** @var Error $error */
5660 foreach ($ errors as $ error ) {
57- $ rawException = $ error ->getPrevious ();
61+ $ rawException = $ this -> convertException ( $ error ->getPrevious () );
5862
5963 // Parse error or user error
6064 if (null === $ rawException ) {
@@ -129,4 +133,26 @@ public function handleErrors(ExecutionResult $executionResult, $throwRawExceptio
129133 $ executionResult ->extensions ['warnings ' ] = array_map (['GraphQL\Error ' , 'formatError ' ], $ exceptions ['extensions ' ]['warnings ' ]);
130134 }
131135 }
136+
137+ /**
138+ * Tries to convert a raw exception into a user warning or error
139+ * that is displayed to the user.
140+ *
141+ * @param \Exception $rawException
142+ * @return \Exception
143+ */
144+ protected function convertException (\Exception $ rawException = null )
145+ {
146+ if (null === $ rawException ) {
147+ return null ;
148+ }
149+
150+ if (!empty ($ this ->exceptionMap [get_class ($ rawException )])) {
151+ $ errorClass = $ this ->exceptionMap [get_class ($ rawException )];
152+
153+ return new $ errorClass ($ rawException ->getMessage (), $ rawException ->getCode (), $ rawException );
154+ }
155+
156+ return $ rawException ;
157+ }
132158}
0 commit comments