3

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?

3
  • You need to put them in to your globals.php laravel.com/docs/4.2/errors#handling-errors Commented Jan 30, 2015 at 5:19
  • That is laravel 4.2, my question is for the new laravel 5 Commented Feb 3, 2015 at 10:01
  • Yes. Taylor added Exception folder with Handler class in it where this can be done for Laravel 5. Official release for it is expected in the next few days. Commented Feb 3, 2015 at 15:22

2 Answers 2

1

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); } } 
Sign up to request clarification or add additional context in comments.

Comments

1

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); } } 

1 Comment

I think return a response it is better than just a die, example: return response()->view('errors.500', [], 500); if you want, create a custom view to all errors.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.