npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

oh-my-error

v2.0.0-prerelease.2

Published

A simple error handler for nodejs

Downloads

253

Readme

image

About package:

  • 📖 TSDocs (Internal documentation)
  • ♻️ Minified & Compressed
  • ⚠️ Error Handler (oh-my-error)
  • ✅ Support JS/TS & CJS/ESM

What you get:

  • 🛡️ Type safety
  • 🚀 One-line error handling
  • 🎯 Centralized error management
  • 🧑‍💻👥 Error messages for Developers and users!
  • 📝 Pre-defined error templates
  • 🏗️ Consistent error structure across your application
  • 🔌 Easy integration with existing codebases

Showcase

// Make your life better

// Instead
let data;
try {
	data = readFile("...path");
} catch {
	throw Error("Cant Load File!");
}

// Do
const data = myErrorWrapper(readFile, Error("Cant Load File!"))("...path");

📜 List of Contest

Install

NPM

npm install oh-my-error

PNPM

pnpm add oh-my-error

Yarn

yarn add oh-my-error

TLDR (Only Most Important!)

Functions

| Name | Description | | ---------------------------------------------- | ------------------------------- | | myError | Handle with Error Object | | myErrorWrapper | trycatch wrapper one liner | | myErrorCatcher | promise wrapper | | myErrorHandler | handle scenarios for each error |

Types

Error Templates

| Name | Description | | ------------------------------------------------- | --------------------------------------------------------------- | | TMyErrorList !important | For creating list with errors in one place! | | IMyError | Basic Error | | IMyErrorAPI | Basic Error API | | IMyErrorRateLimit | IMyErrorApi with RateLimit error | | IMyErrorValidation | IMyError with Validation error |

Rest Types for creating own Errors

Functions

myError ♻️ Refactored

Processes an error object, invoking functions with provided arguments

const MyErrorList = {
	BLACKLISTED: {
		name: "Black Listed Name",
		hint: "Try use other one!",
		message: (name: string) => `${name} is on black list!`
	}
} as const satisfies TMyErrorList;

throw new myError(MyErrorList.BLACKLISTED, { message: ["nameInputed"] });

Output

{
  name:"Black Listed Name",
  hint:"Try use other one!",
  message:"nameInputed is on black list!"
}

myErrorWrapper 🎉 New feature!

TryCatch one line wrapper with instant error thrower.

// Before
let data;
try {
  data = readFile("path...");
} catch(){
  throw new Error("Can't read file!");
}

// After

const [data,isError] = myErrorWrapper(readFile)("path...");
if(isError) throw new Error("Can't read file!")

// Or instant Error Throw

const data = myErrorWrapper(readFile,new Error("Can't read file!"))("path...");

[!TIP] Async Functions
At async function it returns Promise, just use await to solve that

const data = await myErrorWrapper(asyncFun, new Error("Oh, Error!"))("MyString");

myErrorCatcher

new Promise wrapper.

const data = await myErrorCatcher(readFile)("path...").catch(() => {
	// Code before crash...
	throw new Error("Can't read file!");
});

myErrorHandler

Execute Scenarios for an error!

const [data, isError] = myErrorWrapper(readFile)("./ReadThisFile");

const MyErrorHandlerList = {
	FS001: () => {
		// Do this code if throw this error
		console.error("ERROR");
	}
};
if (isError) myErrorHandler(data.code, MyErrorHandlerList)();

Types

TMyErrorList

[!IMPORTANT]
Use as const satisfies TMyErrorList to work it properly. Don't forget about const because without this you not gonna get tips.

[!TIP]
You can add satisfies ERRORTYPE per error to have strict typing per error or just add as const satisfies TMyErrorList<ERRORTYPE> to have strict typing too!

const ErrorList = {
	notFound: {
		name: "Not Found",
		code: "FS001",
		message: { user: "File not found", dev: "The file you are trying to read does not exist" },
		hint: (path: string) => `Check if the file exists at ${path}`
	} satisfies IMyError,
	cantRead: {
		code: "FS002",
		name: "Cant Read",
		message: { user: "Can't read file", dev: "readFileSync throw error" },
		hint: {
			user: "Check if the file is not corrupted or permissions",
			dev: "File is corrupted or has no permissions to be read"
		}
	}
} as const satisfies TMyErrorList;

Error Templates (Interfaces) new!

There you can find ready error structures.

| Name | Description | Extends | | --------------------------------------------------------------------------------------------------------- | --------------------------------- | ----------------------------------------------------------------------------------------------------- | | IMyError new! | Basic Error | - | | IMyErrorAPI new! | Basic Error for an API error | IMyError, TApiError | | IMyErrorRateLimit new! | API error for RateLimit | IMyError, TApiRateLimit | | IMyErrorValidation new! | API error for Validation problems | IMyError, TValidationError |

Predefined elements for Functions (Atoms) new!

Short predefined types to easy creating own Error types!

| Name (Col1) | Name (Col2) | Name (Col3) | | -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- | | TSeverity new! | TSeverity2 new! | TErrorMessages new! | | TErrorMessagesExt new! | TMyErrorList new! | TErrorList new! | | TCauseError new! | TDetails new! | TBaseError new! | | TBaseErrorExt new! | TValidationError new! | TApiError new! | | TApiRateLimit new! | StatusCodesnew! | |