|
1 | | -import type { MappingTarget } from './types'; |
| 1 | +import type { MappingTarget } from './types.js'; |
| 2 | +import { describeUnknownValue } from './value-format.js'; |
2 | 3 |
|
3 | 4 | export type AlgorithmIdentifierErrorCode = |
| 5 | + | 'INVALID_ARGUMENT' |
| 6 | + | 'REGISTRY_INVARIANT' |
4 | 7 | | 'UNKNOWN_ALGORITHM' |
5 | 8 | | 'UNKNOWN_IDENTIFIER' |
6 | 9 | | 'UNSUPPORTED_MAPPING'; |
7 | 10 |
|
| 11 | +interface ErrorOptionsLike { |
| 12 | + readonly cause?: unknown; |
| 13 | +} |
| 14 | + |
| 15 | +function defineReadonlyNonEnumerableField( |
| 16 | + target: object, |
| 17 | + propertyName: string, |
| 18 | + value: unknown, |
| 19 | +): void { |
| 20 | + Object.defineProperty(target, propertyName, { |
| 21 | + value, |
| 22 | + enumerable: false, |
| 23 | + writable: false, |
| 24 | + configurable: false, |
| 25 | + }); |
| 26 | +} |
| 27 | + |
8 | 28 | export class AlgorithmIdentifierError extends Error { |
9 | 29 | readonly code: AlgorithmIdentifierErrorCode; |
10 | 30 |
|
11 | | - constructor(code: AlgorithmIdentifierErrorCode, message: string) { |
12 | | - super(message); |
| 31 | + constructor(code: AlgorithmIdentifierErrorCode, message: string, options?: ErrorOptionsLike) { |
| 32 | + super(message, options); |
| 33 | + Object.setPrototypeOf(this, new.target.prototype); |
13 | 34 | this.name = new.target.name; |
14 | 35 | this.code = code; |
15 | 36 | } |
16 | 37 | } |
17 | 38 |
|
18 | 39 | export class UnknownAlgorithmError extends AlgorithmIdentifierError { |
19 | | - readonly algorithm: string; |
| 40 | + readonly algorithm!: string; |
20 | 41 |
|
21 | 42 | constructor(algorithm: string) { |
22 | | - super('UNKNOWN_ALGORITHM', `Unknown algorithm '${algorithm}'.`); |
23 | | - this.algorithm = algorithm; |
| 43 | + super('UNKNOWN_ALGORITHM', `Unknown algorithm '${describeUnknownValue(algorithm)}'.`); |
| 44 | + defineReadonlyNonEnumerableField(this, 'algorithm', algorithm); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +export class InvalidArgumentError extends AlgorithmIdentifierError { |
| 49 | + readonly argumentName: string; |
| 50 | + |
| 51 | + constructor(argumentName: string, message: string, options?: ErrorOptionsLike) { |
| 52 | + super('INVALID_ARGUMENT', `Invalid argument '${argumentName}': ${message}`, options); |
| 53 | + this.argumentName = argumentName; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +export class RegistryInvariantError extends AlgorithmIdentifierError { |
| 58 | + constructor(message: string, options?: ErrorOptionsLike) { |
| 59 | + super('REGISTRY_INVARIANT', message, options); |
24 | 60 | } |
25 | 61 | } |
26 | 62 |
|
27 | 63 | export class UnknownIdentifierError extends AlgorithmIdentifierError { |
28 | | - readonly identifierType: MappingTarget; |
29 | | - readonly identifierValue: string | number; |
| 64 | + readonly identifierType!: MappingTarget; |
| 65 | + readonly identifierValue!: string | number; |
30 | 66 |
|
31 | 67 | constructor(identifierType: MappingTarget, identifierValue: string | number) { |
32 | 68 | super( |
33 | 69 | 'UNKNOWN_IDENTIFIER', |
34 | | - `Unknown ${identifierType} identifier '${String(identifierValue)}'.`, |
| 70 | + `Unknown ${identifierType} identifier '${describeUnknownValue(identifierValue)}'.`, |
35 | 71 | ); |
36 | | - this.identifierType = identifierType; |
37 | | - this.identifierValue = identifierValue; |
| 72 | + defineReadonlyNonEnumerableField(this, 'identifierType', identifierType); |
| 73 | + defineReadonlyNonEnumerableField(this, 'identifierValue', identifierValue); |
38 | 74 | } |
39 | 75 | } |
40 | 76 |
|
41 | 77 | export class UnsupportedMappingError extends AlgorithmIdentifierError { |
42 | | - readonly mapping: MappingTarget; |
43 | | - readonly algorithm: string; |
| 78 | + readonly mapping!: MappingTarget; |
| 79 | + readonly algorithm!: string; |
44 | 80 |
|
45 | 81 | constructor(mapping: MappingTarget, algorithm: string) { |
46 | | - super('UNSUPPORTED_MAPPING', `Algorithm '${algorithm}' does not support ${mapping} mapping.`); |
47 | | - this.mapping = mapping; |
48 | | - this.algorithm = algorithm; |
| 82 | + super( |
| 83 | + 'UNSUPPORTED_MAPPING', |
| 84 | + `Algorithm '${describeUnknownValue(algorithm)}' does not support ${mapping} mapping.`, |
| 85 | + ); |
| 86 | + defineReadonlyNonEnumerableField(this, 'mapping', mapping); |
| 87 | + defineReadonlyNonEnumerableField(this, 'algorithm', algorithm); |
49 | 88 | } |
50 | 89 | } |
0 commit comments