OneupUploaderBundle
OneupUploaderBundle copied to clipboard
How to respond with cusom HTTP status code in error handler
Is it possible to return custom HTTP status code to the client? I want to respond with error code 415(Unsupported Media Type)
Create a custom Exception and inherit from ValidationException. Afterwards create and register your own ErrorHandler where you type-check for your own Exception and modify the passed response object accordingly. Something like this:
<?php
namespace Acme\DemoBundle\ErrorHandler;
use Exception;
use Oneup\UploaderBundle\Uploader\ErrorHandler\ErrorHandlerInterface;
use Oneup\UploaderBundle\Uploader\Response\AbstractResponse;
use Your\Bundle\Upload\Exception\InvalidTypeException;
class CustomErrorHandler implements ErrorHandlerInterface
{
public function addException(AbstractResponse $response, Exception $exception)
{
if ($exception instanceof InvalidTypeException) {
$response->setStatusCode(415);
}
}
}
Uhm. I don't think that it is necessary to inherit from ValidationException, but let me double check that.
Uhm. I don't think that it is necessary to inherit from ValidationException, but let me double check that.
No need to inherit from ValidationException. Just create a custom one extending from \Extension
.
When i try
$response->setStatusCode(415);
i'm getting this
FatalErrorException: Error: Call to undefined method Oneup\UploaderBundle\Uploader\Response\EmptyResponse::setStatusCode()
You're right. EmptyResponse
does not yet inherit from Symfonys Response
object which IMHO should be the case, for exactly such use cases. I'll give it a shot as soon as possible!
To fix this issue, a lions share of the response handling has to be refactored, what implicates bc-breaks. Will be changed and implemented either in 1.1 or 2.0.
@sheeep still nothing about this custom response?