commonjs-errors
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

commonjs-errors

Common errors used in the system

Usage

Creating custom errors

Create a class which should extend either APIError or Extendable error

export class TimeoutError extends ApiError {
  constructor(error?: Error) {
    super(
      "Request timeout error",
      {
        status: httpStatus.REQUEST_TIMEOUT,
        code: 'error.request-timeout',
/** if isPublic is set to true refers the error is known error and server should print the error message
if it's set to false, its unknown / unexpected error
*/
        isPublic: true,
      },
      error,
    );
  }
}

/** Use the created error class to throw error */
throw new TimeoutError(err);

Using existing errors from the package

import { UnauthorizedError, ResourceNotFoundError } from 'commonjs-errors';

/** UnauthorizedError */
if (err instanceof UnauthorizedError) {
  throw new UnauthorizedError(err.message, err));
}

/** ResourceNotFoundError */
if(!resource){
 throw new ResourceNotFoundError(workspaceId.toString());
}

Package Sidebar

Install

npm i commonjs-errors

Weekly Downloads

21

Version

1.0.1

License

ISC

Unpacked Size

75.7 kB

Total Files

12

Last publish

Collaborators

  • anokhi0895