Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x | // Require our core node modules.
let util = require("util");
// Export the factory function for the custom error object. The factory function lets
// the calling context create new AppError instances without calling the [new] keyword.
// I create the new instance of the AppError object, ensureing that it properly
// extends from the Error class.
export function createAppError(message, name, statusCode, details) {
// NOTE: We are overriding the "implementationContext" so that the createAppError()
// function is not part of the resulting stacktrace.
return new AppError(message, name, statusCode, details, createAppError);
}
// ----------------------------------------------------------------------------------- //
// ----------------------------------------------------------------------------------- //
// I am the custom error object for the application. The settings is a hash of optional
// properties for the error instance:
// --
// * name: I am the name of error being thrown.
// * message: I am the reason the error is being thrown.
// * statusCode: I am a custom error code associated with this type of error.
// --
// The implementationContext argument is an optional argument that can be used to trim
// the generated stacktrace. If not provided, it defaults to AppError.
export class AppError {
private isAppError: boolean;
private retryable: boolean;
private name: string;
private message: string;
private statusCode: number;
private code: number;
private details: any;
private implementationContext: any;
constructor(
message: string,
name: string,
statusCode: number,
details: any,
implementationContext: any
) {
// Each of the following properties can be optionally passed-in
// as part of the Settings argument.
this.name = name || "AppError";
this.message = message || "An error occurred.";
this.code = this.statusCode = statusCode || 500;
// This is just a flag that will indicate if the error is a custom AppError. If this
// is not an AppError, this property will be undefined, which is a Falsey.
this.isAppError = true;
this.details = details || {}; //extra meta
this.retryable = this.details.retryable || false;
// Capture the current stacktrace and store it in the property "this.stack". By
// providing the implementationContext argument, we will remove the current
// constructor (or the optional factory function) line-item from the stacktrace; this
// is good because it will reduce the implementation noise in the stack property.
// --
// Read More: https://code.google.com/p/v8-wiki/wiki/JavaScriptStackTraceApi#Stack_trace_collection_for_custom_exceptions
this.implementationContext = implementationContext;
Error.captureStackTrace(this, this.implementationContext || AppError);
}
}
util.inherits(AppError, Error);
|