OneupUploaderBundle icon indicating copy to clipboard operation
OneupUploaderBundle copied to clipboard

How to respond with cusom HTTP status code in error handler

Open USvER opened this issue 11 years ago • 6 comments

Is it possible to return custom HTTP status code to the client? I want to respond with error code 415(Unsupported Media Type)

USvER avatar Nov 06 '13 17:11 USvER

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.

sheeep avatar Nov 06 '13 18:11 sheeep

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.

sheeep avatar Nov 08 '13 11:11 sheeep

When i try

$response->setStatusCode(415); 

i'm getting this

FatalErrorException: Error: Call to undefined method Oneup\UploaderBundle\Uploader\Response\EmptyResponse::setStatusCode()

USvER avatar Nov 08 '13 19:11 USvER

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!

sheeep avatar Nov 12 '13 08:11 sheeep

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 avatar Dec 18 '13 11:12 sheeep

@sheeep still nothing about this custom response?

stipic avatar Jul 19 '19 11:07 stipic