Skip to content

Commit be1008f

Browse files
feat: refactor Errors, remove unnecessary lines
Seems like many things were unnecessary, probably pre-es6.
1 parent d18242a commit be1008f

File tree

7 files changed

+80
-38
lines changed

7 files changed

+80
-38
lines changed

src/error/app.error.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { inspect } from 'util'
12
import { AppError } from './app.error'
23

34
const throwAppError = () => {
@@ -31,3 +32,24 @@ test('appError should work when Error.captureStacktrace is n/a', () => {
3132
expect(r.name).toBe('AppError')
3233
expect(r.stack).not.toBeUndefined()
3334
})
35+
36+
test('AppError log should NOT include constructor and data', () => {
37+
const err = new AppError('hello')
38+
// console.log(err)
39+
40+
expect(err.name).toBe('AppError')
41+
expect(err.constructor.name).toBe('AppError')
42+
expect(err.constructor).toBe(AppError)
43+
const s = filterStackTrace(inspect(err))
44+
// console.log(s)
45+
46+
expect(s).not.toContain('constructor')
47+
expect(s).not.toContain('data')
48+
})
49+
50+
function filterStackTrace(s: string): string {
51+
return s
52+
.split('\n')
53+
.filter(line => !line.trimStart().startsWith('at '))
54+
.join('\n')
55+
}

src/error/app.error.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,22 @@ import { ErrorData } from './error.model'
1010
* Based on: https://medium.com/@xpl/javascript-deriving-from-error-properly-8d2f8f315801
1111
*/
1212
export class AppError<DATA_TYPE extends ErrorData = ErrorData> extends Error {
13-
constructor(message: string, public data = {} as DATA_TYPE) {
13+
data!: DATA_TYPE
14+
15+
constructor(message: string, data = {} as DATA_TYPE) {
1416
super(message)
1517

16-
this.constructor = AppError
17-
;(this as any).__proto__ = AppError.prototype
1818
Object.defineProperty(this, 'name', {
1919
value: this.constructor.name,
2020
configurable: true,
2121
})
2222

23+
Object.defineProperty(this, 'data', {
24+
value: data,
25+
configurable: true,
26+
enumerable: false,
27+
})
28+
2329
if (Error.captureStackTrace) {
2430
Error.captureStackTrace(this, this.constructor)
2531
} else {

src/error/assert.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { inspect } from 'util'
12
import {
23
AssertionError,
34
_assert,
@@ -98,3 +99,26 @@ test('_assertIsNumber', () => {
9899
got : string]
99100
`)
100101
})
102+
103+
test('AssertionError printability', () => {
104+
const err = new AssertionError('error', { userFriendly: true })
105+
// console.log(err)
106+
107+
expect(err.name).toBe('AssertionError')
108+
expect(err.constructor.name).toBe('AssertionError')
109+
expect(err.constructor).toBe(AssertionError)
110+
const s = filterStackTrace(inspect(err))
111+
// console.log(s)
112+
113+
expect(s).not.toContain('constructor')
114+
expect(s).not.toContain('data')
115+
116+
expect(err.data).toEqual({ userFriendly: true })
117+
})
118+
119+
function filterStackTrace(s: string): string {
120+
return s
121+
.split('\n')
122+
.filter(line => !line.trimStart().startsWith('at '))
123+
.join('\n')
124+
}

src/error/assert.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -123,23 +123,7 @@ export function _assertTypeOf<T>(v: any, expectedType: string, message?: string)
123123
}
124124

125125
export class AssertionError extends AppError {
126-
constructor(message: string, data: ErrorData) {
126+
constructor(message: string, data = {} as ErrorData) {
127127
super(message, data)
128-
129-
this.constructor = AssertionError
130-
;(this as any).__proto__ = AssertionError.prototype
131-
Object.defineProperty(this, 'name', {
132-
value: this.constructor.name,
133-
configurable: true, // otherwise throws with "TypeError: Cannot redefine property: name"
134-
})
135-
136-
if (Error.captureStackTrace) {
137-
Error.captureStackTrace(this, this.constructor)
138-
} else {
139-
Object.defineProperty(this, 'stack', {
140-
value: new Error().stack, // eslint-disable-line unicorn/error-message
141-
configurable: true,
142-
})
143-
}
144128
}
145129
}

src/error/http.error.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { inspect } from 'util'
12
import { _errorToErrorObject } from '../index'
23
import { HttpError } from './http.error'
34

@@ -18,3 +19,26 @@ test('default error to match snapshot', async () => {
1819
expect(throwHttpError).toThrow(HttpError)
1920
await expect(throwHttpErrorAsync()).rejects.toThrow(HttpError)
2021
})
22+
23+
test('httpError printability', () => {
24+
const err = new HttpError('error', { httpStatusCode: 500 })
25+
// console.log(err)
26+
27+
expect(err.name).toBe('HttpError')
28+
expect(err.constructor.name).toBe('HttpError')
29+
expect(err.constructor).toBe(HttpError)
30+
const s = filterStackTrace(inspect(err))
31+
// console.log(s)
32+
33+
expect(s).not.toContain('constructor')
34+
expect(s).not.toContain('data')
35+
36+
expect(err.data).toEqual({ httpStatusCode: 500 })
37+
})
38+
39+
function filterStackTrace(s: string): string {
40+
return s
41+
.split('\n')
42+
.filter(line => !line.trimStart().startsWith('at '))
43+
.join('\n')
44+
}

src/error/http.error.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,5 @@ export class HttpError<
99
> extends AppError<DATA_TYPE> {
1010
constructor(message: string, data: DATA_TYPE) {
1111
super(message, data)
12-
13-
this.constructor = HttpError
14-
;(this as any).__proto__ = HttpError.prototype
15-
Object.defineProperty(this, 'name', {
16-
value: this.constructor.name,
17-
configurable: true, // otherwise throws with "TypeError: Cannot redefine property: name"
18-
})
19-
20-
if (Error.captureStackTrace) {
21-
Error.captureStackTrace(this, this.constructor)
22-
} else {
23-
Object.defineProperty(this, 'stack', {
24-
value: new Error().stack, // eslint-disable-line unicorn/error-message
25-
configurable: true,
26-
})
27-
}
2812
}
2913
}

src/promise/AggregatedError.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ export class AggregatedError<RESULT = any> extends Error {
2323
this.errors = mappedErrors
2424
this.results = results
2525

26-
this.constructor = AggregatedError
27-
;(this as any).__proto__ = AggregatedError.prototype
2826
Object.defineProperty(this, 'name', {
2927
value: this.constructor.name,
3028
configurable: true,

0 commit comments

Comments
 (0)