node.js errors

15
NodeJS - Errors PEGGY 2016-06-17

Transcript of node.js errors

Page 1: node.js errors

NodeJS - ErrorsPEGGY2016-06-17

Page 2: node.js errors

Four Categories Of Errors

1. Standard JavaScript errors such as: <EvalError> : thrown when a call to eval() fails. <SyntaxError> : thrown in response to improper JavaScript language syntax. <RangeError> : thrown when a value is not within an expected range <ReferenceError> : thrown when using undefined variables <TypeError> : thrown when passing arguments of the wrong type <URIError> : thrown when a global URI handling function is misused.

Page 3: node.js errors

RangeError

A subclass of Error that indicates that a provided argument was not within the set or range of acceptable values for a function

require('net').connect(-1);D:\Framework\nodeJs_errors>node test.jsnet.js:954 throw new RangeError('"port" option should be >= 0 and < 65536: ' + port); ^

RangeError: "port" option should be >= 0 and < 65536: -1 at lookupAndConnect (net.js:954:13) at Socket.connect (net.js:929:5) at Object.exports.connect.exports.createConnection (net.js:70:35) at Object.<anonymous> (D:\Framework\nodeJs_errors\test.js:1:78) at Module._compile (module.js:541:32) at Object.Module._extensions..js (module.js:550:10) at Module.load (module.js:458:32) at tryModuleLoad (module.js:417:12) at Function.Module._load (module.js:409:3) at Function.Module.runMain (module.js:575:10)

Page 4: node.js errors

ReferenceError

A subclass of Error that indicates that an attempt is being made to access a variable that is not defined.

D:\Framework\nodeJs_errors>node test.jsD:\Framework\nodeJs_errors\test.js:1(function (exports, require, module, __filename, __dirname) { test; ^

ReferenceError: test is not defined at Object.<anonymous> (D:\Framework\nodeJs_errors\test.js:1:63) at Module._compile (module.js:541:32) at Object.Module._extensions..js (module.js:550:10) at Module.load (module.js:458:32) at tryModuleLoad (module.js:417:12) at Function.Module._load (module.js:409:3) at Function.Module.runMain (module.js:575:10) at startup (node.js:160:18) at node.js:449:3

test;

Page 5: node.js errors

SyntaxError

A subclass of Error that indicates that a program is not valid JavaScript.

D:\Framework\nodeJs_errors>node test.jsD:\Framework\nodeJs_errors\test.js:1(function (exports, require, module, __filename, __dirname) { var 77 = 88; ^^SyntaxError: Unexpected number at Object.exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:513:28) at Object.Module._extensions..js (module.js:550:10) at Module.load (module.js:458:32) at tryModuleLoad (module.js:417:12) at Function.Module._load (module.js:409:3) at Function.Module.runMain (module.js:575:10) at startup (node.js:160:18) at node.js:449:3

var 77 = 88;

Page 6: node.js errors

TypeError

A subclass of Error that indicates that a provided argument is not an allowable type.

D:\Framework\nodeJs_errors>node test.jsD:\Framework\nodeJs_errors\test.js:2temp();^

TypeError: temp is not a function at Object.<anonymous> (D:\Framework\nodeJs_errors\test.js:2:1) at Module._compile (module.js:541:32) at Object.Module._extensions..js (module.js:550:10) at Module.load (module.js:458:32) at tryModuleLoad (module.js:417:12) at Function.Module._load (module.js:409:3) at Function.Module.runMain (module.js:575:10) at startup (node.js:160:18) at node.js:449:3

var temp = "test"; temp();

Page 7: node.js errors

Four Categories Of Errors

2. System errors triggered by underlying operating system constraints such as attempting to open a file that does not exist, attempting to send data over a closed socket, etc

Page 8: node.js errors

Four Categories Of Errors

3. User-specified errors triggered by application code.4. Assertion Errors are a special class of error that can be triggered

whenever Node.js detects an exceptional logic violation that should never occur. These are raised typically by the assert module.

Page 9: node.js errors

Error Propagation and Interception

Try/Catch Callback EventEmitter

Page 10: node.js errors

Try/Catch

All JavaScript errors are handled as exceptions that immediately generate and throw an error using the standard JavaScript throw mechanism. These are handled using the try / catch construct provided by the JavaScript language.

// Throws with a ReferenceError because z is undefined try {

const m = 1; const n = m + z;

} catch (err) { // Handle the error here.

}

Page 11: node.js errors

Callback

const fs = require('fs'); fs.readFile('a file that does not exist', (err, data) => {

if (err) { console.error('There was an error reading the file!', err); return;

} // Otherwise handle the data

});

Page 12: node.js errors

EventEmitter

const net = require('net'); const connection = net.connect('localhost'); // Adding an 'error' event handler to a stream: connection.on('error', (err) => {

// If the connection is reset by the server, or if it can't // connect at all, or on any sort of error encountered by // the connection, the error will be sent here. console.error(err);

});

Page 13: node.js errors

Try / Catch with Asynchronous APIs

The JavaScript try / catch mechanism cannot be used to intercept errors generated by asynchronous APIs.

// THIS WILL NOT WORK: const fs = require('fs');

try { fs.readFile('/some/file/that/does-not-exist', (err, data) => {

// mistaken assumption: throwing here... if (err) { throw err; }

}); } catch(err) {

// This will not catch the throw!console.log(err);

}

Page 14: node.js errors

Class: Error

const err = new Error('The message'); console.log(err.message); console.log(err.stack);

Page 15: node.js errors

Throw Error

function makeError() { throw new Error('oh no!');

}

makeError(); // will throw: