@maestro-js/custom-errors 1.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,43 @@
1
+ declare function makeCustomError<Input = undefined, AdditionalAttributes extends Record<string, any> = Input extends Record<string, any> ? Input : {}, StatusCode extends number | CustomErrors.HttpStatusCodeName | null | undefined = undefined>({ name, message, additionalAttributes, httpStatusCode }: {
2
+ name?: string;
3
+ message?: string | ((input: Input) => string) | null | undefined;
4
+ additionalAttributes?: AdditionalAttributes | ((input: Input) => AdditionalAttributes) | null | undefined;
5
+ httpStatusCode?: StatusCode;
6
+ }): CustomErrors.CustomErrorConstructor<Input, AdditionalAttributes, StatusCode>;
7
+ declare const httpStatusCodeNames: {
8
+ readonly movedPermanently: 301;
9
+ readonly found: 302;
10
+ readonly redirect: 302;
11
+ readonly notModified: 304;
12
+ readonly badRequest: 400;
13
+ readonly unauthorized: 401;
14
+ readonly paymentRequired: 402;
15
+ readonly forbidden: 403;
16
+ readonly notFound: 404;
17
+ readonly conflict: 409;
18
+ readonly unprocessableContent: 422;
19
+ readonly validationError: 422;
20
+ readonly locked: 423;
21
+ readonly tooManyRequests: 429;
22
+ readonly internalServerError: 500;
23
+ };
24
+ declare function getHttpStatusCode(error: unknown): number | null;
25
+ declare const CustomErrors: {
26
+ create: typeof makeCustomError;
27
+ getHttpStatusCode: typeof getHttpStatusCode;
28
+ };
29
+ declare namespace CustomErrors {
30
+ type CustomError<AdditionalAttributes extends Record<string, any> = {}, StatusCode extends number | HttpStatusCodeName | null | undefined = undefined> = Error & AdditionalAttributes & {
31
+ httpStatusCode: StatusCode;
32
+ };
33
+ type CustomErrorConstructor<Input = undefined, AdditionalAttributes extends Record<string, any> = {}, StatusCode extends number | HttpStatusCodeName | null | undefined = undefined> = (undefined extends Input ? {
34
+ new (): CustomError<AdditionalAttributes, StatusCode>;
35
+ } : {
36
+ new (input: Input): CustomError<AdditionalAttributes, StatusCode>;
37
+ }) & {
38
+ readonly prototype: Error;
39
+ };
40
+ type HttpStatusCodeName = keyof typeof httpStatusCodeNames;
41
+ }
42
+
43
+ export { CustomErrors };
package/dist/index.js ADDED
@@ -0,0 +1,67 @@
1
+ // src/index.ts
2
+ function makeCustomError({
3
+ name,
4
+ message,
5
+ additionalAttributes,
6
+ httpStatusCode
7
+ }) {
8
+ class MyError extends Error {
9
+ constructor(input) {
10
+ const mes = typeof message === "function" ? message(input) : message ?? void 0;
11
+ super(mes);
12
+ this.message = mes ?? "";
13
+ if (name) {
14
+ this.name = name;
15
+ }
16
+ const extra = typeof additionalAttributes === "function" ? additionalAttributes(input) : additionalAttributes ?? {};
17
+ Object.entries(extra ?? {}).map(([key, value]) => {
18
+ this[key] = value;
19
+ });
20
+ if (httpStatusCode !== void 0) {
21
+ this["httpStatusCode"] = httpStatusCode;
22
+ }
23
+ }
24
+ }
25
+ if (name) {
26
+ MyError.prototype.name = name;
27
+ }
28
+ return MyError;
29
+ }
30
+ var httpStatusCodeNames = {
31
+ movedPermanently: 301,
32
+ found: 302,
33
+ redirect: 302,
34
+ notModified: 304,
35
+ badRequest: 400,
36
+ unauthorized: 401,
37
+ paymentRequired: 402,
38
+ forbidden: 403,
39
+ notFound: 404,
40
+ conflict: 409,
41
+ unprocessableContent: 422,
42
+ validationError: 422,
43
+ locked: 423,
44
+ tooManyRequests: 429,
45
+ internalServerError: 500
46
+ };
47
+ function getHttpStatusCode(error) {
48
+ if (error instanceof Error) {
49
+ const code = error["httpStatusCode"];
50
+ if (typeof code === "number") {
51
+ return code;
52
+ } else if (Object.keys(httpStatusCodeNames).includes(code)) {
53
+ return httpStatusCodeNames[code];
54
+ } else {
55
+ return null;
56
+ }
57
+ } else {
58
+ return null;
59
+ }
60
+ }
61
+ var CustomErrors = {
62
+ create: makeCustomError,
63
+ getHttpStatusCode
64
+ };
65
+ export {
66
+ CustomErrors
67
+ };
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@maestro-js/custom-errors",
3
+ "type": "module",
4
+ "exports": {
5
+ ".": {
6
+ "types": "./dist/index.d.ts",
7
+ "default": "./dist/index.js"
8
+ }
9
+ },
10
+ "devDependencies": {
11
+ "@types/node": "^22.19.11"
12
+ },
13
+ "version": "1.0.0-alpha.0",
14
+ "publishConfig": {
15
+ "access": "restricted"
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "license": "UNLICENSED",
21
+ "engines": {
22
+ "node": ">=22.18.0"
23
+ },
24
+ "repository": "https://github.com/Marcato-Partners/maestro-js",
25
+ "scripts": {
26
+ "build": "tsup --config ../../tsup.config.ts",
27
+ "typecheck": "tsc --noEmit",
28
+ "test": "beartest ./tests/**/*",
29
+ "format": "prettier --write src/ tests/",
30
+ "lint": "prettier --check src/ tests/"
31
+ }
32
+ }