diff --git a/.size-limit.json b/.size-limit.json index 4889ac3..66e29f4 100644 --- a/.size-limit.json +++ b/.size-limit.json @@ -19,6 +19,10 @@ "path": "packages/dom/dist/index.js", "limit": "500 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 5079ad2..cb6910b 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,9 @@ Common utils used by Rambler team - [@rambler-tech/crypto](packages/crypto) - [@rambler-tech/debug](packages/debug) - [@rambler-tech/dom](packages/dom) +- [@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) - [@rambler-tech/local-storage](packages/local-storage) - [@rambler-tech/react](packages/react) 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 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 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