Error Handling in Laravel

Sept. 29, 2017, 12:10 p.m.

For a Laravel application I am working on the need arose to log errors to the database rather than to log files. I also wanted to show a custom error page instead of Laravel's default error page. This is my solution:

In app/Exceptions/Handler.php:

public function report(Exception $exception)
{
    if(!config('app.debug')) {
        if ($this->shouldReport($exception)) {
            $this->logError($exception);
        }
    }
    if(env('APP_ENV') == 'local'){
        parent::report($exception);
    }
}

public function render($request, Exception $exception)
{
    if(!config('app.debug')) {
        if($this->shouldReport($exception)){
            return response()->view('errors.500', compact('exception'));
        }
    }
    return parent::render($request, $exception);
}

Function report checks to see if APP_ENV is set to local, if so it displays the error as normal. If it is not local it calls function logError which writes the error to the database. 

Function render also checks APP_DEBUG and if it is set to true it reports the error to the screen as normal. If it is set to false it returns a custom error template which is stored in resources/views/error/500.blade.php.

 

Labels: coding , laravel , php2

No comments

ReCaptcha

Sept. 24, 2017, 5 p.m.

I just added a Captcha to the registration and contact me pages due to large amounts of spam mail and what seem to be fake registrations. Luckily Google's ReCaptcha is easy to use.

To integrate with Laravel I used greggilbet/recaptcha which I have used for other projects and which is amazingly easy to use. If anyone needs a Captcha I highly recommend it.

It does get annoying when you are asked to keep clicking on images instead of just typing in numbers, but that only happens if you continually try to submit the same form, which means that it's working. To avoid the endless clicking in development I usually add the rule to the validation conditionally based on the env('APP_ENV') variable so that it is only added in production.

Labels: coding , spam

No comments

Blue Friday

Sept. 8, 2017, 4:39 p.m.

My new song, very loosely inspired by New Order's "Blue Monday." Written and produced by me, made with Ableton.

Labels: music

No comments

Python mysqlclient for Django on Windows

Sept. 6, 2017, 10:44 a.m.

I had been using SQLLite with Django for quite some time because I couldn't get mysqlclient for windows to install properly with pip. SQLLite was fine for local development, but before I deploy an app I wanted to get MySQL working.

It turns out it was very easy:

pip install mysqlclient==1.3.9

That's all I need to do! I had tried downloading wheels and all sorts of other stuff, none of which worked, but version 1.3.9 installs fine with no errors on Windows 10.

Labels: coding , python , django

No comments

Archives