@@ -188,7 +188,7 @@ public function setDefaultErrorHandler($handler): self
188188 * The callable signature MUST match the ErrorHandlerInterface
189189 *
190190 * Pass true to $handleSubclasses to make the handler handle all subclasses of
191- * the type as well.
191+ * the type as well. Pass an array of classes to make the same function handle multiple exceptions.
192192 *
193193 * @see \Slim\Interfaces\ErrorHandlerInterface
194194 *
@@ -201,19 +201,39 @@ public function setDefaultErrorHandler($handler): self
201201 * The callable MUST return an instance of
202202 * \Psr\Http\Message\ResponseInterface.
203203 *
204- * @param string $type Exception/Throwable name. ie: RuntimeException::class
204+ * @param string|string[] $typeOrTypes Exception/Throwable name.
205+ * ie: RuntimeException::class or an array of classes
206+ * ie: [HttpNotFoundException::class, HttpMethodNotAllowedException::class]
205207 * @param callable|ErrorHandlerInterface $handler
206208 * @param bool $handleSubclasses
207209 * @return self
208210 */
209- public function setErrorHandler (string $ type , $ handler , bool $ handleSubclasses = false ): self
211+ public function setErrorHandler ($ typeOrTypes , $ handler , bool $ handleSubclasses = false ): self
212+ {
213+ if (is_array ($ typeOrTypes )) {
214+ foreach ($ typeOrTypes as $ type ) {
215+ $ this ->addErrorHandler ($ type , $ handler , $ handleSubclasses );
216+ }
217+ } else {
218+ $ this ->addErrorHandler ($ typeOrTypes , $ handler , $ handleSubclasses );
219+ }
220+
221+ return $ this ;
222+ }
223+
224+ /**
225+ * Used internally to avoid code repetition when passing multiple exceptions to setErrorHandler().
226+ * @param string $type
227+ * @param callable|ErrorHandlerInterface $handler
228+ * @param bool $handleSubclasses
229+ * @return void
230+ */
231+ private function addErrorHandler (string $ type , $ handler , bool $ handleSubclasses ): void
210232 {
211233 if ($ handleSubclasses ) {
212234 $ this ->subClassHandlers [$ type ] = $ handler ;
213235 } else {
214236 $ this ->handlers [$ type ] = $ handler ;
215237 }
216-
217- return $ this ;
218238 }
219239}
0 commit comments