@grimen/mybad
Advanced tools
Comparing version 0.2.3 to 0.2.4
{ | ||
"name": "@grimen/mybad", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "My friendly error base class - for Node/JavaScript.", | ||
@@ -47,3 +47,3 @@ "keywords": [ | ||
"babel-eslint": "^10.1.0", | ||
"eslint": "^7.19.0", | ||
"eslint": "^7.22.0", | ||
"jest": "^26.6.3", | ||
@@ -50,0 +50,0 @@ "jest-tobetype": "^1.2.3", |
@@ -223,3 +223,3 @@ | ||
- [**`python-mybase`**](https://github.com/grimen/python-mybase) - *"My friendly error base class - for Python"* | ||
- [**`python-mybad`**](https://github.com/grimen/python-mybad) - *"My friendly error base class - for Python"* | ||
@@ -226,0 +226,0 @@ |
@@ -24,3 +24,2 @@ | ||
const DEFAULT_ERROR_INDENT = 4 | ||
const DEFAULT_ERROR_DEPTH = null | ||
const DEFAULT_ERROR_COLORS = true | ||
@@ -38,2 +37,9 @@ const DEFAULT_ERROR_VERBOSE = true | ||
#error = undefined | ||
#id = undefined | ||
#key = undefined | ||
#code = undefined | ||
#message = undefined | ||
#details = undefined | ||
constructor (...args) { | ||
@@ -51,2 +57,7 @@ let error = args.find((arg) => { | ||
error = new Error(`${error}`) | ||
error.stack = error.stack.split('\n') | ||
.filter(stackLine => !/new [A-Za-z_]+Error/gmi.test(stackLine)) | ||
.filter(stackLine => !stackLine.includes('js-mybad/src')) | ||
.join('\n') | ||
} | ||
@@ -92,57 +103,60 @@ | ||
this._error = error | ||
this._id = id | ||
this._key = key | ||
this._code = code | ||
this._message = message | ||
this._details = details | ||
this._error = error | ||
if (error) { | ||
this.stack = error?.stack || this.stack | ||
} | ||
this.#error = error | ||
this.#id = id | ||
this.#key = key | ||
this.#code = code | ||
this.#message = message | ||
this.#details = details | ||
} | ||
get error () { | ||
return this._error | ||
return this.#error | ||
} | ||
set error (value) { | ||
this._error = value | ||
this.#error = value | ||
} | ||
get id () { | ||
return this._id | ||
return this.#id | ||
} | ||
set id (value) { | ||
this._id = value | ||
this.#id = value | ||
} | ||
get key () { | ||
return this._key | ||
return this.#key | ||
} | ||
set key (value) { | ||
this._key = value | ||
this.#key = value | ||
} | ||
get code () { | ||
return this._code | ||
return this.#code | ||
} | ||
set code (value) { | ||
this._code = value | ||
this.#code = value | ||
} | ||
get message () { | ||
return this._message | ||
return this.#message | ||
} | ||
set message (value) { | ||
this._message = value | ||
this.#message = value | ||
} | ||
get details () { | ||
return this._details || {} | ||
return this.#details || {} | ||
} | ||
set details (value) { | ||
this._details = value | ||
this.#details = value | ||
} | ||
@@ -175,9 +189,10 @@ | ||
if (stackframeLine.includes('(')) { | ||
stackframeData = stackframeLine.match(/^at ([^\()]+)\((.+)(?:\:(\d+)\:(\d+)\))/i) | ||
stackframeData = stackframeLine.match(/^at ([^()]+)\((.+)(?::(\d+):(\d+)\))/i) | ||
} else { | ||
stackframeData = stackframeLine.match(/^at (.+)(?:\:(\d+)\:(\d+))/i) | ||
stackframeData = stackframeLine.match(/^at (.+)(?::(\d+):(\d+))/i) | ||
} | ||
let [ | ||
// eslint-disable-next-line | ||
_, | ||
@@ -360,2 +375,6 @@ functionName, | ||
static from (error) { | ||
return this.cast(error) | ||
} | ||
static object (error, attrs) { | ||
@@ -365,9 +384,9 @@ const extendedError = BaseError.cast(error) | ||
return { | ||
'type': error.constructor.name, | ||
'id': extendedError.id, | ||
'code': extendedError.code, | ||
'key': extendedError.key, | ||
'message': extendedError.message, | ||
'details': extendedError.details, | ||
'stack': extendedError.stackobjects, | ||
type: error.constructor.name, | ||
id: extendedError.id, | ||
code: extendedError.code, | ||
key: extendedError.key, | ||
message: extendedError.message, | ||
details: extendedError.details, | ||
stack: extendedError.stackobjects, | ||
...attrs, | ||
@@ -374,0 +393,0 @@ } |
@@ -1,2 +0,2 @@ | ||
/* global jest describe test expect */ | ||
/* global describe test expect */ | ||
@@ -12,3 +12,2 @@ // ========================================= | ||
const stripAnsi = require('strip-ansi') | ||
const semver = require('semver') | ||
@@ -282,3 +281,3 @@ | ||
expect(error.stackobjects[0].function).toEqual(expect.stringMatching(/^Object\.(?:test|<anonymous>|mybad\.Error)$/)) | ||
expect(error.stackobjects[0].line).toEqual(271) | ||
expect(error.stackobjects[0].line).toEqual(270) | ||
expect(error.stackobjects[0].source).toEqual(undefined) | ||
@@ -294,3 +293,3 @@ | ||
expect(error.stackobjects[0].function).toEqual(expect.stringMatching(/^Object\.(?:test|<anonymous>|mybad\.Error)$/)) | ||
expect(error.stackobjects[0].line).toEqual(284) | ||
expect(error.stackobjects[0].line).toEqual(270) | ||
expect(error.stackobjects[0].source).toEqual(undefined) | ||
@@ -326,3 +325,3 @@ }) | ||
expect(error.data.stack[0].function).toEqual(expect.stringMatching(/^Object\.(?:test|<anonymous>|mybad\.Error)$/)) | ||
expect(error.data.stack[0].line).toEqual(297) | ||
expect(error.data.stack[0].line).toEqual(296) | ||
expect(error.data.stack[0].source).toEqual(undefined) | ||
@@ -344,3 +343,3 @@ | ||
expect(error.data.stack[0].function).toEqual(expect.stringMatching(/^Object\.(?:test|<anonymous>|mybad\.Error)$/)) | ||
expect(error.data.stack[0].line).toEqual(326) | ||
expect(error.data.stack[0].line).toEqual(296) | ||
expect(error.data.stack[0].source).toEqual(undefined) | ||
@@ -375,3 +374,3 @@ }) | ||
expect(data.stack[0].function).toEqual(expect.stringMatching(/^Object\.(?:test|<anonymous>|mybad\.Error)$/)) | ||
expect(data.stack[0].line).toEqual(345) | ||
expect(data.stack[0].line).toEqual(344) | ||
expect(data.stack[0].source).toEqual(undefined) | ||
@@ -400,3 +399,3 @@ | ||
expect(data.stack[0].function).toEqual(expect.stringMatching(/^Object\.(?:test|<anonymous>|mybad\.Error)$/)) | ||
expect(data.stack[0].line).toEqual(373) | ||
expect(data.stack[0].line).toEqual(344) | ||
expect(data.stack[0].source).toEqual(undefined) | ||
@@ -466,6 +465,19 @@ }) | ||
test('.from()', async () => { | ||
expect('from' in mybad.Error).toBe(true) | ||
let error = mybad.Error.from(new mybad.Error('Foo')) | ||
expect(error).toBeInstanceOf(mybad.Error) | ||
error = mybad.Error.from(new TypeError('Bar')) | ||
expect(error).toBeInstanceOf(mybad.Error) | ||
}) | ||
test('.object()', async () => { | ||
expect('object' in mybad.Error).toBe(true) | ||
let errorObject = mybad.Error.object(new mybad.Error('Foo')) | ||
let error = new mybad.Error('Foo') | ||
let errorObject = mybad.Error.object(error) | ||
@@ -495,3 +507,3 @@ expect(errorObject).toBeType('object') | ||
expect(errorObject.stack[0].function).toEqual(expect.stringMatching(/^Object\.(?:test|<anonymous>|mybad\.Error)$/)) | ||
expect(errorObject.stack[0].line).toEqual(462) | ||
expect(errorObject.stack[0].line).toEqual(473) | ||
expect(errorObject.stack[0].source).toEqual(undefined) | ||
@@ -501,3 +513,4 @@ | ||
errorObject = mybad.Error.object(new CustomError('Bar')) | ||
error = new CustomError('Bar') | ||
errorObject = mybad.Error.object(error) | ||
@@ -527,3 +540,3 @@ expect(errorObject).toBeType('object') | ||
expect(errorObject.stack[0].function).toEqual(expect.stringMatching(/^Object\.(?:test|<anonymous>|mybad\.Error)$/)) | ||
expect(errorObject.stack[0].line).toEqual(492) | ||
expect(errorObject.stack[0].line).toEqual(504) | ||
expect(errorObject.stack[0].source).toEqual(undefined) | ||
@@ -556,3 +569,3 @@ | ||
expect(errorObject.stack[0].function).toEqual(expect.stringMatching(/^Object\.(?:test|<anonymous>|mybad\.Error)$/)) | ||
expect(errorObject.stack[0].line).toEqual(520) | ||
expect(errorObject.stack[0].line).toEqual(533) | ||
expect(errorObject.stack[0].source).toEqual(undefined) | ||
@@ -559,0 +572,0 @@ }) |
Sorry, the diff of this file is not supported yet
41850
14
783