-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from rambler-digital-solutions/errors
feat(errors): add errors
- Loading branch information
Showing
8 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Errors | ||
|
||
Enchanced generic errors | ||
|
||
## Install | ||
|
||
``` | ||
npm install -D @rambler-tech/errors | ||
``` | ||
|
||
or | ||
|
||
``` | ||
yarn add -D @rambler-tech/errors | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* eslint-disable import/no-unused-modules */ | ||
|
||
/** Generic error options */ | ||
export interface GenericErrorInput { | ||
code?: number | string | ||
details?: Record<string, any> | ||
} | ||
|
||
/** | ||
* 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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../tsconfig.package.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../typedoc.package.json |