generated from sapphiredev/sapphire-template
-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: display the property that errored (#35)
- Loading branch information
Showing
9 changed files
with
137 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type { InspectOptionsStylized } from 'node:util'; | ||
import { BaseError, customInspectSymbolStackLess } from './BaseError'; | ||
|
||
export class CombinedPropertyError extends BaseError { | ||
public readonly errors: [PropertyKey, BaseError][]; | ||
|
||
public constructor(errors: [PropertyKey, BaseError][]) { | ||
super('Received one or more errors'); | ||
|
||
this.errors = errors; | ||
} | ||
|
||
protected [customInspectSymbolStackLess](depth: number, options: InspectOptionsStylized): string { | ||
if (depth < 0) { | ||
return options.stylize('[CombinedPropertyError]', 'special'); | ||
} | ||
|
||
const newOptions = { ...options, depth: options.depth === null ? null : options.depth! - 1, compact: true }; | ||
|
||
const padding = `\n ${options.stylize('|', 'undefined')} `; | ||
|
||
const header = `${options.stylize('CombinedPropertyError', 'special')} (${options.stylize(this.errors.length.toString(), 'number')})`; | ||
const message = options.stylize(this.message, 'regexp'); | ||
const errors = this.errors | ||
.map(([key, error]) => { | ||
const property = CombinedPropertyError.formatProperty(key, options); | ||
const body = error[customInspectSymbolStackLess](depth - 1, newOptions).replaceAll('\n', padding); | ||
|
||
return ` input${property}${padding}${body}`; | ||
}) | ||
.join('\n\n'); | ||
return `${header}\n ${message}\n\n${errors}`; | ||
} | ||
|
||
private static formatProperty(key: PropertyKey, options: InspectOptionsStylized): string { | ||
if (typeof key === 'string') return options.stylize(`.${key}`, 'symbol'); | ||
if (typeof key === 'number') return `[${options.stylize(key.toString(), 'number')}]`; | ||
return `[${options.stylize('Symbol', 'symbol')}(${key.description})]`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { inspect } from 'node:util'; | ||
import { CombinedPropertyError, ValidationError } from '../../../src'; | ||
|
||
describe('CombinedError', () => { | ||
const error = new CombinedPropertyError([ | ||
['foo', new ValidationError('StringValidator', 'Expected a string primitive', 42)], | ||
[2, new ValidationError('StringValidator', 'Expected a string primitive', true)], | ||
[Symbol('hello.there'), new ValidationError('StringValidator', 'Expected a string primitive', 75n)] | ||
]); | ||
|
||
test('GIVEN an instance THEN assigns fields correctly', () => { | ||
expect(error.message).toBe('Received one or more errors'); | ||
expect(error.errors).toHaveLength(3); | ||
expect(error.errors[0][1]).toBeInstanceOf(ValidationError); | ||
expect(error.errors[1][1]).toBeInstanceOf(ValidationError); | ||
expect(error.errors[2][1]).toBeInstanceOf(ValidationError); | ||
}); | ||
|
||
describe('inspect', () => { | ||
test('GIVEN an inspected instance THEN formats data correctly', () => { | ||
const content = inspect(error, { colors: false }); | ||
const expected = [ | ||
'CombinedPropertyError (3)', | ||
' Received one or more errors', | ||
'', | ||
' input.foo', | ||
' | ValidationError > StringValidator', | ||
' | Expected a string primitive', | ||
' | ', | ||
' | Received:', | ||
' | | 42', | ||
'', | ||
' input[2]', | ||
' | ValidationError > StringValidator', | ||
' | Expected a string primitive', | ||
' | ', | ||
' | Received:', | ||
' | | true', | ||
'', | ||
' input[Symbol(hello.there)]', | ||
' | ValidationError > StringValidator', | ||
' | Expected a string primitive', | ||
' | ', | ||
' | Received:', | ||
' | | 75n', | ||
'' | ||
]; | ||
|
||
expect(content.startsWith(expected.join('\n'))).toBe(true); | ||
}); | ||
|
||
test('GIVEN an inspected instance with negative depth THEN formats name only', () => { | ||
const content = inspect(error, { colors: false, depth: -1 }); | ||
const expected = [ | ||
'[CombinedPropertyError]' // | ||
]; | ||
|
||
expect(content.startsWith(expected.join('\n'))).toBe(true); | ||
}); | ||
}); | ||
}); |