HI WELCOME TO SIRIS

Node.js Errors

Leave a Comment
The Node.js applications generally face four types of errors:
  • Standard JavaScript errors i.e. <EvalError>, <SyntaxError>, <RangeError>, <ReferenceError>, <TypeError>, <URIError> etc.
  • System errors
  • User-specified errors
  • Assertion errors

Node.js Errors Example 1

Let's take an example to deploy standard JavaScript error - ReferenceError.
File: error_example1.js
  1. // Throws with a ReferenceError because b is undefined  
  2. try {  
  3.   const a = 1;  
  4.   const c = a + b;  
  5. catch (err) {  
  6.   console.log(err);  
  7. }  
Open Node.js command prompt and run the following code:
  1. node error_example1.js  
Node.js error example 1

Node.js Errors Example 2

File: timer2.js
  1. const fs = require('fs');  
  2. function nodeStyleCallback(err, data) {  
  3.  if (err) {  
  4.    console.error('There was an error', err);  
  5.    return;  
  6.  }  
  7.  console.log(data);  
  8. }  
  9. fs.readFile('/some/file/that/does-not-exist', nodeStyleCallback);  
  10. fs.readFile('/some/file/that/does-exist', nodeStyleCallback);  
Open Node.js command prompt and run the following code:
  1. node error_example2.js  
Node.js error example 2

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.