ABOUT ME
CONTENTS
DEFINITION OF ERROR
DEFINITION OF ERROR: IN LIFE
DEFINITION OF ERROR: PROGRAMMING
ERROR TYPES
Operation Errors
Developer Errors
ERROR TYPES
Operation Errors
ERROR TYPES
Developer Errors
ERROR TYPES
SOURCE OF ERRORS
"I think I've got an awesome idea.
Frank, take your chainsaw and a flamethrower,let's have fun"
SOURCE OF ERRORS
ERROR HANDLING PATTERNS
Return a neutral value
ERROR HANDLING PATTERNS
Return the same answer as the previous time
ERROR HANDLING PATTERNS
Substitute the next piece of valid data
ERROR HANDLING PATTERNS
Substitute the closest legal value
ERROR HANDLING PATTERNS
Log a warning message
ERROR HANDLING PATTERNS
Return an error code
ERROR HANDLING PATTERNS
Call an error-processing routine/object
ERROR HANDLING PATTERNS
Display an error message wherever the error
is encountered
ERROR HANDLING PATTERNS
Handle the error in whatever way works best locally
ERROR HANDLING PATTERNS
Shut down
ERROR HANDLING PATTERNS
EXCEPTIONS
EXCEPTIONS
Handling:
EXCEPTIONS
Important:
EXCEPTIONS
SECURITY ISSUES
SECURITY ISSUES: INFORMATION LEAKAGE
Syntax error message
ReferenceError: foo is not defined\n at /home/roman_sachenko/Projects/API/app/controllers/main.js:28:9\n at wrapped (/home/roman_sachenko/Projects/API/node_modules/newrelic/lib/transaction/tracer/index.js:183:28)\n at wrappedPromise.linkTransaction (/home/roman_sachenko/Projects/API/node_modules/newrelic/lib/instrumentation/promise.js:273:65)\n at wrappedPromise.wrapped [as __NR_wrapper]
SECURITY ISSUES: INFORMATION LEAKAGE
Application error message
Authentication: "Email doesn't exist"
SECURITY ISSUES: INFORMATION LEAKAGE
SECURITY ISSUES: INFORMATION LEAKAGE
Painkiller
SECURITY ISSUES: INFORMATION LEAKAGE
IN CONTEXT OF NODE.JS
Assert API is not for tests only
IN CONTEXT OF NODE.JS
Assert API is not for tests only
const assert = require('assert');
const getResult = () => {
return {
err: 'error message',
value: null,
};
};
const result = getResult();
assert.ifError(result.err);
//do something else
IN CONTEXT OF NODE.JS
Yes, we've got more than just new Error();
IN CONTEXT OF NODE.JS
Yes, we've got more than just new Error();
Error
AssertionError
Range
Error
ReferenceError
Syntax
Error
Type
Error
IN CONTEXT OF NODE.JS
Still not enough? Let's extend the Error object
IN CONTEXT OF NODE.JS
Error
Still not enough? Let's extend the Error object
IN CONTEXT OF NODE.JS
Error
API
Error
Custom features
Bad
Request
Not
Found
Oops
Extend the Error object? How does it look like?
const httpStatus = require('http-status');
module.exports = class ApiError extends Error {
constructor(message, status) {
super(message);
this.name = this.constructor.name;
Error.captureStackTrace(this, this.constructor);
this.status = status || httpStatus.INTERNAL_SERVER_ERROR;
this.message = message;
}
};
IN CONTEXT OF NODE.JS
Want more? Events are everywhere
IN CONTEXT OF NODE.JS
Want more? Events are everywhere
IN CONTEXT OF NODE.JS
Event Emitter
Main Service
Error
Handling
Service
0
Service
1
Service
n
Events? Show me the code -_-
const EventEmitter = require('events');
class MainService extends EventEmitter {
constructor(serviceName) {
super();
this._serviceName = serviceName;
this.on('error', (err) => {
// handle ${err}
// from ${this._serviceName}
});
}
};
IN CONTEXT OF NODE.JS
class MyService extends MainService {
constructor() {
super('My Service');
}
foo() {
try {
// do something
} catch (err) {
this.emit('error', err);
}
}
};
Don't forget to catch exceptions and shut down the system
IN CONTEXT OF NODE.JS
Don't forget to catch exceptions and shut down the system
process
.on('uncaughtException', (err) => {
//log error
//do asynchronous OR synchronous stuff
process.exit(1) //shut down the system
})
.on('exit', () => {
//do synchronous stuff only - last chance
});
IN CONTEXT OF NODE.JS
And don't reveal too much
IN CONTEXT OF NODE.JS
And don't reveal too much
const applicationStartupFunction = () => {
if (process.env.NODE_ENV = 'production') {
Error.stackTraceLimit = -1;
}
};
IN CONTEXT OF NODE.JS
BEST PRACTICES
BEST PRACTICES
CHECKLIST
CHECKLIST
HELPFUL TOOLS
HELPFUL TOOLS
System Analysts
Logging
ARTICLES AND LINKS
ARTICLES AND LINKS
Questions
roman.sachenko