Skip to content

Javascript exception class with ts for both server-side and clients

License

Notifications You must be signed in to change notification settings

yeliex/exception-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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;
    }
}

About

Javascript exception class with ts for both server-side and clients

Resources

License

Stars

Watchers

Forks

Packages

No packages published