@toorieaa/singletonclasserror
v2.0.2
Published
A very basic and simple error class. An error designed to be thrown in the constructor of singleton classes.
Downloads
5
Maintainers
Keywords
Readme
1. Information
The module default export is an subclass of the Error class.
2. Installing
npm i @toorieaa/singletonclasserror
3. Importing
import SingletonClassError from "@toorieaa/singletonclasserror";
4. Utilizing
This section describe how to use the import
4.1. Examples
Use cases for the class
4.1.1. Subclassing
How do we subclass the class?
class ServiceSingletonError extends SingletonClassError {
name = "ServiceSingletonError";
constructor(message) {
throw new SingletonClassError(message);
}
}
4.1.2. Creating services
class UserCreationService {
constructor() {
throw new SingletonClassError();
}
static createUser() {
//do some stuff with database etc...
}
}
4.1.3. Simple Configuration classes
import SingletonClassError from "@toorieaa/singletonclasserror";
/**
*
*
* @export
* @class SwaggerInfoOptionsConfig
*/
export default class SwaggerInfoOptionsConfig {
static #INFO = class Info {
static #TITLE = process.env.SWAGGER_TITLE;
static #DESCRIPTION = process.env.SWAGGER_DESCRIPTION;
static #CONTACT = class Contact {
static #NAME = process.env.SWAGGER_CONTACT_NAME;
constructor() {
throw new SingletonClassError();
}
static get NAME() {
return Contact.#NAME;
}
};
constructor() {
throw new SingletonClassError();
}
static get TITLE() {
return Info.#TITLE;
}
static get DESCRIPTION() {
return Info.#DESCRIPTION;
}
static get CONTACT() {
return { name: Info.#CONTACT.NAME };
}
};
static get info() {
return {
info: {
title: SwaggerInfoOptionsConfig.#INFO.TITLE,
description: SwaggerInfoOptionsConfig.#INFO.DESCRIPTION,
contact: SwaggerInfoOptionsConfig.#INFO.CONTACT,
},
};
}
constructor() {
throw new SingletonClassError();
}
}
4.1.4. Create Utility Classes
import SingletonClassError from "@toorieaa/singletonclasserror";
/**
*
*
* @export
* @class UsefulUtils
*/
export default class GeneralUtils {
constructor() {
throw new SingletonClassError();
}
/**
*
*
* @static
* @memberof UsefulUtils
*/
static doNothing = () => undefined;
/**
*
*
* @static
* @param {*} item
* @memberof UsefulUtils
*/
static notNullOrUndefined = (item) =>
!(item === null) && !(item === undefined);
/**
*
*
* @static
* @param {*} obj
* @param {*} fns
* @memberof UsefulUtils
*/
static validateCriteria = (obj, ...fns) => fns.every((fn) => fn(obj));
}
4.2. Options
You can as an option, specify a message argument for the SingletonClassError.
5. Implementation
How is it implemented?
/**
*
*
* @export
* @class SingletonClassError
* @extends {Error}
*/
export default class SingletonClassError extends Error {
static #INSTANCES_NOT_ALLOWED_STRING_MSG = "Instances of this class are not allowed.";
static #NAME = "SingletonClassError";
constructor(message = SingletonClassError.#INSTANCES_NOT_ALLOWED_STRING_MSG) {
super(message);
}
/**
*
*
* @readonly
* @memberof SingletonClassError
*/
get name() {
return SingletonClassError.#NAME;
}
}
6. Recent Updates
As of the most recent update, you can also access the name property "polymorphically".
This means that you can tell that SingletonClassError instances are instances of Error, but also that those instances must have the name SingletonClassError.
The ability to do this allows for a convenient way to skip checking the constructor function to tell if an instance is actually a SingletonClassError.
6.1. License
[MIT]