Showing posts with label: coding. Show all posts.

Error Handling in Laravel

Friday 29 September 2017

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

Django

Wednesday 27 September 2017

I have really been loving Django lately, and I wrote another version of this site in it. That site is skooch.com. The new site uses the same database as this one, so the only real difference is the language they are written in.

I find Python to be a much more opinionated and formal language than PHP, which makes it a steeper learning curve, but it forces you to think things through a bit more. I find the extra effort to be well worth it in the end as far as code quality goes. 

As a note the Python code was significantly shorter than the same code in PHP, for whatever that is worth.

 

Labels: coding, python, django
No comments

ReCaptcha

Sunday 24 September 2017

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

PHP Imap

Wednesday 06 September 2017

If you get an error trying to connect with php_imap for Kerberos:

PHP Notice:  Unknown: Kerberos error: No credentials cache found (try running kinit) for imap.example.com (errflg=1) in Unknown on line 0

This is the solution:

Pass ['DISABLE_AUTHENTICATOR' => 'GSSAPI'] to imap_open for the last options parameter.

Labels: coding, php2
No comments

Python mysqlclient for Django on Windows

Wednesday 06 September 2017

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