emsifa / evo

Evolve your Laravel code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ResponseStatus doesn't set on error response from UseErrorResponse

emsifa opened this issue · comments

Controller:

#[UseErrorResponse(MyErrorResponse::class)]
class ExampleController extends Controller
{
    #[Post('do-something')]
    public function doSomething()
    {
        throw new CustomException("There is something wrong.");
    }

    #[Post('do-another-thing')]
    public function doAnotherThing()
    {
        throw new \RuntimeException("Whoops runtime error");
    }
}

MyErrorResponse.php

#[ResponseStatus(500)]
class MyErrorResponse extends JsonResponse
{
    public string $message;
}

CustomException.php

#[ResponseStatus(400)]
class CustomException extends Exception
{
}

In example above, when we call POST /do-something or POST /do-another-thing, it send response with status 200.

Expectation

When we call POST /do-something it should response status 400 since it throws CustomException that has ResponseStatus(400) attribute on it. And when we call POST /do-another-thing it should response status 500 because RuntimeException doesn't has ResponseStatus attribute on it, so it use the ResponseStatus(500) attached on MyErrorResponse class.