1.0.6 • Published 2 years ago

my-error-handler v1.0.6

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

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
//     }
//   }
// }
1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago