Security News
Opengrep Emerges as Open Source Alternative Amid Semgrep Licensing Controversy
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
fjl-error-throwing
Advanced tools
Error throwing helpers for quickly throwing errors when a value doesn't match a type.
======= Error throwing helpers. In particular for when a type doesn't match an expected type (or one of one or more types).
npm install elycruz/fjl-errorThrowing
errorIfNotType<contextName, valueName, value, type, messageSuffix=null> {Undefined}
-
Curried. Throws an error when your value
doesn't match given type
.errorIfNotTypes<contextName, valueName, value, ...types> : {Undefined}
-
Curried. Throws an error when your value
doesn't match one of the given types
passed in.getErrorIfNotTypeThrower<messageTmplFunction> : errorIfNotType
- Gives an errorIfNotType
function (curried version) that uses the passed message template function to generate the error message string.getErrorIfNotTypesThrower<messageTmplFunction> : errorIfNotTypes
- Gives an errorIfNotTypes
function (curried version) that uses the passed message template function to generate the error message string.Note: For un-curried versions of the methods above access them with a trailing '$' character;
Example errorIfNotType$(...)
More extensive docs can be found in './jsdocs' folder. Docs will go up somewhere later. @todo
// Template Context used by `messageTmplFunction`:
/**
* @typedef {Object<value, valueName, expectedTypeName, foundTypeName, messageSuffix>} TemplateContext
* @description Template context used for error message renderers (functions that take a context obj and return a string).
* @property value {Any}
* @property valueName {String}
* @property expectedTypeName {String} - Expected name of constructor of `value`; E.g., usually `SomeConstructor.name`;
* @property foundTypeName {String} - Found types name; E.g., `FoundConstructor.name`;
* @property [messageSuffix=null] {Any} - Message suffix (sometimes an extra hint or instructions for
* directing user to fix where his/her error has occurred). Optional.
*/
/**
* Error message template function.
* @typedef {Function} errorMessageCall
* @param tmplContext {TemplateContext}
* @returns {String}
*/
/**
* Used to ensure value matches passed in type.
* @typedef {Function} errorIfNotType
* @param valueName {String}
* @param value {Any}
* @param type {String|Function} - Constructor name or constructor.
* @throws {Error} - If value doesn't match type.
* @returns {Undefined}
*/
/**
* Used to ensure a value matches one of one or more types passed in.
* @typedef {Function} errorIfNotTypes
* @param valueName {String}
* @param value {Any}
* @param valueTypes {...(String|Function)} - Constructor names or constructors.
* @throws {Error} - If value doesn't match type.
* @returns {Undefined}
*/
@todo add more extensive usage examples
import {
getErrorIfNotTypeThrower, // For passing your own error string renderer
getErrorIfNotTypesThrower, // For passing your own error string renderer
errorIfNotType,
errorIfNotTypes
} from 'fjl-errorThrowing';
const errorIfNotFunction = (valueName, value) =>
errorIfNotType('', valueName, value, Function);
function someFunc (fn) {
errorIfNotFunction(fn); // Will throw well detailed error if passed in value is not a Function
// Do something here ...
}
someFunc(_ => 1 + 1); // No error here
someFunc(99); // Error here type doesn't match expected type.
npm test
FAQs
Error throwing helpers for quickly throwing errors when a value doesn't match a type.
The npm package fjl-error-throwing receives a total of 0 weekly downloads. As such, fjl-error-throwing popularity was classified as not popular.
We found that fjl-error-throwing 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
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.