Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
[![npm version](https://badge.fury.io/js/ebec.svg)](https://badge.fury.io/js/ebec) [![main](https://github.com/Tada5hi/ebec/actions/workflows/main.yml/badge.svg)](https://github.com/Tada5hi/ebec/actions/workflows/main.yml) [![codecov](https://codecov.io/g
This is a library, which provides a base error class, which can simply be extended ⚡.
It also provides some utility functions to build
& merge
options.
Table of Contents
npm install ebec --save
The BaseError
class can be initialized on different ways, like demonstrated by the following examples:
Example #1
In this example no options are specified on class instantiation, but afterwards.
import { BaseError } from 'ebec';
const error = new BaseError('An error occurred.');
console.log(error.message);
// An error is occurred.
console.log(error.getOptions());
// {}
error.setOption('statusCode', 404);
console.log(error.getOptions());
// {statusCode: 404}
console.log(error.getOption('statusCode'));
// 404
Example #2
In the following example the error options are specified on instantiation.
import { BaseError, Options } from 'ebec';
const options : Options = {
statusCode: 404,
//... define some own options
foo: 'bar'
}
const error = new BaseError('The entity could not be found', options);
const statusCode = error.getOption('statusCode');
console.log(statusCode);
// 404
const foo = error.getOption('foo');
console.log(foo);
// bar
Like demonstrated in the example above, self defined options can be provided in addition to the existing options keys ⚡.
Example #3
In the following example only the error options are passed as single argument to the error constructor.
import { BaseError, Options } from 'ebec';
const options : Options = {
message: 'The entity could not be found',
statusCode: 404,
//... define some own options
foo: 'bar'
}
const error = new BaseError(options);
console.log(error.message);
// The entity could not be found
// access the option values
const statusCode = error.getOption('statusCode');
console.log(statusCode);
// 404
Besides, using only the BaseError class, own classes which inherit the BaseError class, can simply be created and provide a better way to handle errors more differentiated.
import {
BaseError,
Options,
mergeOptions
} from 'ebec';
export class NotFoundError extends BaseError {
constructor(message?: Options) {
super(mergeOptions(
{
logMessage: true,
logLevel: 'warning',
statusCode: 404,
},
...(options ? options : {})
));
}
}
The library is like already mentioned also shipped with some utility functions, to make life easier.
The buildOptions
method requires two arguments. The first one can either be a string
, Error
or a
value of type Options
. The second argument one, on the other hand must be of type Options
.
import { buildOptions } from 'ebec';
let options = buildOptions({
statusCode: 404
}, {
error: 'ERROR'
});
console.log(options);
// {statusCode: 404, code: 'ERROR'}
options = buildOptions('An error occurred.', {code: 'ERROR'});
console.log(options);
// {code: 'ERROR'}
The mergeOptions
accepts 1-n arguments of type Options
and merge them to one option set,
which is then provided as return value.
import { mergeOptions } from 'ebec';
let options = mergeOptions({
statusCode: 404
}, {
error: 'ERROR'
});
console.log(options);
// {statusCode: 404, code: 'ERROR'}
options = mergeOptions('An error occurred.', {code: 'ERROR'});
console.log(options);
// {code: 'ERROR'}
export type Options = {
/**
* The error code is either a short uppercase string identifier
* for the error or a numeric error code. For example: SERVER_ERROR
*/
code?: string | number,
/**
* The actual error message, if not provided on another way.
*/
message?: string,
/**
* Mark this error as error which need to be logged.
*/
logMessage?: boolean,
/**
* Set the log level for this error.
*/
logLevel?: string | number,
/**
* Specify if the error message should be decorated for public view
* or already provide a decoration message.
*/
decorateMessage?: boolean | string,
/**
* Specify a previous error.
*/
previous?: Error,
/**
* In case of a http error provide a numeric Status Code between 400-599.
*/
statusCode?: number,
/**
* Specify a redirect URL in case of a http error.
*/
redirectURL?: string,
/**
* Additional options.
*/
[key: string]: any
}
Made with 💚
Published under MIT License.
FAQs
A library that provides a basic ES6 error class.
The npm package ebec receives a total of 109,078 weekly downloads. As such, ebec popularity was classified as popular.
We found that ebec demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.