Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
typescript-chained-error
Advanced tools
Chained custom errors for Typescript and Javascript. ('Caused by' in error stack)
ChainedErrorFactory
that can extend any already existing error with Caused by
clause additional properties and$ npm install --save typescript-chained-error
import ChainedCustomError from "typescript-chained-error";
class CannotPerformCalculationsError extends ChainedCustomError {
public constructor(msg?: string, cause?: Error) {
super(msg, cause);
}
}
// function that may throw
function buildArray(desiredLength: number) {
try {
return new Array(desiredLength);
} catch (error) {
throw new CannotPerformCalculationsError("Cannot build array", error);
}
}
// call function with illegal parameter
buildArray(-5);
The output:
CannotPerformCalculationsError: Cannot build array
at buildArray (/typescript-chained-error/src/test.ts:14:15)
at Object.<anonymous> (/typescript-chained-error/src/test.ts:18:1)
(...)
Caused by: RangeError: Invalid array length
at buildArray (/typescript-chained-error/src/test.ts:12:16)
at Object.<anonymous> (/typescript-chained-error/src/test.ts:18:1)
(...)
interface Options {
cleanStack: boolean; // default: true
}
// Pass options in constructor
public constructor(msg?: string, cause?: Error) {
super(msg, cause, { cleanStack: false });
}
Example with firebase-functions https error (which is the only error that is thrown at the call site).
import { ChainedErrorFactory } from "typescript-chained-error";
import * as functions from "firebase-functions";
throw ChainedErrorFactory.make(
// primary error that will be extended (this error is preserved in the prototype chain):
functions.https.HttpsError("resource-exhausted", "Message"),
// Causing error:
new TypeError("Cause error"),
// (optional) Additional fields that will be assigned to the returned error object
// e.g.: functions.https.HttpsError allow to add a details field to the error. That field will be reconstructed at the call site.
/* optional: */ { details: { additional: "properties" } },
// (optional) Options. Specified above.
);
Extends the brilliant ts-custom-error. Uses clean-stack by @sindresorhus for pretty output. | Made with ❤️ by Jędrzej Lewandowski.
FAQs
Chained custom errors for Typescript and Javascript. ('Caused by' in error stack)
The npm package typescript-chained-error receives a total of 0 weekly downloads. As such, typescript-chained-error popularity was classified as not popular.
We found that typescript-chained-error 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
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.