S
S
Sergei2016-06-26 12:08:34
Laravel
Sergei, 2016-06-26 12:08:34

How do you organize error output in laravel?

Hello. Recently I started to master laravel and I was faced with the question of how best to organize error output.
At the moment, I tried to organize it as follows:
1. I slightly expanded the basic Exception Handler and in the render method I see what type of request led to the error:

public function render($request, Exception $e)
    {
        $statusCode = $this->getStatusCode($e);

        if ($request->wantsJson()) {
            return response()->json(['message' => $e->getMessage()], $statusCode);
        }

        if (!config('app.debug')) {
            return parent::render($request, $e);
        }

        return response()->view('errors', ['error' => $e->getMessage()], 500);
    }

If everything is more or less normal with ajax requests, I catch the error text in error, and then display a "pop-up" (although it still makes me want to repeat the same thing in each ajax method, and it seems to me that this can somehow be made more beautiful)
)
 .error(function(data) {
    $.notify(data.message, "error");
});

, then with errors when loading pages, everything is not so smooth. I tried to use flash messages by adding a block with errors like this to the base template:
@section('flash')
                @if(Session::has('flash_success'))
                    <div class="alertFlash alert alert-success">{!! session('flash_success') !!}</div>
                @endif
                @if(Session::has('flash_warning'))
                    <div class="alertFlash alert alert-warning">{!! session('flash_warning') !!}</div>
                @endif
                @if(Session::has('flash_error'))
                    <div class="alertFlash alert alert-danger">{!! session('flash_error') !!}</div>
                @endif
            @show

, but as for me, somehow this is not an option and I would like all errors to be of the same type, for example, using js pop-ups through the $ notify call. I would like some kind of
universal solution, or at least quite elegant ... page and displaying the text of the error instead of the requested one... And what if the error is not critical, but I would like to display it anyway and open the page + even with errors that will not be visible in production.
abort(500, 'Текст ошибки);
Actually, somehow I got confused in the desire to beautifully organize the output of errors, and I could not find the norms of examples in projects on github / articles. I will be glad to see any suggestions, links to articles / turnips. Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tridcatij, 2016-06-30
@tridcatij

I throw all the necessary information into the log (by category). Viewing the log brought to the admin panel, viewing through this pribluda:
Laravel 5 log viewer

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question