@ingestkorea/util-error-handler
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

@ingestkorea/util-error-handler

npm (scoped) NPM downloads

Description

INGESTKOREA Utility Error Handler for Node.js.

Installing

npm install @ingestkorea/util-error-handler

Getting Started

Pre-requisites

  • Use TypeScript v4.x

Import

// ES5 example
const { IngestkoreaError } = require('@ingestkorea/util-error-handler');
// ES6+ example
import { IngestkoreaError } from '@ingestkorea/util-error-handler';

Usage

Create custom error instance.

let customError = new IngestkoreaError({
  code: 500, type: 'Internal Server Error',
  message: 'Something Broken', description: 'Unhandled Error'
});

Async/await (sample api response)

const sampleAPIResponseFunction = async (input: boolean) => {
  try {
    if (!input) throw new IngestkoreaError({
      code: 401, type: 'Unauthorized',
      message: 'Invalid Credentials', description: 'Access Token Expired'
    });

    const contents = { message: 'Verfiy Token Success' };
    return {
      statusCode: 200,
      headers: { 'content-type': 'application/json; charset=utf-8' },
      body: JSON.stringify(contents, null, 2)
    };
  } catch (err) {
    let customError = new IngestkoreaError({
      code: 500, type: 'Internal Server Error',
      message: 'Something Broken', description: 'Unhandled Error'
    });

    if (err instanceof Error) customError.error.description = `${err.name}: ${err.message}`;
    if (err instanceof IngestkoreaError) customError = err;

    const contents = { ...customError };
    return {
      statusCode: contents.error.code,
      headers: { 'content-type': 'application/json; charset=utf-8' },
      body: JSON.stringify(contents, null, 2)
    };
  };
};

(async () => {
  const response = await sampleAPIResponseFunction(false); // (true | false)
  console.log(response)
})();

License

This Utility is distributed under the MIT License, see LICENSE for more information.

Readme

Keywords

none

Package Sidebar

Install

npm i @ingestkorea/util-error-handler

Weekly Downloads

0

Version

1.0.3

License

MIT

Unpacked Size

7.85 kB

Total Files

6

Last publish

Collaborators

  • ingestkorea