Node.js Applications generally experience 4 categories of errors:
Standard JavaScript errors :
System errors : Operating system constraints such as file does not exist, send data over a closed socket, etc;
User-specified errors: User-specified errors triggered from application code.
AssertionErrors : AssertionErrors are a special class of error that can be triggered whenever Node.js detects an exceptional logic violation that should never occur.
Node.js errors inherit from the Error base class
Error
TypeError
JavaScript errors are handled as exceptions,below is the example.
Example 1
try {
const a = 1;
const c = a + b;
} catch (err) {
console.log(err);
}
Output :
ReferenceError: b is not defined
Below is a example of System errors ,We are going to open a file , that does not exists.
Example 2
const fs = require('fs');
function MyCallback(err, data) {
if (err)
{
console.error('There was an error opeing the file', err);
return;
}
console.log(data);
}
fs.readFile('C:/does-not-exist.txt', MyCallback);Output :
There was an error opeing the file { Error: ENOENT: no such file or directory, open 'C:\does-not-exist.txt'
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: 'C:\\does-not-exist.txt' }
Standard JavaScript errors :
System errors : Operating system constraints such as file does not exist, send data over a closed socket, etc;
User-specified errors: User-specified errors triggered from application code.
AssertionErrors : AssertionErrors are a special class of error that can be triggered whenever Node.js detects an exceptional logic violation that should never occur.
Node.js errors inherit from the Error base class
Error
TypeError
JavaScript errors are handled as exceptions,below is the example.
Example 1
try {
const a = 1;
const c = a + b;
} catch (err) {
console.log(err);
}
Output :
ReferenceError: b is not defined
Below is a example of System errors ,We are going to open a file , that does not exists.
Example 2
const fs = require('fs');
function MyCallback(err, data) {
if (err)
{
console.error('There was an error opeing the file', err);
return;
}
console.log(data);
}
fs.readFile('C:/does-not-exist.txt', MyCallback);Output :
There was an error opeing the file { Error: ENOENT: no such file or directory, open 'C:\does-not-exist.txt'
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: 'C:\\does-not-exist.txt' }
No comments:
Post a Comment