Chris
Chris

Reputation: 58322

How to set a 'plain' exception handler in Laravel 4

In Laravel 4.1 a plain exception is shown when an exception in thrown and debug is set to false.

The message is along the lines of:

Whoops, something has gone wrong

I've trawled through the code, and it seems that the plain exception handler is registered in:

Illuminate\Exception\ExceptionServiceProvider.php

via the following line

protected function registerHandler()
{
    $this->app['exception'] = $this->app->share(function($app)
    {
        return new Handler($app, $app['exception.plain'], $app['exception.debug']);
    });
}

Where do I set my own custom plain handler? I don't like the "Whoops" message, I want to be able to show a site specific message

Cheers

Upvotes: 2

Views: 1656

Answers (2)

The Alpha
The Alpha

Reputation: 146269

This is the plain exception handler;, already available in app/start/global.php by default, modify it as given below (Btw, Whoops! PHP Errors only for Cool Kids):

App::error(function(Exception $exception)
{
    Log::error($exception);Log::error($exception->getMessage());
    return View::make('errors.index')->with('exception', $exception);
});

Create a view view/errors/index.blade.php

@extends('layouts.master')

@section('content')

    <div class="page-header">
        <h1>Oops!</h1>
    </div>

    <div class='well'>ERROR: {{ $exception->getMessage() }}</div>

@stop

Also make 'debug' => false in your app/config/app.php file:

/*
|--------------------------------------------------------------------------
| Application Debug Mode
|--------------------------------------------------------------------------
|
| When your application is in debug mode, detailed error messages with
| stack traces will be shown on every error that occurs within your
| application. If disabled, a simple generic error page is shown.
|
*/

'debug' => false,

You have following methods to use in $exception object:

array (size=10)
  //0 => string '__construct' (length=11)
  1 => string 'getSeverity' (length=11)
  2 => string 'getMessage' (length=10)
  3 => string 'getCode' (length=7)
  4 => string 'getFile' (length=7)
  5 => string 'getLine' (length=7)
  6 => string 'getTrace' (length=8)
  7 => string 'getPrevious' (length=11)
  8 => string 'getTraceAsString' (length=16)
  //9 => string '__toString' (length=10)

If you leave 'debug' => true, then still your exception handler will work but in some cases it may display whoops when the exception is not caught in you handler but in another specific handler before your generic Exception handler.

Also remember that, Exception class is the most generic exception type, if you have other more specific exception handlers defined after that then it'll not get triggered if any response returned from that specific handler.

Upvotes: 4

Razor
Razor

Reputation: 9855

In your app/start/global.php file add

App::missing(function($exception)
{
    return 'Your custom message'; // you can also specify a view to render.
});

Please visit http://laravel.com/docs/errors for more information

Upvotes: 1

Related Questions