Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(errors): add errors #1

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading