D
D
Denis2021-01-12 11:06:32
Exceptions
Denis, 2021-01-12 11:06:32

How to display custom error page for each exception?

I can't figure out the exceptions. But it should, because If I start doing it wrong, then I will have to correct it throughout the project. Here's what interests me: as I understand it, when we throw something, we get a 404.blade file in which I have an exception object and, in theory, I should display a message. But this, as I understand it, is already for the sale, that is, for the user to see only the message. HttpException works the same way (well, abort() is basically just a helper that does it). But when I throw InvalidArgumentException and others without http, they are always shown in laravel debug page with all necessary information.abort(404);

Accordingly, of course, I need the usual exceptions for debugging. I will be throwing them all over the project. But when the project goes into production, how to make it so that with InvalidArgumentException and others like it, a custom page is displayed. The problem is that when I turn off debug in .env, a page called 500.blade always comes out, no matter what code I pass in the parameters.

And so, In debug mode - everything is fine, in off debug - always page 500.blade. In both modes, it displays a template with the desired name and everything is fine. Let me summarize, which exceptions in which cases and how to throw them correctly, so that after switching debug to false, all exceptions become clear to the user?
throw new \Exception('qwe', 404);
throw new HttpException(404, 'message');

Answer the question

In order to leave comments, you need to log in

4 answer(s)
O
on_click, 2021-01-12
@on_click

https://laravel.demiart.ru/isklyucheniya-laravel-l... in my opinion a good article on the topic of
throw new \Exception on the prod will always give 500, it is possible to get around this somehow, but not necessary)

F
FanatPHP, 2021-01-12
@FanatPHP

500.blade is the user page . The only thing the user needs to see is an InvalidArgumentException error.
No other is needed

A
Alex Glebov, 2021-01-12
@SkiperX

throw new \Exception('qwe', 404);
in dev mode you get a detailed error page.
in prod, the user will see a beautiful 404/500 page, and in the logs there will be an error in detail.

D
Dmitry, 2021-01-22
@dlnsk

You can take advantage of the App\Exceptions\Handler global exception handler and map whatever you want to any exception.
You can read more in this free book:
https://github.com/adelf/acwa_book_en/blob/master/...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question