my-error-handler
A simple little stupid library to personal purpose.
Don't mind.
The library helps to handle errors into API projects.
Instalation
npm i my-error-handler
Usage
throw
import { ErrorHandler, MyErrorHandler } from 'my-error-handler';
const fooService = (foo?: object) => {
if (!foo){
const customError: ErrorHandler = {
message: "Not foo to process.",
error: {
code: 3256,
others: {
some: "foo"
}
}
};
const source: string = "./foo/foo.service";
const detail: string = "Process foo in all intances";
throw MyErrorHandler.throw(customError, source, detail);
}
return foo;
}
const fooController = () => {
try {
fooService();
} catch (error) {
throw MyErrorHandler.throw(error, "/foo/foo.controller");
}
}
try {
fooController();
} catch (error) {
console.log("ERROR", error);
}
throwError
import { ErrorHandler, MyErrorHandler } from 'my-error-handler';
const fooService = (foo?: object): Observable<unknown> => {
if (!foo){
const customError: ErrorHandler = {
message: "Not foo to process.",
error: {
code: 3256,
others: {
some: "foo"
}
}
};
const source: string = "./foo/foo.service";
const detail: string = "Process foo in all intances";
return MyErrorHandler.throwError(customError, source, detail); }
return createFoo$.pipe(
...
);
}
const fooController$ = (): Observable<unknown> => {
return from(fooService()).pipe(
catchError((error: any) => MyErrorHandler.throwError(error, "/foo/foo.controller"))
)
}
fooController$().subscribe({
next: (res: any) => console.log(res),
error: (err: any) => console.log("ERROR", err)
})