Skip to content

Commit

Permalink
Upgrade erreur to v7
Browse files Browse the repository at this point in the history
  • Loading branch information
etienne-dldc committed Apr 22, 2024
1 parent ae4f1fe commit 4187313
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 92 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"test": "pnpm run lint && vitest run --coverage",
"test:run": "vitest run",
"test:watch": "vitest --watch",
"test:watch:coverage": "vitest --watch --coverage",
"typecheck": "tsc",
"typecheck:watch": "tsc --watch"
},
Expand Down Expand Up @@ -85,12 +86,12 @@
"root": true
},
"dependencies": {
"@dldc/erreur": "^6.0.6"
"@dldc/erreur": "7.0.0-1"
},
"devDependencies": {
"@types/node": "^20.12.7",
"@typescript-eslint/eslint-plugin": "^7.6.0",
"@typescript-eslint/parser": "^7.6.0",
"@typescript-eslint/eslint-plugin": "^7.7.1",
"@typescript-eslint/parser": "^7.7.1",
"@vitest/coverage-v8": "^1.5.0",
"auto-changelog": "^2.4.0",
"eslint": "^8.56.0",
Expand Down
92 changes: 43 additions & 49 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 36 additions & 31 deletions src/erreur.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { TKey } from '@dldc/erreur';
import { Erreur, Key } from '@dldc/erreur';
import { createErreurStore } from '@dldc/erreur';

export type TZenjsonErreurData =
| { kind: 'CustomTypeNotFound'; typeName: string }
Expand All @@ -8,32 +7,38 @@ export type TZenjsonErreurData =
| { kind: 'InvalidSerializedSpecialValue'; value: string }
| { kind: 'DuplicatedCustomTypeName'; typeName: string };

export const ZenjsonErreurKey: TKey<TZenjsonErreurData, false> = Key.create<TZenjsonErreurData>('ZenjsonErreur');

export const ZenjsonErreur = {
CustomTypeNotFound: (typeName: string) => {
return Erreur.create(new Error('Unexpected: custom type not found'))
.with(ZenjsonErreurKey.Provider({ kind: 'CustomTypeNotFound', typeName }))
.withName('ZenjsonErreur');
},
KeyNotFound: (keyName: string) => {
return Erreur.create(new Error(`Key ${keyName} not found`))
.with(ZenjsonErreurKey.Provider({ kind: 'KeyNotFound', keyName }))
.withName('ZenjsonErreur');
},
UnexpectedSpecialValue: (value: unknown) => {
return Erreur.create(new Error(`Unexpected special number value ${String(value)}`))
.with(ZenjsonErreurKey.Provider({ kind: 'UnexpectedSpecialValue', value }))
.withName('ZenjsonErreur');
},
InvalidSerializedSpecialValue: (value: string) => {
return Erreur.create(new Error(`Invalid serialized special number: ${value}`))
.with(ZenjsonErreurKey.Provider({ kind: 'InvalidSerializedSpecialValue', value }))
.withName('ZenjsonErreur');
},
DuplicatedCustomTypeName: (typeName: string) => {
return Erreur.create(new Error(`Invalid custom type list: duplicated type name: "${typeName}"`))
.with(ZenjsonErreurKey.Provider({ kind: 'DuplicatedCustomTypeName', typeName }))
.withName('ZenjsonErreur');
},
};
const ZenjsonErreurInternal = createErreurStore<TZenjsonErreurData>();

export const ZenjsonErreur = ZenjsonErreurInternal.asReadonly;

export function throwCustomTypeNotFound(typeName: string): never {
return ZenjsonErreurInternal.setAndThrow(`Unexpected: custom type "${typeName}" not found`, {
kind: 'CustomTypeNotFound',
typeName,
});
}

export function throwKeyNotFound(keyName: string): never {
return ZenjsonErreurInternal.setAndThrow(`Key "${keyName}" not found`, { kind: 'KeyNotFound', keyName });
}

export function throwUnexpectedSpecialValue(value: unknown): never {
return ZenjsonErreurInternal.setAndThrow(`Unexpected special number value ${String(value)}`, {
kind: 'UnexpectedSpecialValue',
value,
});
}

export function throwInvalidSerializedSpecialValue(value: string): never {
return ZenjsonErreurInternal.setAndThrow(`Invalid serialized special number: ${value}`, {
kind: 'InvalidSerializedSpecialValue',
value,
});
}

export function throwDuplicatedCustomTypeName(typeName: string): never {
return ZenjsonErreurInternal.setAndThrow(`Invalid custom type list: duplicated type name: "${typeName}"`, {
kind: 'DuplicatedCustomTypeName',
typeName,
});
}
2 changes: 1 addition & 1 deletion src/mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { ZenjsonErreur, ZenjsonErreurKey, type TZenjsonErreurData } from './erreur';
export { ZenjsonErreur, type TZenjsonErreurData } from './erreur';
export { createRestore, restore } from './restore';
export { createSanitize, sanitize } from './sanitize';
export { createTypedKey, createTypedMap, type ITypedKey, type ITypedMap } from './typedMap';
Expand Down
4 changes: 2 additions & 2 deletions src/restore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ZenjsonErreur } from './erreur';
import { throwCustomTypeNotFound } from './erreur';
import type { ITypedMap } from './typedMap';
import { createTypedMap } from './typedMap';
import type { IRestoreContext, TCustomTypes } from './types';
Expand Down Expand Up @@ -28,7 +28,7 @@ function restoreInternal(data: unknown, customTypes: TCustomTypes, state: ITyped
const [typeName, sanitized] = item;
const type = customTypes.find((t) => t.name === typeName);
if (!type) {
throw ZenjsonErreur.CustomTypeNotFound(typeName);
return throwCustomTypeNotFound(typeName);
}
return type.restore(sanitized, restoreCtx);
}
Expand Down
4 changes: 2 additions & 2 deletions src/typedMap.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ZenjsonErreur } from './erreur';
import { throwKeyNotFound } from './erreur';

const TYPED_KEY = Symbol('TYPED_KEY');

Expand Down Expand Up @@ -53,7 +53,7 @@ export function createTypedMap(): ITypedMap {
function getOrFail<T>(key: ITypedKey<T>): T {
const value = get(key);
if (value === undefined) {
throw ZenjsonErreur.KeyNotFound(key.name);
return throwKeyNotFound(key.name);
}
return value;
}
Expand Down
12 changes: 8 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { ZenjsonErreur } from './erreur';
import {
throwDuplicatedCustomTypeName,
throwInvalidSerializedSpecialValue,
throwUnexpectedSpecialValue,
} from './erreur';
import type { ITypedMap } from './typedMap';

export interface ICheckContext {
Expand Down Expand Up @@ -63,7 +67,7 @@ export const specialNumberType: ICustomType<number, 'Infinity' | '-Infinity' | '
if (val === -Infinity) {
return '-Infinity';
}
throw ZenjsonErreur.UnexpectedSpecialValue(val);
return throwUnexpectedSpecialValue(val);
},
restore: (str) => {
if (str === 'NaN') {
Expand All @@ -75,7 +79,7 @@ export const specialNumberType: ICustomType<number, 'Infinity' | '-Infinity' | '
if (str === 'Infinity') {
return Infinity;
}
throw ZenjsonErreur.InvalidSerializedSpecialValue(str);
return throwInvalidSerializedSpecialValue(str);
},
};

Expand All @@ -93,7 +97,7 @@ export function validateTypes(types: TCustomTypes): void {
const names = new Set();
types.forEach((type) => {
if (names.has(type.name)) {
throw ZenjsonErreur.DuplicatedCustomTypeName(type.name);
return throwDuplicatedCustomTypeName(type.name);
}
names.add(type.name);
});
Expand Down

0 comments on commit 4187313

Please sign in to comment.