feat: add basic error factory

This commit is contained in:
Leonard Kinday 2026-05-17 17:50:27 +02:00
parent ce239dd724
commit a8ac9d5638
Signed by: kinday
SSH key fingerprint: SHA256:oFBH7NZvG8XTgZ8aEmSUIQFAkuvBP5GKtHj/RvbWHRg
7 changed files with 1128 additions and 0 deletions

4
.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
*.log
/lib
/node_modules

4
.oxfmtrc.json Normal file
View file

@ -0,0 +1,4 @@
{
"$schema": "./node_modules/oxfmt/configuration_schema.json",
"ignorePatterns": []
}

40
package.json Normal file
View file

@ -0,0 +1,40 @@
{
"name": "coded-exception",
"version": "1.0.0",
"description": "",
"keywords": [],
"license": "Unlicense",
"author": "Leonard Kinday <leonard@kinday.se> (https://leonard.kinday.se/)",
"type": "module",
"main": "lib/index.js",
"exports": {
"types": "./lib/index.d.ts",
"default": "./lib/index.js"
},
"scripts": {
"build": "tsc",
"conform:format": "oxfmt --no-error-on-unmatched-pattern",
"conform:source": "oxlint --fix",
"conform": "concurrently 'pnpm:conform:*'",
"test:format": "oxfmt --check --no-error-on-unmatched-pattern",
"test:source": "oxlint",
"test:size": "size-limit",
"test": "concurrently 'pnpm:test:*'"
},
"devDependencies": {
"@size-limit/preset-small-lib": "12.1.0",
"@tsconfig/node-lts": "~24.0.0",
"concurrently": "9.2.1",
"oxfmt": "0.48.0",
"oxlint": "1.63.0",
"size-limit": "12.1.0",
"typescript": "6.0.3"
},
"size-limit": [
{
"path": "lib/index.js",
"limit": "512 B"
}
],
"packageManager": "pnpm@11.1.2+sha512.415a1cc25974731e75455c1468371be74c5aa5fb7621b50d4056d222451609f11412f23fd602e6169f1e060466641f798597e1be961a10688836a67b16569499"
}

1042
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load diff

8
pnpm-workspace.yaml Normal file
View file

@ -0,0 +1,8 @@
allowBuilds:
esbuild: true
simple-git-hooks: true
minimumReleaseAge: 10080 # 7 days
overrides:
coded-exception: "link:"
savePrefix: ""
useStderr: true

21
src/index.ts Normal file
View file

@ -0,0 +1,21 @@
export interface ErrorCode {
message: string;
}
export function createError<Name extends string, Codes extends Record<string, ErrorCode>>(
name: Name,
codes: Codes,
) {
return class extends Error {
public code: keyof Codes;
public name: Name;
constructor(code: keyof Codes, cause?: Error) {
const { message } = codes[code];
super(message);
this.cause = cause;
this.code = code;
this.name = name;
}
};
}

9
tsconfig.json Normal file
View file

@ -0,0 +1,9 @@
{
"extends": "@tsconfig/node-lts",
"compilerOptions": {
"declaration": true,
"outDir": "lib",
"rootDir": "src"
},
"files": ["src/index.ts"]
}