@grimen/mybad
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -59,6 +59,35 @@ | ||
} catch (error) { | ||
console.error(error.toString()) | ||
console.log() | ||
console.log('===============================') | ||
console.log(' error.toString()') | ||
console.log('---------------------------') | ||
console.log() | ||
console.log(error.toString()) | ||
console.log() | ||
throw error | ||
console.log() | ||
console.log('===============================') | ||
console.log(' error.stack') | ||
console.log('---------------------------') | ||
console.log() | ||
console.log(error.stack) | ||
console.log() | ||
console.log() | ||
console.log('===============================') | ||
console.log(' error.inspect()') | ||
console.log('---------------------------') | ||
console.log() | ||
console.log(error.inspect(colors = true, verbose = true)) | ||
console.log() | ||
console.log('===============================') | ||
console.log(' error.json()') | ||
console.log('---------------------------') | ||
console.log() | ||
console.log(error.json()) | ||
console.log() | ||
break | ||
} | ||
} |
{ | ||
"name": "@grimen/mybad", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "My friendly error base class - for Node/JavaScript.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
117
README.md
@@ -96,18 +96,109 @@ | ||
PRINT $$$$$$$$$$$$$ | ||
=============================== | ||
error.toString() | ||
--------------------------- | ||
Out of money printing ink... - { stash: '$$$$$$$$$$$$$$' } | ||
examples/basic.js:61 | ||
throw error | ||
^ | ||
=============================== | ||
error.stack | ||
--------------------------- | ||
ToMuchError: Out of money printing ink... | ||
at printMoney (examples/basic.js:29:15) | ||
at Object.<anonymous> (examples/basic.js:56:9) | ||
at Module._compile (internal/modules/cjs/loader.js:688:30) | ||
at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10) | ||
at Module.load (internal/modules/cjs/loader.js:598:32) | ||
at tryModuleLoad (internal/modules/cjs/loader.js:537:12) | ||
at Function.Module._load (internal/modules/cjs/loader.js:529:3) | ||
at Function.Module.runMain (internal/modules/cjs/loader.js:741:12) | ||
at startup (internal/bootstrap/node.js:285:19) | ||
at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3) | ||
at printMoney (/Users/grimen/Dev/Private/js-mybad/examples/basic.js:29:15) | ||
at Object.<anonymous> (/Users/grimen/Dev/Private/js-mybad/examples/basic.js:56:9) | ||
at Module._compile (module.js:653:30) | ||
at Object.Module._extensions..js (module.js:664:10) | ||
at Module.load (module.js:566:32) | ||
at tryModuleLoad (module.js:506:12) | ||
at Function.Module._load (module.js:498:3) | ||
at Function.Module.runMain (module.js:694:10) | ||
at startup (bootstrap_node.js:204:16) | ||
at bootstrap_node.js:625:3 | ||
=============================== | ||
error.inspect() | ||
--------------------------- | ||
Out of money printing ink... | ||
=============================== | ||
error.json() | ||
--------------------------- | ||
{ | ||
"type": "ToMuchError", | ||
"id": 1557522743469, | ||
"code": 400, | ||
"key": "too_much", | ||
"message": "Out of money printing ink...", | ||
"details": { | ||
"stash": "$$$$$$$$$$$$$$" | ||
}, | ||
"stack": [ | ||
{ | ||
"file": "/Users/grimen/Dev/Private/js-mybad/examples/basic.js", | ||
"function": "printMoney", | ||
"line": 29, | ||
"column": 15 | ||
}, | ||
{ | ||
"file": "/Users/grimen/Dev/Private/js-mybad/examples/basic.js", | ||
"function": "Object.<anonymous>", | ||
"line": 56, | ||
"column": 9 | ||
}, | ||
{ | ||
"file": "module.js", | ||
"function": "Module._compile", | ||
"line": 653, | ||
"column": 30 | ||
}, | ||
{ | ||
"file": "module.js", | ||
"function": "Object.Module._extensions..js", | ||
"line": 664, | ||
"column": 10 | ||
}, | ||
{ | ||
"file": "module.js", | ||
"function": "Module.load", | ||
"line": 566, | ||
"column": 32 | ||
}, | ||
{ | ||
"file": "module.js", | ||
"function": "tryModuleLoad", | ||
"line": 506, | ||
"column": 12 | ||
}, | ||
{ | ||
"file": "module.js", | ||
"function": "Function.Module._load", | ||
"line": 498, | ||
"column": 3 | ||
}, | ||
{ | ||
"file": "module.js", | ||
"function": "Function.Module.runMain", | ||
"line": 694, | ||
"column": 10 | ||
}, | ||
{ | ||
"file": "bootstrap_node.js", | ||
"function": "startup", | ||
"line": 204, | ||
"column": 16 | ||
}, | ||
{ | ||
"file": "625", | ||
"function": "bootstrap_node.js", | ||
"line": 3 | ||
} | ||
] | ||
} | ||
``` | ||
@@ -114,0 +205,0 @@ |
@@ -143,2 +143,6 @@ | ||
get stack () { | ||
return super.stack || '' | ||
} | ||
get stacktrace () { | ||
@@ -148,8 +152,9 @@ return this.stack | ||
get stackArray () { | ||
let _stackArray = this.stack | ||
get stackframes () { | ||
let _stackframes | ||
if (_stackArray) { | ||
_stackArray = _stackArray | ||
if (this.stack) { | ||
_stackframes = this.stack | ||
.split(/\n/) | ||
.slice(1) | ||
.filter((value) => { | ||
@@ -159,16 +164,102 @@ return value && !!value.trim().length | ||
.map((value) => { | ||
return value.trim() | ||
try { | ||
const stackframeLine = value.trim() | ||
let stackframeData | ||
if (stackframeLine.includes('(')) { | ||
stackframeData = stackframeLine.match(/^at ([^\()]+)\((.+)(?:\:(\d+)\:(\d+)\))/i) | ||
} else { | ||
stackframeData = stackframeLine.match(/^at (.+)(?:\:(\d+)\:(\d+))/i) | ||
} | ||
let [ | ||
_, | ||
functionName, | ||
fileName, | ||
lineNumber, | ||
columnNumber, | ||
] = stackframeData || [] | ||
functionName = functionName && functionName.trim() | ||
lineNumber = lineNumber && parseInt(lineNumber) | ||
columnNumber = columnNumber && parseInt(columnNumber) | ||
return { | ||
functionName, | ||
fileName, | ||
lineNumber, | ||
columnNumber, | ||
} | ||
} catch (error) { | ||
return error | ||
} | ||
}) | ||
} else { | ||
_stackArray = null | ||
_stackframes = null | ||
} | ||
return _stackArray | ||
return _stackframes | ||
} | ||
get stacktraceArray () { | ||
return this.stackArray | ||
get stackobjects () { | ||
const _stackobjects = (this.stackframes || []) | ||
.map((stackframe) => { | ||
const file = stackframe.fileName | ||
const function_ = stackframe.functionName | ||
const line = stackframe.lineNumber | ||
const column = stackframe.columnNumber | ||
const source = stackframe.source | ||
const stackobject = { | ||
file, | ||
function: function_, | ||
line, | ||
column, | ||
source, | ||
} | ||
return stackobject | ||
}) | ||
.filter((stackobject) => { | ||
return !!stackobject.file | ||
}) | ||
.filter((stackobject) => { | ||
const isInternalStackFile = stackobject.file.includes(__filename) | ||
const isInternalStackFunction = Object.getOwnPropertyNames(this).includes(stackobject.function) | ||
const isInternalStackObject = isInternalStackFile && isInternalStackFunction | ||
return !isInternalStackObject | ||
}) | ||
return _stackobjects | ||
} | ||
get data () { | ||
return { | ||
'type': this.constructor.name, | ||
'id': this.id, | ||
'code': this.code, | ||
'key': this.key, | ||
'message': this.message, | ||
'details': this.details, | ||
'stack': this.stackobjects, | ||
} | ||
} | ||
json (options = {}) { | ||
let { | ||
indent, | ||
} = options || {} | ||
if (!indent && indent !== false) { | ||
indent = DEFAULT_ERROR_INDENT | ||
} | ||
return JSON.stringify(this.data, null, indent) | ||
} | ||
inspect (options = {}) { | ||
@@ -207,4 +298,2 @@ options = options || {} | ||
// console.log('xxx', details) | ||
} else { | ||
@@ -259,3 +348,7 @@ details = null | ||
} else { | ||
return new BaseError(error) | ||
const castedError = new BaseError(error) | ||
castedError.stack = error.stack | ||
return castedError | ||
} | ||
@@ -274,3 +367,3 @@ } | ||
'details': extendedError.details, | ||
'stack': extendedError.stackArray, | ||
'stack': extendedError.stackobjects, | ||
...attrs, | ||
@@ -277,0 +370,0 @@ } |
@@ -7,5 +7,8 @@ /* global jest describe test expect */ | ||
const path = require('path') | ||
const mybad = require('../src') | ||
const stripAnsi = require('strip-ansi') | ||
const semver = require('semver') | ||
@@ -32,2 +35,9 @@ | ||
// ========================================= | ||
// CONSTANTS | ||
// -------------------------------------- | ||
const ROOT_PATH = path.resolve(path.join(__dirname, '..')) | ||
// ========================================= | ||
// TESTS | ||
@@ -193,2 +203,3 @@ // -------------------------------------- | ||
expect(error.stack).toBeType('string') | ||
expect(error.stacktrace.length).toBeGreaterThan(0) | ||
}) | ||
@@ -202,22 +213,90 @@ | ||
expect(error.stacktrace).toBeType('string') | ||
expect(error.stacktrace.length).toBeGreaterThan(0) | ||
}) | ||
test('#stackArray', async () => { | ||
test('#stackframes', async () => { | ||
let error = new mybad.Error() | ||
expect('stackArray' in error).toBe(true) | ||
expect('stackframes' in error).toBe(true) | ||
expect(error.stackArray).toBeInstanceOf(Array) | ||
expect(error.stackArray.length).toBeGreaterThan(0) | ||
expect(error.stackframes).toBeType('array') | ||
expect(error.stackframes.length).toBeGreaterThan(0) | ||
}) | ||
test('#stacktraceArray', async () => { | ||
test('#stackobjects', async () => { | ||
let error = new mybad.Error() | ||
expect('stackArray' in error).toBe(true) | ||
expect('stackobjects' in error).toBe(true) | ||
expect(error.stacktraceArray).toBeInstanceOf(Array) | ||
expect(error.stacktraceArray.length).toBeGreaterThan(0) | ||
expect(error.stackobjects).toBeType('array') | ||
expect(error.stackobjects.length).toBeGreaterThan(0) | ||
expect(error.stackobjects[0]).toBeType('object') | ||
expect(error.stackobjects[0].column).toEqual(expect.any(Number)) | ||
expect(error.stackobjects[0].file).toEqual(`${ROOT_PATH}/test/test_errors.js`) | ||
expect(error.stackobjects[0].function).toEqual(expect.stringMatching(/^Object\.(?:test|<anonymous>|mybad\.Error)$/)) | ||
expect(error.stackobjects[0].line).toEqual(223) | ||
expect(error.stackobjects[0].source).toEqual(undefined) | ||
}) | ||
test('#data', async () => { | ||
let error = new mybad.Error() | ||
expect('data' in error).toBe(true) | ||
expect(error.data).toBeType('object') | ||
expect('type' in error.data).toBe(true) | ||
expect('id' in error.data).toBe(true) | ||
expect('code' in error.data).toBe(true) | ||
expect('key' in error.data).toBe(true) | ||
expect('message' in error.data).toBe(true) | ||
expect('details' in error.data).toBe(true) | ||
expect('stack' in error.data).toBe(true) | ||
expect(error.data.type).toEqual('BaseError') | ||
expect(error.data.id).toEqual(undefined) | ||
expect(error.data.code).toEqual(undefined) | ||
expect(error.data.key).toEqual(undefined) | ||
expect(error.data.message).toEqual('Unknown') | ||
expect(error.data.details).toEqual({}) | ||
expect(error.data.stack.length).toBeGreaterThan(0) | ||
expect(error.data.stack[0]).toBeType('object') | ||
expect(error.data.stack[0].column).toEqual(expect.any(Number)) | ||
expect(error.data.stack[0].file).toEqual(`${ROOT_PATH}/test/test_errors.js`) | ||
expect(error.data.stack[0].function).toEqual(expect.stringMatching(/^Object\.(?:test|<anonymous>|mybad\.Error)$/)) | ||
expect(error.data.stack[0].line).toEqual(238) | ||
expect(error.data.stack[0].source).toEqual(undefined) | ||
}) | ||
test('#json()', async () => { | ||
let error = new mybad.Error() | ||
expect('json' in error).toBe(true) | ||
const json = error.json() | ||
expect(json).toBeType('string') | ||
const data = JSON.parse(json) | ||
expect(data.type).toBeType('string') | ||
expect(data.type).toEqual('BaseError') | ||
expect(data.message).toBeType('string') | ||
expect(data.message).toEqual('Unknown') | ||
expect(data.details).toBeType('object') | ||
expect(data.details).toEqual({}) | ||
expect(data.stack).toBeType('array') | ||
expect(data.stack.length).toBeGreaterThan(0) | ||
expect(data.stack[0]).toBeType('object') | ||
expect(data.stack[0].column).toEqual(expect.any(Number)) | ||
expect(data.stack[0].file).toEqual(`${ROOT_PATH}/test/test_errors.js`) | ||
expect(data.stack[0].function).toEqual(expect.stringMatching(/^Object\.(?:test|<anonymous>|mybad\.Error)$/)) | ||
expect(data.stack[0].line).toEqual(269) | ||
expect(data.stack[0].source).toEqual(undefined) | ||
}) | ||
test('#inspect()', async () => { | ||
@@ -254,3 +333,2 @@ let error = new mybad.Error() | ||
test('.cast()', async () => { | ||
@@ -289,4 +367,12 @@ expect('cast' in mybad.Error).toBe(true) | ||
expect(errorObject.details).toEqual({}) | ||
// expect(errorObject.stack).toEqual(undefined) | ||
expect(errorObject.stack).toBeType('array') | ||
expect(errorObject.stack.length).toBeGreaterThan(0) | ||
expect(errorObject.stack[0]).toBeType('object') | ||
expect(errorObject.stack[0].column).toEqual(expect.any(Number)) | ||
expect(errorObject.stack[0].file).toEqual(`${ROOT_PATH}/test/test_errors.js`) | ||
expect(errorObject.stack[0].function).toEqual(expect.stringMatching(/^Object\.(?:test|<anonymous>|mybad\.Error)$/)) | ||
expect(errorObject.stack[0].line).toEqual(344) | ||
expect(errorObject.stack[0].source).toEqual(undefined) | ||
class CustomError extends mybad.Error {} | ||
@@ -312,4 +398,12 @@ | ||
expect(errorObject.details).toEqual({}) | ||
// expect(errorObject.stack).toEqual(undefined) | ||
expect(errorObject.stack).toBeType('array') | ||
expect(errorObject.stack.length).toBeGreaterThan(0) | ||
expect(errorObject.stack[0]).toBeType('object') | ||
expect(errorObject.stack[0].column).toEqual(expect.any(Number)) | ||
expect(errorObject.stack[0].file).toEqual(`${ROOT_PATH}/test/test_errors.js`) | ||
expect(errorObject.stack[0].function).toEqual(expect.stringMatching(/^Object\.(?:test|<anonymous>|mybad\.Error)$/)) | ||
expect(errorObject.stack[0].line).toEqual(374) | ||
expect(errorObject.stack[0].source).toEqual(undefined) | ||
errorObject = mybad.Error.object(new TypeError('Baz')) | ||
@@ -333,3 +427,11 @@ | ||
expect(errorObject.details).toEqual({}) | ||
// expect(errorObject.stack).toEqual(undefined) | ||
expect(errorObject.stack).toBeType('array') | ||
expect(errorObject.stack.length).toBeGreaterThan(0) | ||
expect(errorObject.stack[0]).toBeType('object') | ||
expect(errorObject.stack[0].column).toEqual(expect.any(Number)) | ||
expect(errorObject.stack[0].file).toEqual(`${ROOT_PATH}/test/test_errors.js`) | ||
expect(errorObject.stack[0].function).toEqual(expect.stringMatching(/^Object\.(?:test|<anonymous>|mybad\.Error)$/)) | ||
expect(errorObject.stack[0].line).toEqual(402) | ||
expect(errorObject.stack[0].source).toEqual(undefined) | ||
}) | ||
@@ -336,0 +438,0 @@ |
Sorry, the diff of this file is not supported yet
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
35213
675
229
8