Errors are any issue that arises unexpectedly and cause our program to not function properly , or halt its execution.
An error
object in node.js does not specify any circumstances under which the error occured but
error
object does capture a stack trace which details the point in the code where the error
was instantiated and may provide error description. All errors generated by node.js are either instantiated from
error
class or are intance of error
class.
error.message
property to the text specified.
const err = new Error('This is an error message');
new Error(msg)
property. Mostly , the message "msg" comes in the first line of the error's stack trace .
An example is shown below :
//here error.message property is set to the string provided.
const err = new Error('This is an error message');
console.error(err.message);
//Output will be :
//This is an error message
node.js error codes
.
.stack
property on the target
object which returns the location in the code where Error.captureStackTrace()
was called.
It returns the data in the form of string.
In node.js , we are having several mechanisms for error propagating and handle those propagated errors while the application is in its running state. However the way in which these errors are reported and handled completely depends upon the type of error and API style.
throw
to report errors.
error
event if an asynchronous event is
called on an object.
connection.on('error', (err) => {
//This is the error event which is used
//to handle the error properly
console.error(err);
});
//Reading a File Asynchronously using nodejs
//if any error occurs such as file not found
//we don't have permissions , etc
//then an err message will be console.
//otherwise data will be printed
var fs=require('fs');
fs.readFile('message.txt', (err, data) => {
if (err)
return console.error(err);
console.log("Content : " + data);
});
throw
to raise exception which can further
be handled using try/catch
block. (Not recommended)
Node.js methods follows a idiomatic pattern which is knows as Node.js style callback . Following this
pattern , we pass the callback function as an argument to the method. When the execution of the operation is
completed or an error is raised then the callback function is called with error object if there exists any error
object otherwise the first argument is passed as null
.
const fs = require('fs');
function nSCallback(error, data) {
if (error) {
console.error('Error : ', error);
return;
}
console.log(data);
}
fs.readFile('file_that_exists', nSCallback);
fs.readFile('file_does_not_exists', nSCallback);
We cannot use try/catch
mechanism to intercept the errors generated by asynchronous APIs.
Note : The snippet Given below is invalid
//Invalid Snippet
//An asynchronous operation which will generate error
try {
async_method('invalid_arguments_generate_error', (err, data) => {
if (err) {
throw err;
}
});
} catch (err) {
console.error(err);
}
In node.js applications , mostly all the errors belong to one of the mentioned categories :
eval()
fails.
object
where a string
is expected
would be considered a TypeError.
syscall
. error.code
. assert
. This type of errors
are special case of errors
which occurs when an exceptional logic violation is detected in node.js
that should never occur.
In this part of the node.js series 30 days of node we learned about errors
. We learned what are errors and
errors in node.js and properties of errors in node.js.
We also learned about error propagation and interception in nodejs , what is the concept of node.js styles callbacks and why
it is better to avoid try/catch while using asynchronous functions. Lastly we learned about the 4 categories most of the errors
belong to which are Standard javascript errors , system errors , user-specified errors and assertion errors .