Josh Hallow
Josh Hallow

Reputation: 349

Show error page in Laravel?

Before I get into the question, I'll give you a brief introduction into what I want to do. I want to be able to show a friend;y view to users when a error happens in my Laravel project but unfortunately I don't know how to do this and was hoping some of you may be able to help me?

Hello. Today I am asking a question about Laravel. For a long time I have wanted to log EVERY single error in my laravel project. Now some of you may say that its easy, just overwrite the handle() method in the App\Exceptions\Handler class.

While that's relatively easy to do, it doesn't handle EVERY single error. I have noticed it doesn't handle Query exceptions.

What I am asking is how can I handle EVERY exception. What I mainly want to do is log the error in a database table, and disable a nice view to the end user. Then if I am the one, or another member of my team that receives the error, they can easily check in the database without having to show any critical details to the end user.

I followed this tutorial but it's the same, it doesn't log every single exception. http://blog.sarav.co/registering-custom-exception-handler-laravel-5/

Upvotes: 1

Views: 1617

Answers (2)

The Alpha
The Alpha

Reputation: 146191

You can simply add some code in the App\Exceptions\Handler::render method, for example:

public function render($request, Exception $exception)
{
    if($exception instanceof \Illuminate\Database\QueryException) {
        // Do something with the $exception
        // dd($exception); // analyze it
        // return a response/redirect
    }

    return parent::render($request, $exception);
}

In this $exception instance, you can find many useful information using following methods (Omit magic methods, it's a dump of get_class_methods):

array:12 [▼
  0 => "__construct"
  1 => "getSql"
  2 => "getBindings"
  3 => "__wakeup"
  4 => "getMessage"
  5 => "getCode"
  6 => "getFile"
  7 => "getLine"
  8 => "getTrace"
  9 => "getPrevious"
  10 => "getTraceAsString"
  11 => "__toString"
]

Also, the property $exception->errorInfo will give you some information as well, for example (One particular errorInfo):

errorInfo: array:3 [▼
    0 => "42S22"
    1 => 1054
    2 => "Unknown column 'ids' in 'field list'"
]

Note: FYI, error and exception are two different things so the set_error_handler and the set_exception_handler both are for different reasons, an error handler will not catch an exception.

Upvotes: 1

NateShumate
NateShumate

Reputation: 302

Use set_error_handler to set custom exception. Or you could use log4php they have a package for laravel I believe.

Upvotes: 0

Related Questions