SIGN IN

8. Node.js Lessons. Inheritance from Errors, Error

node_js_by_myvushka-d8yrtqh

Inheritance of a built object from Error is quite different in Node.js. Here is an example that will help me to explain you first why we may need this thing, and then – how we should do it correctly.

Here our coding consists of two functions. The first one is getPhrase.

It is the simplest internationalization function that has ever existed. It takes a phrase name (name) and returns the respective data – if there are any; if they do not exist – it shows an exception (“There is no such a phrase”).

The second function is makePage:

This function acquires url. Right now it can work only with index.html. That’s why for others it shows an error; and if it is index.html, it returns it to the formatted string. Let us see how a code like this works:

Get it started. It works! “Hello World” outputted a formatted string. Everything’s correct. And now let us add the correct error processing to this code. For example, if we’ve got an incorrect url that does not exist, makePage will show up a mistake “There is no such a page.” And if we imagine it is a web server, it means, we need to output a message to a user: 404 – Page Not Found. It is just one of the error variants, just one processing method. And let us take a look at what will happen, if the phrase is unknown. If somewhere in our coding we requested a phrase output, for example:

But “There is no such a phrase” is already another error type. And then you need to make not a 404, but a 500 status and write a notification to system administrator that there is something wrong, a vocabulary lacks a phrase and you need to correct it. It is a program error, so you need to process it differently.

Unfortunately, in our current code

even if I add try catch around the function makePage, it is still hard to differ one error from another. Both of them just belong to the error class. From the object-oriented programming point of view, a wise solution would be to make individual error objects for different errors. Here it will be:

And here

And in its construction we’ve also added its status – 404. Respectively, for those http-type error that we need to show to a user, we will use the following errors: HttpError. And for the errors dealing with translation – PhraseError(“There is no such a phrase: ” + name).

Let us announce the respective classes using util inherits. For example, like here:

And this way:

Let us take a look at some aspects of work with an error object. What properties do we need here? The first one is surely message. In order to put message, I need to do it manually. It is the specifics of dealing with the errors. It means, we do not request a standard superclass parent, i.e. Error.apply(this, arguments): – that is a common way of requesting a superclass constructor. In our case, it will do nothing useful for us. But we need to do it manually.

The next parameter every built-in error has is name. According to this property we can obviously understand the error we’ve got. Any built-in error has this property in prototypes, that’s why we’ll add this here, too.

Finally, the last important parameter for us is stack. But let us talk about this later.

So, let me activate our code.

As we see, here makePage(“index”); is an unknown url, that’s why we need to get HttpError.  This error gets processed in the following way:

We simply output it. Activate:

node errors

Everything works.

So, let us now check out another variant: if an url is correct (note that changed strings are bold in the code below), but an error took place in another program, i.e. a strange phrase that we do not have came out. In this case, if an error is different, we need to answer it differently. Panic, disaster, program error! Wake up everybody and get everything corrected! This coding branch will work for all program errors, including the built-in ones, not only for PhraseError

*****

index.html

Activate it. I’ve outputted it – indeed, console.error. Take a look at its properties. Everything’s correct. But stack should keep information on where and in what file the error took place and what it followed. In constructors I didn’t do anything specific to correct stack. And in standard JavaScript we lack them. I simply create a new object. In fact, there are different cycles that allow you to get stack, but here we do not need them for there is a special JS command in V8 that is not included in a standard version, but allows to get a stack. It looks just like that:

This command acquires the current stack, i.e. a consequence of requests put together that save it to this in the current code place, which means in the error object location. Let us take a look at what we’ve got now. Right now the system has outputted stack – the place, where it all occurred. But if we look carefully, the error happened exactly here:

We are interested in what happened here and how we came up to this. We are barely interested in what was going on inside PhraseError. This string, stack, is unnecessary in our case. To get it removed, the second optional special parameter exists in captureStackTrace. That is a function that will be a final destination for StackTrace. So, if I specify a current constructor

in function HttpError

and in function PhraseError

I add the following changes,

the stack  inside this function won’t be displayed here and won’t be gathered. Let us check for ourselves. I restarted the program, and the unnecessary string got cut out.

So, we’ve received inherited error objects with correct message and name properties, as well as an opportunity to output stack.

This lesson coding can be downloaded from here

ogimage-1024x538

The lesson materials were borrowed from the following screencast

We are looking forward to meeting you on our website soshace.com

About the author

Stay Informed

It's important to keep up
with industry - subscribe!

Stay Informed

Looks good!
Please enter correct name
Please enter correct email
Looks good!

Related articles

The Path of the Self-Taught Programmer: Avoiding Common Problems

In this article, we’ll explore how a self-taught programmer comes to be: which education opportunities they can utilize, which problems they may>>>

Programming

Introduction to GitHub Desktop: A GUI Enhancement to a CLI Approach

In this article, we'll explore various use cases of GitHub Desktop -- GUI software designed to ehance your git workflow. Does it hold up against CLI>>>

Programming
29.10.2019

Python Array Explained and Visualized

In this tutorial, we’ll delve into how Python arrays work, what their limitations are, and how you can use them to maximize their>>>

Programming

Sign in

Forgot password?

Or use a social network account

 

By Signing In \ Signing Up, you agree to our privacy policy

Password recovery

You can also try to

Or use a social network account

 

By Signing In \ Signing Up, you agree to our privacy policy