Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/lint-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
matrix:
# the Node.js versions to build on
node-version: [16.x, 18.x]
node-version: [20.x, 22.x]

steps:
- uses: actions/checkout@v2
Expand Down
46 changes: 44 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ $ npm install @nayotta/errors --save
// COMMONJS
const {
ToBeDoneError,
errors
errors,
JsError
} = require('@nayotta/errors')
// ESM
import {
ToBeDoneError,
errors
errors,
JsError
} from '@nayotta/errors'

async function do () {
Expand All @@ -41,6 +43,40 @@ async function run () {
throw res
}
}

// custom errors
const TEST_ERROR_NAME = 'TEST_ERROR'
class TestError extends Error {
public readonly name: string = TEST_ERROR_NAME
public readonly code: number = 400
public message: string = 'test'
constructor (message?: string) {
super()
this.message += `: ${message}`
}
}

function isTestError (err: Error) {
return errors.isError(err, { name: TEST_ERROR_NAME })
}

async function doSomethingCustom () {
throw new TestError('do something custom')
}

async function runCustomErr () {
const res = await doSomethingCustom().catch(err => err)
if (res instanceof Error) {
if (isTestError(res)) {
console.log(res.name) // TEST_ERROR
console.log(res.message) // echo: do something custom
console.log(res.code) // 400
console.log(res)
return
}
throw res
}
}
```

### errors
Expand Down Expand Up @@ -129,6 +165,12 @@ async function run () {
- code: `403`
- message: `type not allowed: <type>:<name>`

#### UnexpectedError

- name: `UNEXPECTED_ERROR`
- code: `500`
- message: `unexpected: <name>`

#### ToBeDoneError

- name: `TBD_ERROR`
Expand Down
Loading
Loading