Description
The problem with exceptions is that once caught you don't know what type they are. You can of course create a bunch of custom error classes and use instanceof to overcome this. The advantage of ts-pojo-error is that you have a single error type PojoError which can be easily typed and serialized.
ts-pojo-error alternatives and similar libraries
Based on the "NodeJS" category.
Alternatively, view ts-pojo-error alternatives based on common mentions on social networks and blogs.
-
Crawlee
CrawleeโA web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation. -
Gluon
DISCONTINUED. A new framework for creating desktop apps from websites, using system installed browsers and NodeJS -
pwa-asset-generator
Automates PWA asset generation and image declaration. Automatically generates icon and splash screen images, favicons and mstile images. Updates manifest.json and index.html files with the generated images according to Web App Manifest specs and Apple Human Interface guidelines. -
teachcode
A tool to develop and improve a studentโs programming skills by introducing the earliest lessons of coding. -
Stylify
๐ Monorepository for Stylify packages. Stylify uses CSS-like selectors to generate Extremely optimized utility-first CSS dynamically based on what you write ๐. -
Autometrics
Easily add metrics to your system โ and actually understand them using automatically customized Prometheus queries -
DIOD
A very opinionated inversion of control (IoC) container and dependency injector for Typescript, Node.js or browser apps. -
github-star-search
A CLI that search your github starred repositories offline through README , description and other fields. -
Brainyduck
๐ฅ A micro "no-backend" framework ๐คฏ Quickly build powerful BaaS using only your graphql schemas -
jirax
:sunglasses: :computer: Simple and flexible CLI Tool for your daily JIRA activity (supported on all OSes) -
zeit
Clock and task scheduler for node.js applications, providing extensive control of time and callback scheduling in prod and test code -
chef-express
Command Line Interface Static Files Server written in TypeScript for Single Page Applications serving in Node with Express -
chef-socket
Command Line Interface Static Files Server written in TypeScript for Single Page Applications serving in Node with Socket.IO -
Be notified of new signups in your app using Firebase Authentication and Google Chat
Be notified of new signups in your app directly in Google Chat
InfluxDB - Purpose built for real-time analytics at any scale.
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of ts-pojo-error or a related project?
README
๐ฅ Type safe pojo error will help you to easily create typed and serializable error.
Intro
The problem with exceptions is that once caught you don't know what type they are. You can of course create a bunch of custom error classes and use instanceof
to overcome this. The advantage of ts-pojo-error
is that you have a single error type PojoError
which can be easily typed and serialized.
Features
- Type safe & autocompletion
- Serializable output
- Node or Browser
- ESM or CJS
- Well tested
Usage
Installation
pnpm add @skarab/ts-pojo-error # yarn and npm also works
Defining an error factory
- factory( errors:
PojoErrorTypes
) :PojoFactory
An error factory is constructed from a Record
where the key is the type of the error and the value is a callback
that defines the PojoError
.
The callback
parameters define the parameters passed at the creation of the error and the return value defines the data of the error.
// errors.ts
import { factory } from "@skarab/ts-pojo-error";
export const errors = factory({
UNKNOWN: () => ({ message: "Unknown error..." }),
WARNING: (message: string) => ({ message, time: Date.now() }),
FATAL: (message?: string) => ({ message: message ?? "Fatal error" }),
EXIT: (message: string, code: number) => ({ message, code }),
});
Instantiating & Throwing errors
- new( type:
infered
, ...args:infered[]
) :PojoError
- throw( type:
infered
, ...args:infered[]
) :never
The first parameter is always the type of error, the following parameters are the ones you set in the factory.
All parameters have support for autocompletion.
// action.ts
import { errors } from "./errors";
export function action() {
// Do your awsome stuff...
// ...something go wrong, throw an typed error
throw errors.new("FATAL");
// or with a custom error message
throw errors.new("FATAL", "Oupsy!");
// or by using the .throw helper
errors.throw("FATAL");
// or by using the fake enum
errors.throw(errors.type.FATAL);
}
Catching & Typing errors
- is( type:
infered
, error:unknown
) : boolean - has( error:
unknown
) : boolean
This is where it gets really interesting, the problem with exceptions is that once caught you don't know what type they are. You can of course create a bunch of custom error classes and use instanceof
to overcome this. The advantage of ts-pojo-error
is that you have a single error type PojoError
which can be easily typed and serialized.
The is
method is a type guard that will narrow the error to the specific type if the original type is compatible.
In the if block all properties are typed and have support for autocompletion.
// index.ts
import { action } from "./action";
import { errors } from "./errors";
try {
action();
} catch (error) {
error; // <- unknown type
if (errors.is("FATAL", error)) {
error; // <- PojoError instance with type "FATAL"
error.message; // "Oupsy!": string
error.type; // "FATAL": "FATAL"
error.args; // ["Oupsy!"] : [message?: string | undefined]
error.data; // { message: "Oupsy!" }: { message: string }
error.toObject(); // { type, args, data, stack?: string | undefined }
error.toJSON(); // string
}
if (errors.has(error)) {
error.type; // "UNKNOWN" | "WARNING" | "FATAL" | ...
}
if (error instanceof PojoError) {
error.type; // any (Bad!)
}
}
Contributing ๐
See CONTRIBUTING.md
*Note that all licence references and agreements mentioned in the ts-pojo-error README section above
are relevant to that project's source code only.