common-types
Advanced tools
Comparing version 1.2.3 to 1.3.0
@@ -42,1 +42,48 @@ /** | ||
} | ||
/** A decorator signature for a class property */ | ||
export declare type PropertyDecorator = (target: any, key: string | symbol) => void; | ||
/** A decorator signature for a class */ | ||
export declare type ClassDecorator = <TFunction extends Function>(target: TFunction) => TFunction | void; | ||
export interface IVerboseError { | ||
/** A short and unique identifier for the error; typically would not have any spaces in it */ | ||
code: string; | ||
/** A user-friendly description of the error message */ | ||
message: string; | ||
module?: string; | ||
function?: string; | ||
fileName?: string; | ||
stackFrames?: any[]; | ||
} | ||
export declare type LazyString = () => string; | ||
export interface IStackFrame { | ||
getTypeName: LazyString; | ||
getFunctionName: LazyString; | ||
getMethodName: LazyString; | ||
getFileName: LazyString; | ||
getLineNumber: LazyString; | ||
getColumnNumber: LazyString; | ||
isNative: LazyString | string; | ||
} | ||
export declare class VerboseError extends Error implements IVerboseError { | ||
private static stackParser(err); | ||
/** | ||
* If you want to use a library like stack-trace(node) or stacktrace-js(client) add in the "get" | ||
* function that they provide | ||
*/ | ||
static setStackParser(fn: (err: IVerboseError) => any): void; | ||
static useColor: true; | ||
static filePathDepth: 3; | ||
code: string; | ||
message: string; | ||
module?: string; | ||
function?: string; | ||
stackFrames?: IStackFrame[]; | ||
constructor(err: IVerboseError, ...args: any[]); | ||
toString(): string; | ||
toJSON(): string; | ||
toObject(): { | ||
code: string; | ||
message: string; | ||
module: string; | ||
}; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const chalk = require("chalk"); | ||
var STAGE; | ||
@@ -23,2 +24,62 @@ (function (STAGE) { | ||
; | ||
class VerboseError extends Error { | ||
static stackParser(err) { | ||
return undefined; | ||
} | ||
/** | ||
* If you want to use a library like stack-trace(node) or stacktrace-js(client) add in the "get" | ||
* function that they provide | ||
*/ | ||
static setStackParser(fn) { | ||
VerboseError.stackParser = fn; | ||
} | ||
constructor(err, ...args) { | ||
super(...args); | ||
this.code = err.code; | ||
this.message = err.message; | ||
this.module = err.module; | ||
this.function = err.function; | ||
const stackFrames = VerboseError.stackParser(this); | ||
if (stackFrames) { | ||
this.stackFrames = stackFrames | ||
.filter(frame => (frame.getFileName() || '').indexOf('common-types') === -1); | ||
this.function = stackFrames[0].getMethodName(); | ||
this.stack = this.message + "\n\n" + this.stackFrames.map(frame => { | ||
const isNative = typeof frame.isNative === 'function' ? frame.isNative() : frame.isNative; | ||
const colorize = (content) => VerboseError.useColor && isNative ? chalk.grey.italic(content) : content; | ||
const className = frame.getTypeName() ? frame.getTypeName() + ' → ' : ''; | ||
const functionName = frame.getMethodName() || frame.getFunctionName() || '<anonymous>'; | ||
const classAndFunction = VerboseError.useColor | ||
? chalk.bold(`${className}${functionName}`) | ||
: `${className}${functionName}`; | ||
const fileName = (frame.getFileName() || '') | ||
.split('/') | ||
.slice(-1 * VerboseError.filePathDepth) | ||
.join('/'); | ||
const details = isNative | ||
? '( native function )' | ||
: `[ line ${frame.getLineNumber()}, col ${frame.getColumnNumber()} in ${fileName} ]`; | ||
console.log(colorize(`\t at ${classAndFunction} ${details}`)); | ||
return colorize(`\t at ${classAndFunction} ${details}`); | ||
}).join("\n"); | ||
} | ||
else { | ||
this.stack = this.stack.split("\n").filter(line => line.indexOf('VerboseError') === -1).join("\n"); | ||
} | ||
} | ||
toString() { | ||
return this.message + this.stack; | ||
} | ||
toJSON() { | ||
return JSON.stringify(this.toObject(), null, 2); | ||
} | ||
toObject() { | ||
return { | ||
code: this.code, | ||
message: this.message, | ||
module: this.module, | ||
}; | ||
} | ||
} | ||
exports.VerboseError = VerboseError; | ||
//# sourceMappingURL=common-types.js.map |
{ | ||
"name": "common-types", | ||
"version": "1.2.3", | ||
"version": "1.3.0", | ||
"description": "Common types not included in Typescript", | ||
@@ -11,3 +11,9 @@ "main": "lib/common-types.js", | ||
"author": "Ken Snyder <ken@ken.net>", | ||
"license": "MIT" | ||
"license": "MIT", | ||
"dependencies": { | ||
"chalk": "^2.1.0" | ||
}, | ||
"devDependencies": { | ||
"@types/chalk": "^0.4.31" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
import chalk = require('chalk'); | ||
/** | ||
@@ -47,2 +47,104 @@ * A Javascript hash which allows for any set of keys | ||
child_changed = 'child_changed' | ||
}; | ||
}; | ||
// DECORATORS | ||
/** A decorator signature for a class property */ | ||
export type PropertyDecorator = (target: any, key: string | symbol) => void; | ||
/** A decorator signature for a class */ | ||
export type ClassDecorator = <TFunction extends Function>(target: TFunction) => TFunction | void; | ||
// ERRORS | ||
export interface IVerboseError { | ||
/** A short and unique identifier for the error; typically would not have any spaces in it */ | ||
code: string; | ||
/** A user-friendly description of the error message */ | ||
message: string; | ||
module?: string; | ||
function?: string; | ||
fileName?: string; | ||
stackFrames?: any[]; | ||
} | ||
export type LazyString = () => string; | ||
export interface IStackFrame { | ||
getTypeName: LazyString; | ||
getFunctionName: LazyString; | ||
getMethodName: LazyString; | ||
getFileName: LazyString; | ||
getLineNumber: LazyString; | ||
getColumnNumber: LazyString; | ||
isNative: LazyString | string; | ||
} | ||
export class VerboseError extends Error implements IVerboseError { | ||
private static stackParser(err: VerboseError): void | any[] { | ||
return undefined; | ||
} | ||
/** | ||
* If you want to use a library like stack-trace(node) or stacktrace-js(client) add in the "get" | ||
* function that they provide | ||
*/ | ||
public static setStackParser(fn: (err: IVerboseError) => any) { | ||
VerboseError.stackParser = fn; | ||
} | ||
public static useColor: true; | ||
public static filePathDepth: 3; | ||
code: string; | ||
message: string; | ||
module?: string; | ||
function?: string; | ||
stackFrames?: IStackFrame[]; | ||
constructor(err: IVerboseError, ...args: any[]) { | ||
super(...args); | ||
this.code = err.code; | ||
this.message = err.message; | ||
this.module = err.module; | ||
this.function = err.function; | ||
const stackFrames = VerboseError.stackParser(this); | ||
if(stackFrames) { | ||
this.stackFrames = stackFrames | ||
.filter(frame => (frame.getFileName() || '').indexOf('common-types') === -1); | ||
this.function = stackFrames[0].getMethodName(); | ||
this.stack = this.message + "\n\n" + this.stackFrames.map(frame => { | ||
const isNative = typeof frame.isNative === 'function' ? frame.isNative() : frame.isNative; | ||
const colorize = (content: string) => VerboseError.useColor && isNative ? chalk.grey.italic(content) : content; | ||
const className = frame.getTypeName() ? frame.getTypeName() + ' → ' : ''; | ||
const functionName = frame.getMethodName() || frame.getFunctionName() || '<anonymous>'; | ||
const classAndFunction = VerboseError.useColor | ||
? chalk.bold(`${className}${functionName}`) | ||
: `${className}${functionName}`; | ||
const fileName = (frame.getFileName() || '') | ||
.split('/') | ||
.slice(-1 * VerboseError.filePathDepth) | ||
.join('/'); | ||
const details = isNative | ||
? '( native function )' | ||
: `[ line ${frame.getLineNumber()}, col ${frame.getColumnNumber()} in ${fileName} ]`; | ||
console.log(colorize(`\t at ${classAndFunction} ${details}`)); | ||
return colorize(`\t at ${classAndFunction} ${details}`); | ||
}).join("\n"); | ||
} else { | ||
this.stack = this.stack.split("\n").filter(line => line.indexOf('VerboseError') === -1).join("\n"); | ||
} | ||
} | ||
toString() { | ||
return this.message + this.stack; | ||
} | ||
toJSON() { | ||
return JSON.stringify(this.toObject(), null, 2); | ||
} | ||
toObject() { | ||
return { | ||
code: this.code, | ||
message: this.message, | ||
module: this.module, | ||
}; | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16193
9
327
1
1
+ Addedchalk@^2.1.0
+ Addedansi-styles@3.2.1(transitive)
+ Addedchalk@2.4.2(transitive)
+ Addedcolor-convert@1.9.3(transitive)
+ Addedcolor-name@1.1.3(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedhas-flag@3.0.0(transitive)
+ Addedsupports-color@5.5.0(transitive)