Type of error #
In Node.js, errors are categorized into various types, each serving a specific purpose in signaling and handling exceptional conditions. Understanding these error types is essential for effective debugging and building robust applications. Here’s a basic overview of common error types in Node.js:
Error Object: #
The Error object is the base class for all errors in Node.js. It allows developers to create custom errors with a specific error message.
const customError = new Error('This is a custom error');
System Errors: #
System errors, also known as built-in errors, are thrown by Node.js core modules or underlying system operations.
const fs = require('fs');
try {
fs.readFileSync('nonexistent-file.txt', 'utf-8');
} catch (error) {
console.error(error.message); // ENOENT: no such file or directory
}
Assertion Errors: #
Assertion errors are thrown when an assertion from the assert module fails.
const assert = require('assert');
try {
assert.strictEqual(1, 2, 'Values are not equal');
} catch (error) {
console.error(error.message); // Values are not equal
}
TypeError: #
The TypeError is thrown when an operation is performed on an object of an inappropriate type.
try {
null.foo(); // TypeError: Cannot read property 'foo' of null
} catch (error) {
console.error(error.message);
}
ReferenceError: #
The ReferenceError occurs when trying to reference an undeclared variable.
try {
console.log(undefinedVariable); // ReferenceError: undefinedVariable is not defined
} catch (error) {
console.error(error.message);
}
RangeError: #
The RangeError is thrown when a numeric value is outside the expected range.
try {
const array = new Array(-1); // RangeError: Invalid array length
} catch (error) {
console.error(error.message);
}
SyntaxError: #
The SyntaxError is thrown when there is a mistake in the syntax of JavaScript code.
try {
eval('console.log("Hello, Node.js!"'); // SyntaxError: Unexpected end of input
} catch (error) {
console.error(error.message);
}
Custom Errors: #
Developers can create custom error types by extending the Error class. Custom errors often include additional properties or methods for better identification and handling.
class CustomError extends Error {
constructor(message, code) {
super(message);
this.code = code;
}
}
const customError = new CustomError('Custom error message', 500);
UnhandledPromiseRejectionWarning: #
This warning is emitted when a Promise is rejected but has no associated rejection handler. It is a runtime warning that indicates potential issues in the application’s asynchronous code.
const promise = Promise.reject(new Error('Unhandled Promise Rejection'));
// Warning: UnhandledPromiseRejectionWarning: Unhandled promise rejection.
// This error originated either by throwing inside of an async function
// without a catch block, or by rejecting a promise which was not handled
// with .catch(). (rejection id: 1)
Process-Specific Errors: #
Node.js may emit process-specific errors, such as uncaughtException and unhandledRejection, providing a way to catch global errors in the application.
process.on('uncaughtException', (error) => {
console.error('Uncaught Exception:', error.message);
process.exit(1);
});
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection:', reason);
process.exit(1);
});
Understanding these basic error types helps developers diagnose issues efficiently and implement appropriate error-handling strategies in Node.js applications. Custom errors, in particular, offer a way to create meaningful and context-specific error objects tailored to the needs of the application.