From 893b1116ad518e96388d5fb70284643ba56136f1 Mon Sep 17 00:00:00 2001 From: Andrey Polischuk Date: Tue, 13 Aug 2024 18:26:20 +0300 Subject: [PATCH 1/2] feat(errors): add errors --- .size-limit.json | 4 ++++ README.md | 1 + packages/errors/README.md | 15 ++++++++++++++ packages/errors/index.test.ts | 28 +++++++++++++++++++++++++ packages/errors/index.ts | 39 +++++++++++++++++++++++++++++++++++ packages/errors/package.json | 12 +++++++++++ packages/errors/tsconfig.json | 9 ++++++++ packages/errors/typedoc.json | 5 +++++ 8 files changed, 113 insertions(+) create mode 100644 packages/errors/README.md create mode 100644 packages/errors/index.test.ts create mode 100644 packages/errors/index.ts create mode 100644 packages/errors/package.json create mode 100644 packages/errors/tsconfig.json create mode 100644 packages/errors/typedoc.json diff --git a/.size-limit.json b/.size-limit.json index d6bf74a..ef56bd7 100644 --- a/.size-limit.json +++ b/.size-limit.json @@ -3,6 +3,10 @@ "path": "packages/cookie-storage/dist/index.js", "limit": "990 B" }, + { + "path": "packages/errors/dist/index.js", + "limit": "290 B" + }, { "path": "packages/local-storage/dist/index.js", "limit": "290 B" diff --git a/README.md b/README.md index 6357652..d06677b 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ Common utils used by Rambler team ## Packages - [@rambler-tech/cookie-storage](packages/cookie-storage) +- [@rambler-tech/errors](packages/errors) - [@rambler-tech/local-storage](packages/local-storage) - [@rambler-tech/session-storage](packages/session-storage) - [@rambler-tech/lhci-report](packages/lhci-report) diff --git a/packages/errors/README.md b/packages/errors/README.md new file mode 100644 index 0000000..902da9f --- /dev/null +++ b/packages/errors/README.md @@ -0,0 +1,15 @@ +# Errors + +Enchanced generic errors + +## Install + +``` +npm install -D @rambler-tech/errors +``` + +or + +``` +yarn add -D @rambler-tech/errors +``` diff --git a/packages/errors/index.test.ts b/packages/errors/index.test.ts new file mode 100644 index 0000000..6a05404 --- /dev/null +++ b/packages/errors/index.test.ts @@ -0,0 +1,28 @@ +import {GenericError} from '.' + +test('base error', () => { + const error = new GenericError('failed') + + expect(error).toBeInstanceOf(Error) + expect(error).toBeInstanceOf(GenericError) + expect(error.name).toBe('GenericError') + expect(error.message).toBe('failed') + expect(error.code).toBe('unknown') + expect(error.details).toBeUndefined() +}) + +test('error with details', () => { + const error = new GenericError('api failed', { + code: 'code', + details: { + xid: 'xid' + } + }) + + expect(error).toBeInstanceOf(Error) + expect(error).toBeInstanceOf(GenericError) + expect(error.name).toBe('GenericError') + expect(error.message).toBe('api failed') + expect(error.code).toBe('code') + expect(error.details).toEqual({xid: 'xid'}) +}) diff --git a/packages/errors/index.ts b/packages/errors/index.ts new file mode 100644 index 0000000..4875d28 --- /dev/null +++ b/packages/errors/index.ts @@ -0,0 +1,39 @@ +/* eslint-disable import/no-unused-modules */ + +/** Generic error options */ +export interface GenericErrorInput { + code?: number | string + details?: Record +} + +/** + * Generic error + * + * Base usage + * ```ts + * new GenericError('failed') + * ``` + * + * Usage with details + * ```ts + * new GenericError('api failed', { + * code: 'account_suspended', + * details: { + * xid: 'k1k2jn31kjnasxa9' + * } + * }) + * ``` + */ +export class GenericError extends Error { + code: GenericErrorInput['code'] + details?: GenericErrorInput['details'] + + constructor(message: string, input?: GenericErrorInput) { + super() + this.name = 'GenericError' + this.message = message.toString().toLowerCase() + this.code = input?.code ?? 'unknown' + this.details = input?.details + Object.setPrototypeOf(this, GenericError.prototype) + } +} diff --git a/packages/errors/package.json b/packages/errors/package.json new file mode 100644 index 0000000..a09ce36 --- /dev/null +++ b/packages/errors/package.json @@ -0,0 +1,12 @@ +{ + "name": "@rambler-tech/errors", + "version": "0.0.0", + "main": "dist", + "module": "dist", + "types": "dist/index.d.ts", + "license": "MIT", + "sideEffects": false, + "publishConfig": { + "access": "public" + } +} diff --git a/packages/errors/tsconfig.json b/packages/errors/tsconfig.json new file mode 100644 index 0000000..b81d8e5 --- /dev/null +++ b/packages/errors/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "module": "commonjs", + "baseUrl": ".", + "outDir": "dist" + }, + "include": [".", "../../types"] +} diff --git a/packages/errors/typedoc.json b/packages/errors/typedoc.json new file mode 100644 index 0000000..fd9d16b --- /dev/null +++ b/packages/errors/typedoc.json @@ -0,0 +1,5 @@ +{ + "extends": "@rambler-tech/typedoc-config", + "readme": "../../README.md", + "entryPoints": ["./index.ts"] +} From 3bfbcd50821a253a05bdea9dff919f1bb4128d78 Mon Sep 17 00:00:00 2001 From: Andrey Polischuk Date: Wed, 14 Aug 2024 18:38:00 +0300 Subject: [PATCH 2/2] docs: fix docs --- packages/errors/tsconfig.json | 10 +--------- packages/errors/typedoc.json | 6 +----- 2 files changed, 2 insertions(+), 14 deletions(-) mode change 100644 => 120000 packages/errors/tsconfig.json mode change 100644 => 120000 packages/errors/typedoc.json diff --git a/packages/errors/tsconfig.json b/packages/errors/tsconfig.json deleted file mode 100644 index b81d8e5..0000000 --- a/packages/errors/tsconfig.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "extends": "../../tsconfig.json", - "compilerOptions": { - "module": "commonjs", - "baseUrl": ".", - "outDir": "dist" - }, - "include": [".", "../../types"] -} diff --git a/packages/errors/tsconfig.json b/packages/errors/tsconfig.json new file mode 120000 index 0000000..238bf1b --- /dev/null +++ b/packages/errors/tsconfig.json @@ -0,0 +1 @@ +../../tsconfig.package.json \ No newline at end of file diff --git a/packages/errors/typedoc.json b/packages/errors/typedoc.json deleted file mode 100644 index fd9d16b..0000000 --- a/packages/errors/typedoc.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "extends": "@rambler-tech/typedoc-config", - "readme": "../../README.md", - "entryPoints": ["./index.ts"] -} diff --git a/packages/errors/typedoc.json b/packages/errors/typedoc.json new file mode 120000 index 0000000..213b456 --- /dev/null +++ b/packages/errors/typedoc.json @@ -0,0 +1 @@ +../../typedoc.package.json \ No newline at end of file