Skip to content

Commit

Permalink
Merge pull request #1 from rambler-digital-solutions/errors
Browse files Browse the repository at this point in the history
feat(errors): add errors
  • Loading branch information
andrepolischuk authored Aug 15, 2024
2 parents 2145de6 + 89f2894 commit d470391
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
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
```
28 changes: 28 additions & 0 deletions packages/errors/index.test.ts
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'})
})
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 = 'GenericError'
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"
}
}
1 change: 1 addition & 0 deletions packages/errors/tsconfig.json
1 change: 1 addition & 0 deletions packages/errors/typedoc.json

0 comments on commit d470391

Please sign in to comment.