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

Allow customizing exception handling behavior #625

Closed
lucasromanojf opened this issue Feb 19, 2020 · 3 comments
Closed

Allow customizing exception handling behavior #625

lucasromanojf opened this issue Feb 19, 2020 · 3 comments

Comments

@lucasromanojf
Copy link
Contributor

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

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

lucasromanojf commented Mar 15, 2020

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

This functionality has been addressed in this PR and will be available for the next tagged release: #1146

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants