Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
catch-unknown
Advanced tools
While exceptions thrown in JavaScript are usually objects of class Error
, they can actually be values of any type.
This is particularly relevant in TypeScript, which in version 4.4 starting defaulting catch
variables to unknown
type (instead of any
).
This small library provides two functions to make writing type-safe catch blocks easier: isError
returns whether a
value conforms to the Error
interface, and asError
will convert any value to an object conforming to Error
if necessary:
interface Error {
name: string;
message: string;
stack?: string;
cause?: unknown;
}
export declare function isError(err: unknown): err is Error;
export declare function asError(err: unknown): Error;
This library has no runtime dependencies, compiles to ES6 for wide compatibility, and has a package size of 2.9 kB.
npm install catch-unknown
Typical usage might look something like this:
import { asError } from 'catch-unknown';
try {
// stuff
} catch (err) {
logger.warn(`Stuff failed due to ${asError(err).message}`);
throw err;
}
Hopefully you never see a non-Error
thrown, but if you do, nothing else will break:
import { asError, isError } from 'catch-unknown';
try {
throw new Error('Something is wrong');
} catch (err) {
console.log(isError(err)); // true
console.log(asError(err)); // Error: Something is wrong
}
try {
throw { message: 'An odd thing to throw' };
} catch (err) {
console.log(isError(err)); // false
console.log(asError(err)); // Object: An odd thing to throw
}
try {
throw { x: 12, y: 5 };
} catch (err) {
console.log(isError(err)); // false
console.log(asError(err)); // Object: {"x":12,"y":5}
}
try {
throw new Date(0);
} catch (err) {
console.log(isError(err)); // false
console.log(asError(err)); // Date: Thu Jan 01 1970 00:00:00 GMT+0000 (Coordinated Universal Time)
}
try {
throw 42;
} catch (err) {
console.log(isError(err)); // false
console.log(asError(err)); // number: 42
}
catch-unknown
is available under the ISC license.
FAQs
Utility functions for writing type-safe catch blocks
The npm package catch-unknown receives a total of 6,658 weekly downloads. As such, catch-unknown popularity was classified as popular.
We found that catch-unknown 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.