livewire / livewire

A full-stack framework for Laravel that takes the pain out of building dynamic UIs.
MIT License
22.36k stars 1.56k forks source link

Allow customizing exception handling behavior #625

Closed lucasromanojf closed 4 years ago

lucasromanojf commented 4 years ago

Is your feature request related to a problem? Please describe. Currently, when an exception happens in a Livewire request, a modal is displayed with the response HTML. There is no (documented) way of overriding/customizing this.

Describe the solution you'd like The goal of this ticket is to request a (official/documented) way of customizing the exception handling behavior in frontend.

Describe alternatives you've considered For now, we are overriding the showHtmlModal function, like this:

window.livewire.connection.driver.showHtmlModal = function(response) {
    const json = JSON.parse(response);
    // Handle JSON response...
    // E.g.: alert(response.error);
};

And in the Laravel's Handler.php class, inside the render method, we are doing something like this:

$response = parent::render($request, $exception);
if ($response->getStatusCode() == 500) {
    if ($request->header('X-Livewire')) {
        return response()->json(['success' => false, 'status' => 500, 'error' => 'Internal server error'], 500);
    }
}
if ($response->getStatusCode() == 404) {
    if ($request->header('X-Livewire')) {
        return response()->json(['success' => false, 'status' => 404, 'error' => 'Page or resource not found'], 404);
    }
}
return $response;

For now this works, but maybe there is a better and more elegant solution.

Additional context I am VERY involved with Livewire project and I want it to be great (more than it already is)! I am available for helping with anything you need.

calebporzio commented 4 years ago

What specifically do you want to be able to do or show instead of the modal? I could see the need for something like this, but I'd like to talk more about your specific needs.

lucasromanojf commented 4 years ago

What specifically do you want to be able to do or show instead of the modal? I could see the need for something like this, but I'd like to talk more about your specific needs.

Hey @calebporzio for example, I am showing, instead of the default HTML modal, a Tailwind UI alert. Other people may want to just show a default Javascript alert, I don't know. This can be easily achieved by overriding the showHtmlModal function as shown above, but maybe you will want do do this customization in another way (maybe something like window.livewire.on('error', (error) => {}) that overrides the showHtmlModal function) or, if not, just document the possibility of overriding showHtmlModal function. I can help with a PR for the decision whatever it will be, just wanted to discuss with you the better solution first.

calebporzio commented 4 years ago

This functionality has been addressed in this PR and will be available for the next tagged release: https://github.com/livewire/livewire/pull/1146