Package Exports
- @vladbasin/strong-api-models
- @vladbasin/strong-api-models/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@vladbasin/strong-api-models) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Strong API models
Common models for Strong API framework
Installation
npm
npm install @vladbasin/strong-api-models
yarn
yarn add @vladbasin/strong-api-models
Usage
CodedError
Create CodedError to match errors with particular strings for reason parsing by consumers (e.g., InvalidInput, NotAuthorized, ValidationFailed, etc.)
import { CodedError } from '@vladbasin/strong-api-constants';
export class CodedError extends Error {
/**
* Creates new CodedError
* @param code - code corresponding to this error (good for API response for further parsing by clients)
* @param message
* @param errors - inner errors to capture
* @param stack
* @param details - any additional details
*/
constructor(
public code: string,
public message: string,
public errors: InnerErrorType[] = [],
public stack = '',
public details: Record<string, unknown> = {}
)
/**
* Creates CodedError based on another error
* @param error - error occured
* @param code - code corresponding to this error (good for API response for further parsing by clients)
* @param errors - inner errors to capture
* @param details - any aditional details
* @returns CodedError
*/
static from(
error: Error,
code: string = CommonErrorCodes.unknown,
errors: InnerErrorType[] = [],
details: Record<string, unknown> = {}
): CodedError
}CommonErrorCodes
export enum CommonErrorCodes {
unknown = 'Unknown',
unauthorized = 'Unauthorized',
notFound = 'NotFound',
validationFailed = 'ValidationFailed',
}Get HTTP status code for error code
// get HTTP status code
codedErrorToHttpStatusMap.getHttpCode('InvalidParameter'); //returns 400
// add custom error codes
codedErrorToHttpStatusMap.extend({ 'MyNewErrorCode': 502 });
codedErrorToHttpStatusMap.getHttpCode('MyNewErrorCode'); //returns 502