Fixed ContentBased error handler not using the default content if accepted contents are not valid

This commit is contained in:
Alejandro Celaya 2016-07-29 11:25:53 +02:00
parent ab6aa99a6d
commit c569cef239

View File

@ -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
));
}
}