INGESTKOREA Utility Error Handler for Node.js.
npm install @ingestkorea/util-error-handler

sh
npm install @ingestkorea/util-error-handler
`Getting Started
$3
+ Use TypeScript v4.x$3
`ts
// ES5 example
const { IngestkoreaError } = require('@ingestkorea/util-error-handler');
``ts
// ES6+ example
import { IngestkoreaError } from '@ingestkorea/util-error-handler';
`$3
#### Create custom error instance.
`ts
let customError = new IngestkoreaError({
code: 500, type: 'Internal Server Error',
message: 'Something Broken', description: 'Unhandled Error'
});
`#### Async/await (sample api response)
`ts
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)
})();
``