Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to respond with cusom HTTP status code in error handler #71

Open
USvER opened this issue Nov 6, 2013 · 6 comments
Open

How to respond with cusom HTTP status code in error handler #71

USvER opened this issue Nov 6, 2013 · 6 comments

Comments

@USvER
Copy link
Contributor

USvER commented Nov 6, 2013

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

@sheeep
Copy link
Contributor

sheeep commented Nov 6, 2013

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
Copy link
Contributor

sheeep commented Nov 8, 2013

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.

@USvER
Copy link
Contributor Author

USvER commented Nov 8, 2013

When i try

$response->setStatusCode(415); 

i'm getting this

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

@sheeep
Copy link
Contributor

sheeep commented Nov 12, 2013

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
Copy link
Contributor

sheeep commented Dec 18, 2013

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.

@stipic
Copy link

stipic commented Jul 19, 2019

@sheeep still nothing about this custom response?

@bytehead bytehead modified the milestones: 2.0, 4.x Nov 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants