Skip to content

Add default render for the HttpException #40

@DenTray

Description

@DenTray

Description

What needs to be done?

Modify the initialization process by editing the bootstrap/app.php file. Specifically, inside the withExceptions call, add the following logic only if it has not already been added:

$exceptions->render(function (HttpException $exception, Request $request) {
    return ($request->expectsJson())
        ? response()->json(['error' => $exception->getMessage()], $exception->getStatusCode())
        : null;
});


This requires:

  • Detecting if this exception rendering logic is already registered to avoid duplicate calls.

  • Adding the logic conditionally only if it is not present.

  • Ensuring that the logic integrates smoothly with existing code in the bootstrap/app.php.

Expected Outcome

What is the expected result?

  • The withExceptions call in bootstrap/app.php should include the new rendering logic only once.

  • When a HttpException occurs and the request expects JSON, a JSON response with the error message and status code is returned.

  • No duplicate handlers or conflicts occur if the initialization runs multiple times.

Verification Scenarios

How can this be tested?

  1. Run the initialization process when the logic is not present; verify that the new exception rendering logic is added to bootstrap/app.php.

  2. Run the initialization process again; verify that the logic is not duplicated.

  3. Trigger an HTTP exception with a request that expects JSON and verify the response is JSON formatted with the error message and status code.

  4. Trigger an HTTP exception with a request that does not expect JSON and verify the handler returns null (allowing normal exception handling).

  5. Review the bootstrap/app.php file to ensure no duplicate exception handlers are present.

  6. Confirm no errors or debug information appear in the application logs or console.


Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions