Skip to content

Commit

Permalink
feat(errors): add errors
Browse files Browse the repository at this point in the history
  • Loading branch information
andrepolischuk committed Aug 13, 2024
1 parent 230cc52 commit 7471e55
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/errors/README.md
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
```
26 changes: 26 additions & 0 deletions packages/errors/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {GenericError} from '.'

test('base error', () => {
const error = new GenericError('failed')

expect(error).toBeInstanceOf(Error)
expect(error).toBeInstanceOf(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.message).toBe('api failed')
expect(error.code).toBe('code')
expect(error.details).toEqual({xid: 'xid'})
})
39 changes: 39 additions & 0 deletions packages/errors/index.ts
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 = 'ApiError'
this.message = message.toString().toLowerCase()
this.code = input?.code ?? 'unknown'
this.details = input?.details
Object.setPrototypeOf(this, GenericError.prototype)
}
}
12 changes: 12 additions & 0 deletions packages/errors/package.json
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"
}
}
9 changes: 9 additions & 0 deletions packages/errors/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"baseUrl": ".",
"outDir": "dist"
},
"include": [".", "../../types"]
}
5 changes: 5 additions & 0 deletions packages/errors/typedoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "@rambler-tech/typedoc-config",
"readme": "../../README.md",
"entryPoints": ["./index.ts"]
}

0 comments on commit 7471e55

Please sign in to comment.