Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More

my-error-handler

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

my-error-handler

A simple little stupid library to personal purpose.


Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created

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';

// Service
const fooService = (foo?: object) => {
  // code
  // condition ... 
  if (!foo){
    // Custom Error
    const customError: ErrorHandler = {
      message: "Not foo to process.",    
      error: {  // Optional                                
        code: 3256,
        others: {
          some: "foo"
        }
      }
    };

    const source: string = "./foo/foo.service";
    const detail: string = "Process foo in all intances";   // Optional

    // My Error Handler
    throw MyErrorHandler.throw(customError, source, detail);
  }  
  
  // more code
  return foo;
}


// Controller
const fooController = () => {
  try {
    fooService();
  } catch (error) {
    // My Error Handler 
    throw MyErrorHandler.throw(error, "/foo/foo.controller");
  }
}


// Caller
try {
  fooController();
} catch (error) {
  console.log("ERROR", error);
}


// Output
//
// ERROR {
//   message: 'Not foo to process.',
//   error: { 
//     code: 3256, 
//     others: { 
//       some: 'foo' 
//     } 
//   },
//   trace: {
//     source: '/user/foo.controller',
//     detail: '',
//     trace: {
//       source: './user/foo.service',
//       detail: 'Process foo in all intances',
//       trace: null
//     }
//   }
// }


throwError


import { ErrorHandler, MyErrorHandler } from 'my-error-handler';

// Service
const fooService = (foo?: object): Observable<unknown> => {
  // code
  // condition ... 
  if (!foo){
    // Custom Error
    const customError: ErrorHandler = {
      message: "Not foo to process.",    
      error: {  // Optional                                
        code: 3256,
        others: {
          some: "foo"
        }
      }
    };

    const source: string = "./foo/foo.service";
    const detail: string = "Process foo in all intances";   // Optional

    // My Error Handler
    return  MyErrorHandler.throwError(customError, source, detail);  }  
  
  // more code
  return createFoo$.pipe(
    ...
  );
}


// Controller
const fooController$ = (): Observable<unknown> => {
  // code
  return from(fooService()).pipe(
    // My Error Handler
    catchError((error: any) => MyErrorHandler.throwError(error, "/foo/foo.controller"))
  )
}



// Caller
fooController$().subscribe({
  next: (res: any) => console.log(res),
  error: (err: any) => console.log("ERROR", err)
})



// Output
//
// ERROR {
//   message: 'Not foo to process.',
//   error: { 
//     code: 3256, 
//     others: { 
//       some: 'foo' 
//     } 
//   },
//   trace: {
//     source: '/user/foo.controller',
//     detail: '',
//     trace: {
//       source: './user/foo.service',
//       detail: 'Process foo in all intances',
//       trace: null
//     }
//   }
// }


Keywords

FAQs

Package last updated on 03 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts