π2.0.0.prerelease.2π
A Very Clean Error Handler!
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
// 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");
NPM
npm install oh-my-error
PNPM
pnpm add oh-my-error
Yarn
yarn add oh-my-error
Name | Description |
---|---|
myError | Handle with Error Object |
myErrorWrapper | trycatch wrapper one liner |
myErrorCatcher | promise wrapper |
myErrorHandler | handle scenarios for each error |
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
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!"
}
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 useawait
to solve that
const data = await myErrorWrapper(asyncFun, new Error("Oh, Error!"))("MyString");
new Promise
wrapper.
const data = await myErrorCatcher(readFile)("path...").catch(() => {
// Code before crash...
throw new Error("Can't read file!");
});
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)();
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;
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 |
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! |