From c569cef23912189bf1513f0ad55f83b77c20477b Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 29 Jul 2016 11:25:53 +0200 Subject: [PATCH] Fixed ContentBased error handler not using the default content if accepted contents are not valid --- .../src/Expressive/ContentBasedErrorHandler.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/module/Common/src/Expressive/ContentBasedErrorHandler.php b/module/Common/src/Expressive/ContentBasedErrorHandler.php index 60e1dfdb..7dbe2509 100644 --- a/module/Common/src/Expressive/ContentBasedErrorHandler.php +++ b/module/Common/src/Expressive/ContentBasedErrorHandler.php @@ -47,6 +47,7 @@ class ContentBasedErrorHandler extends AbstractPluginManager implements ErrorHan */ protected function resolveErrorHandlerFromAcceptHeader(Request $request) { + // Try to find an error handler for one of the accepted content types $accepts = $request->hasHeader('Accept') ? $request->getHeaderLine('Accept') : self::DEFAULT_CONTENT; $accepts = explode(',', $accepts); foreach ($accepts as $accept) { @@ -57,8 +58,17 @@ class ContentBasedErrorHandler extends AbstractPluginManager implements ErrorHan return $this->get($accept); } + // If it wasn't possible to find an error handler for accepted content type, use default one if registered + if ($this->has(self::DEFAULT_CONTENT)) { + return $this->get(self::DEFAULT_CONTENT); + } + + // It wasn't possible to find an error handler throw new InvalidArgumentException(sprintf( - 'It wasn\'t possible to find an error handler for ' + 'It wasn\'t possible to find an error handler for ["%s"] content types. ' + . 'Make sure you have registered at least the default "%s" content type', + implode('", "', $accepts), + self::DEFAULT_CONTENT )); } }