Skip to content

Commit 2117af5

Browse files
feat(errors): add error class and functions (#113)
Signed-off-by: james-a-morris <[email protected]> Co-authored-by: amateima <[email protected]>
1 parent e0f8b8d commit 2117af5

File tree

16 files changed

+280
-45
lines changed

16 files changed

+280
-45
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ COPY packages/persistence-example/package.json ./packages/persistence-example/pa
2020
COPY packages/template/package.json ./packages/template/package.json
2121
COPY packages/typescript-config/package.json ./packages/typescript-config/package.json
2222
COPY packages/webhooks/package.json ./packages/webhooks/package.json
23+
COPY packages/error-handling/package.json ./packages/error-handling/package.json
2324

2425
# Build the dependencies into a node_modules folder
2526
RUN pnpm install --frozen-lockfile
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extension": [
3+
"ts"
4+
],
5+
"spec": "**/*.test.ts",
6+
"require": [
7+
"ts-node/register"
8+
],
9+
"recursive": true
10+
}

packages/error-handling/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# HTTP Error Handling Module
2+
3+
This module provides a comprehensive system for managing errors within the indexer package. It is meant to be a centralized constants repository for Indexer related errors
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// .eslintrc.js in the new package
2+
module.exports = {
3+
root:true,
4+
extends: ['@repo/eslint-config/library.js'],
5+
};
6+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"name": "@repo/error-handling",
3+
"version": "0.0.1",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"build": "tsc -b",
8+
"build:check": "tsc --noEmit",
9+
"watch": "tsc -b --watch",
10+
"fix": "pnpm format && pnpm lint",
11+
"format": "prettier --write src",
12+
"format:check": "prettier src --check",
13+
"lint": "eslint --fix",
14+
"lint:check": "eslint",
15+
"check": "pnpm format:check && pnpm lint:check && pnpm build:check",
16+
"test": "mocha",
17+
"coverage": "nyc mocha",
18+
"test:watch": "mocha --watch"
19+
},
20+
"keywords": [],
21+
"author": "",
22+
"license": "ISC",
23+
"dependencies": {
24+
"http-status-codes": "^2.3.0"
25+
},
26+
"exports": {
27+
".": "./dist/index.js"
28+
},
29+
"devDependencies": {
30+
"@istanbuljs/nyc-config-typescript": "^1.0.2",
31+
"@repo/eslint-config": "workspace:*",
32+
"@repo/typescript-config": "workspace:*",
33+
"@types/chai": "^4.3.17",
34+
"@types/mocha": "^10.0.7",
35+
"@types/node": "^16.11.10",
36+
"chai": "^4.5.0",
37+
"eslint": "^8.57.0",
38+
"mocha": "^10.7.0",
39+
"nyc": "^17.0.0",
40+
"prettier": "^3.3.3",
41+
"source-map-support": "^0.5.21",
42+
"ts-node": "^10.9.2",
43+
"typescript": "^5.5.4",
44+
"winston": "^3.13.1"
45+
}
46+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { IndexerError } from "./IndexerError";
2+
3+
export class AssertError extends IndexerError {
4+
constructor(message: string) {
5+
super(AssertError.name, message);
6+
}
7+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Generic Error class that should be used in the Indexer to
3+
* provide common error patterns to log
4+
*/
5+
export abstract class IndexerError extends Error {
6+
constructor(
7+
private readonly errorName: string,
8+
private readonly errorMessage: string,
9+
private readonly errorData?: Record<string, string>,
10+
) {
11+
super(errorMessage);
12+
}
13+
14+
/**
15+
* A function used by `JSON.stringify` to specify which data will be serialized
16+
* @returns A formatted JSON
17+
*/
18+
public toJSON(): Record<string, unknown> {
19+
return {
20+
error: this.errorName,
21+
message: this.errorMessage,
22+
data: this.errorData,
23+
};
24+
}
25+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { StatusCodes } from "http-status-codes";
2+
import { IndexerError } from "./IndexerError";
3+
4+
export { StatusCodes };
5+
6+
/**
7+
* Used to distinguish a similar design pattern as {@link IndexerError} but with
8+
* additional HTTP context
9+
* @see {@link IndexerError}
10+
*/
11+
export abstract class IndexerHTTPError extends IndexerError {
12+
constructor(
13+
private readonly httpStatusCode: StatusCodes,
14+
errorName: string,
15+
errorMessage: string,
16+
errorData?: Record<string, string>,
17+
) {
18+
super(errorName, errorMessage, errorData);
19+
}
20+
21+
/**
22+
* A function used by `JSON.stringify` to specify which data will be serialized
23+
* @returns A formatted JSON
24+
*/
25+
public toJSON(): Record<string, unknown> {
26+
return {
27+
statusCode: this.httpStatusCode,
28+
...super.toJSON(),
29+
};
30+
}
31+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from "./IndexerError";
2+
export * from "./IndexerHTTPError";
3+
export * from "./AssertError";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./errors";
2+
export * from "./utils";

0 commit comments

Comments
 (0)