AdonisJS: Custom Error Handling.

Here I am again folks, back with a random story on AdonisJS to which you might find useful… Read on ;-).

I’ll keep this one short because I have quite a habit on typing and typing and even more typing. ← See, I’m already getting carried away now… I don’t know about you, but sometimes I find Error Handling in general a bit of a pain in the ass, simply because anything can happen and you’ve got to work out what on earth went wrong in the first place. We don’t get on…

One thing in particular I noticed was that upon switching the environment from Development to Production within my .env file I noticed that if I (deliberately) typed in a wrong URL into the browser it wouldn’t load. “Hmm, what’s going on here…?” I thought. I shrugged and just thought maybe my browser was playing about again (It’s like Russian roulette with Microsoft Edge sometimes), so I resorted to Mozilla Firefox. “S******g hell…”. Surely you can guess what happened next. It came up right? Nope. It just sat there. Don’t worry there is a happy ending so no need to panic haha. I’m about to show you how you (yes you) can handle specific errors.

Some very detailed info on Error Handling.

There is some great information about Error Handling on the AdonisJS docs. Here is the code used to catch exceptions (taken from AdonisJS website):

Http.handleError = function * (error, request, response) {
if (error.name === 'ModelNotFoundException') {
yield response.status(404).sendView('404')
return
}
  if (error.name === 'PasswordMisMatch') {
response.status(400).send('Invalid credentials')
return
}
  response.status(error.status).send(error.message)
}

I’ll translate this block. If a page wasn’t found on the website or in this instance a ‘ModelNotFoundException’ then a page should come up confirming this. If it’s a ‘PasswordMismatch’ exception, naughty naughty… You can’t get past the golden barriers. But for some reason when I tried it, it just wouldn’t work for me.

Let’s face it, NodeJS can be a little fussy…

It important to check the syntax when you write your code. NodeJS tends to be funny like that, but I found a way to work around the issue. I wanted to use my own custom error page on Fibermouse. Here’s how I did it. “Pin back your lugholes…” (Jeremy Kyle uses this phrase often…). If you don’t know who he is, give him a Google, or a Bing, or a DuckDuckGo in your spare time.

Caution: Don’t mess up your app now will you…

Although this is simple to carry out it does mean you’re modifying files (or cogs) that keep your AdonisJS app on it’s feet, so just take care when you do it.

Let’s crack on, open up your app files.

Find your AdonisJS app files and navigate to the following:

app/Listeners/Http.js

Open this file with your favourite editor, mine are Atom and Adobe Brackets. This is where your error handling is done (hence why I said take care…). For development purposes AdonisJS utilises Youch to catch errors. OMG what a saviour that was for me when diagnosing errors, and boy did I have many errors to deal with at the time as I was migrating from a PHP app. There’s a block of code for handling errors within development mode. No need to touchy touchy… Move on towards the bottom of the page.

Handling specific errors.

I want to serve a custom page for each specific error, so for instance it could be a 404 (Not Found) or a 403 (Access Denied)/401 (Unauthorised), or a good ol’ 500 (Internal Server Error).

Here I have 2 blocks. One for the Error 400 and the other for Error 401. These are included within the Http.handleError block. You do not need to include it again, I have only done this for illustrational purposes.

Save these to the file, and you can also backup a copy in case you want to revert back as well. By the way, the ‘||’ I have included is known as a Logical OR, hopefully this MSDN link will help explain it. In theory this does the following: The ‘if’ statement is executed and checks whether or not if there is an error. If there is then it’ll check whether or not it has the error of 404 (using as an example). If it does then the rest of the block is executed. Both statements have to be true in order for it to be executed.

Focusing on the custom pages…

On the error page I want to display the system response that is shown when the user strikes a ‘404’. I’ll do so like this within the following location:

resources/views/errors

By the way… Your error pages can be put anywhere you want within the views folder as long as you clearly state where you want your app to find it.

Here’s my Error 404 page. This features the Error data (such as the status code + the response), and the custom message I set under a custom variable.

Now this should be working on your app. You should be able to do this with various other errors too. I consider this a workaround, as I’m sure there are many other ways to implement error handling techniques. Only mentioning this one as it wasn’t quite working for me previously. So there you have it, you should now have your very own custom error page working nicely.

That’s enough from me at the moment.

Fibermouse (my project…) is now up and running and are looking for new people to come and join. I am hoping to offer AdonisJS setups to new customers in the coming months. I build and maintain this single-handedly and would love for you to come and visit.

If you like what I am doing, you can sprinkle some bitcoin on Keybase.io. I'm still building this project so any support would be greatly appreciated! I’ll be setting up my own Patreon page shortly.

Support AdonisJS on Patreon as well!

What a great framework. It’s certainly worth the support. Head along to the page on Patreon to show your support.

Until next time, cheerio.

Myles (@theformalmouse)