2.0.1 β€’ Published 8 months ago

oh-my-error v2.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

image

About package:

  • πŸ›‘οΈ Type safety
  • πŸ“– TSDocs (Internal documentation)
  • ♻️ Minified & Compressed
  • ⚠️ Error Handler (oh-my-error)
  • βœ… Support JS/TS & CJS/ESM

What you get:

  • πŸš€ 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

NameDescription
myErrorHandle with Error Object
myErrorWrappertrycatch wrapper one liner
myErrorCatcherpromise wrapper
myErrorHandlerhandle scenarios for each error

Types

Error Templates

NameDescription
TMyErrorList !importantFor creating list with errors in one place!
IMyErrorBasic Error
IMyErrorAPIBasic Error API
IMyErrorRateLimitIMyErrorApi with RateLimit error
IMyErrorValidationIMyError 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 (with errorToThrow)
const data = myErrorWrapper(readFile,new Error("Can't read file!"))("path...");
// With Passing Throwed Error to our error
const data = myErrorWrapper(readFile,err => new Error(`ERROR MESSAGE: ${err.message}}`))("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 (Organisms) new!

There you can find ready error structures.

NameDescriptionExtends
IMyError new!Basic Error Template for Error
IMyErrorAPI new!Basic Error Template for APIIMyError, TApiError
IMyErrorRateLimit new!Basic Error Template for API Rate limitIMyError, TApiRateLimit
IMyErrorValidation new!Basic Error Template for ValidationIMyError, TValidationError
TAllMyErrorTypes new!Every Error Template exclude TBaseError & TBaseErrorExtIMyError | IMyErrorAPI | IMyErrorRateLimit | IMyErrorValidation
TAllMyErrorTypesExt new!Every Error Template with return type Function or Value option exclude TBaseError & TBaseErrorExtTValueOrFunction<TAllMyErrorTypes>
TBaseError new!Predefined type for Error-
TBaseErrorExt new!Predefined type for Error with function types-

Predefined elements (Moleculars) new!

Short predefined types to easy creating own Error types!

Name (Col1)Name (Col2)Name (Col3)
TValidationError new!TApiError new!TApiRateLimit new!
TErrorMessagesExt new!TErrorList new!TCauseError new!
TDetails new!--

Predefined types for properties (Atoms) new!

Short predefined types for properties!

Name (Col1)Name (Col2)Name (Col3)
TSeverity new!TSeverity2 new!StatusCodes

Utils Types new!

Name (Col1)Name (Col2)Name (Col3)
TValueOrFunction new!TValueOrFunctionAll new!-