New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

samba-bo-errors

Package Overview
Dependencies
Maintainers
0
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

samba-bo-errors

Create consistent error objects around the codebase

latest
npmnpm
Version
1.0.0
Version published
Maintainers
0
Created
Source

samba-bo-errors

Utility functions to help creating and checking against SambaBO errors.

Installation

pnpm add samba-bo-errors

Usage

Creating a new Error class

createError(code: string, message: string | (T) => string, status = 500): SambaBOErrorConstructor<T>
import { createError } from 'samba-bo-errors';

const ForbiddenError = createError('FORBIDDEN', "You don't have permissions to see this.", 403);

throw new ForbiddenError();

You can configure additional extensions for the error class which should be communicated to the end user:

import { createError } from 'samba-bo-errors';

interface ForbiddenErrorExtensions {
	collection: string;
	field: string;
}

const ForbiddenError = createError<ForbiddenErrorExtensions>(
	'FORBIDDEN',
	"You don't have permissions to see this.",
	403,
);

throw new ForbiddenError({
	collection: 'articles',
	field: 'title',
});

You can then also use those extensions to generate out the error message:

import { createError } from 'samba-bo-errors';

interface ForbiddenErrorExtensions {
	collection: string;
	field: string;
}

const messageConstructor = (extensions: ForbiddenErrorExtensions) =>
	`You don't have permissions to see "${extensions.field}" in "${extensions.collection}".`;

const ForbiddenError = createError<ForbiddenErrorExtensions>('FORBIDDEN', messageConstructor, 403);

throw new ForbiddenError({
	collection: 'articles',
	field: 'title',
});

Checking if a given param is a valid SambaBOError instance

import { isSambaBOError, createError } from 'samba-bo-errors';

const ForbiddenError = createError('FORBIDDEN', "You don't have permissions to see this.", 403);

isSambaBOError(new ForbiddenError()); // true
isSambaBOError(new Error()); // false

FAQs

Package last updated on 14 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts