Jordan Dobrev
Jordan Dobrev

Reputation: 367

Laravel 5 Error Handling

I am using Laravel 5 and I am trying to make custom 404 page and custom Exception handling, but I can't figure out where to put my code. Some time ago there was an ErrorServiceProvider that no longer exists. Can anyone give me some pointers?

EDIT: I saw they have added a Handler class in the App/Exception folder but that still seems not the right place to put it because it does not follow at all the laravel 4.2 App::error, App::missing and App::fatal methods. Anyone has any ideas?

Upvotes: 3

Views: 5845

Answers (2)

unliu
unliu

Reputation: 11

Here is how to customize your error page while complying with the APP_DEBUG setting in .env.

app/Exceptions/Handler.php

public function render($request, Exception $e)
{
    if ($this->isHttpException($e))
    {
        return $this->renderHttpException($e);
    }
    else
    {
        if (env('APP_DEBUG'))
        {
            return parent::render($request, $e);
        }
        return response()->view('errors.500', [], 500);
    }
}

Upvotes: 1

Jordan Dobrev
Jordan Dobrev

Reputation: 367

Use app/Exceptions/Handler.php render method to achieve that. L5 documentation http://laravel.com/docs/5.0/errors#handling-errors

public function render($request, Exception $e)
{
    if ($e instanceof Error) {
        if ($request->ajax()) {
            return response(['error' => $e->getMessage()], 400);
        } else {
            return $e->getMessage();
        }
    }

    if ($this->isHttpException($e)) {
        return $this->renderHttpException($e);
    } else {
        return parent::render($request, $e);
    }
}

Upvotes: 1

Related Questions