Skip to content

pallad-ts/common-errors

Repository files navigation

common-errors


CircleCI npm version Coverage Status License: MIT

Small set of commonly used errors. Carefully designed and battle tested so you don't need to do it.

Installation

npm install @pallad/common-errors

Common api

All errors assume that code property might be set for an error. code is used to uniquely specify what error is being thrown.

Errors

ApplicationError

The most common kind of error. Should be used only if other type of errors do not fit.

import {ApplicationError} from "@pallad/common-errors";

function publishArticle(id: string) {
    const article = findById(id);
    if (article?.status === 'published') {
        throw new ApplicationError('Cannot publish article that is already published');
    }
}

AuthenticationError

Indicates lack of ability to verify who is the participant performing an operation

import {AuthenticationError} from '@pallad/common-errors';

function activateAccount(token: string) {
    if (!verifyToken(token)) {
        throw new AuthenticationError('Invalid token');
    }
}

AuthorizationError

Indicates lack of certain permissions to perform an operation

import {AuthorizationError} from '@pallad/common-errors';

function deleteArticle(participant: Participant, id: string) {
    const article = findById(id);
    if (!hasPermission(article, participant)) {
        throw new AuthorizationError('Invalid token');
    }
}

// TODO