Small set of commonly used errors. Carefully designed and battle tested so you don't need to do it.
npm install @pallad/common-errors
All errors assume that code
property might be set for an error.
code
is used to uniquely specify what error is being thrown.
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');
}
}
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');
}
}
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