3.0.2 • Published 12 months ago

exception.js v3.0.2

Weekly downloads
1
License
MIT
Repository
github
Last release
12 months ago

exception-js

Javascript exception with ts for both server-side and clients

Installation

npm install exception-js --save
# OR
yarn add exception-js

Usage

import * as Exceptions from 'exception-js';

app.use('/not_found', async () => {
    throw new Exceptions.NotFound();
});

Create Exception Instance

const error = new Exceptions.NotFound();

// error.code => 404
// error.name => NotFound
// error.subcode => 0
// error.message => Not Found

Create Exception Instance With Custom Message

const error = new Exceptions.NotFound('resource not found');

// error.code => 404
// error.message => resource not found

Create Exception Instance With Custom MetaData

const error = new Exceptions.NotFound('resource not found', {
    requestId: 'xxxxxx',
});

// error.code => 404
// error.message => resource not found
// error.meta => { requestId: 'xxxxxx' }

toJSON

const error = new Exceptions.NotFound('resource not found', {
    requestId: 'xxxxxx',
});

console.log(error.toJSON());
// {
//     code: 404,
//     subcode: 0,
//     message: 'resource not found',
//     meta: {
//         requestId: 'xxxxxx'
//     }
// }

Define Custom Exception

import { define } from './exception';

const BusinessException = define<'BusinessException'>('BusinessException', {
    code: 500,
    subcode: 1,
    message: 'something went wrong', // default message
    meta: { // default meta info
        api: 'xxx'
    }, // default 
})

API Reference

Exception

interface ExceptionBaseProps<T = any> {
    code?: number;
    subcode?: number;
    meta?: T;
}

interface ExceptionDefination<T> extends ExceptionBaseProps<T> {
    message?: string;
}

declare const define: <T, U = any>(name: T, define: ExceptionDefination<U> = {}) => typeof Exception

declare class Exception<T extends string = 'Exception', U = any> extends Error {
    constructor(message?: string, meta?: U)

    readonly name: T;
    readonly code: number;
    readonly subcode: number;
    readonly meta: U;

    toJSON(): {
        code: number;
        subcode: number;
        message: string;
        meta: U;
    }
}
3.0.2

12 months ago

3.0.1

12 months ago

3.0.0

12 months ago

2.0.3

1 year ago

2.0.2

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.1

5 years ago