roots/acorn

Bug: Routes incompatible with ValidationException

retlehs opened this issue · 3 comments

Acorn's route handling is incompatible with ValidationException because it throws a 422

if (! $response->isServerError() && $response->status() >= 400) {
return;
}

Steps To Reproduce

  1. Enable Acorn's routing
  2. Add a route that will return a 422:
Route::get('/foo', function () {
    try {
        throw new \Illuminate\Validation\ValidationException(
            validator(data: [])
        );
    } catch (\Illuminate\Validation\ValidationException $exception) {
        return response()->json(data: [
            'message' => $exception->getMessage(),
            'errors' => $exception->errors(),
        ], status: $exception->status); // if you change status code to 200, it will work. Otherwise, it will be handled by WordPress
    }
});

h/t @thebezzo

I'm also hitting this problem when trying to return a "400 Bad Request" from a route.
Is there any reason this can't just be fixed by removing the server error and status code check from the Bootloader and just letting the response be rendered like any other? Seems somewhat nonsensical to ignore some types of responses like this.

I'd be up for doing a PR for this, if there are no objections

I looks like this was introduced by @QWp6t in the original illuminate/routing PR. Any thoughts on this @QWp6t?

Feel free to test and push up a PR!