Monday, 28 May 2018

007 Node.js Errors

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' } 





No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...