Skip to content

Commit

Permalink
Merge pull request #45 from haythamlabrini/f/add-detail-generic-to-ap…
Browse files Browse the repository at this point in the history
…ierror

✨ add detail generic to apierror
  • Loading branch information
knor-el-snor committed May 7, 2020
2 parents d38c61e + d051631 commit ba775e3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ yarn add @icapps/tree-house-errors

## Error types

### ApiError
### ApiError<T>

Base error class which extends from the `Error` class.
`ApiError` accepts a generic `T` for the `details` property; if not specified, it defaults to `any`.

```javascript
// All keys are required
Expand Down
42 changes: 21 additions & 21 deletions src/lib/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import * as httpStatus from 'http-status';

import { errors } from '../config/errors.config';

export class ApiError extends Error {
export class ApiError<T = any> extends Error {
code: string;
status: number;
i18n?: string;
id?: string;
detail?: any;
detail?: T;
isApiError: boolean;

constructor(status: number, error: ErrorType, args: { message?: string, detail?: any, stack?: any } = {}) {
constructor(status: number, error: ErrorType, args: { message?: string, detail?: T, stack?: any } = {}) {
const { message, detail, stack } = args;
super(message || error.message);
this.name = 'ApiError';
Expand All @@ -25,57 +25,57 @@ export class ApiError extends Error {
}
}

export class GenericError extends ApiError {
constructor(error?: ErrorType, args?: ErrorArgs) {
export class GenericError<T = any> extends ApiError<T> {
constructor(error?: ErrorType, args?: ErrorArgs<T>) {
super(0, error == null ? errors.GENERIC_ERROR : error, args);
this.name = 'GenericError';
}
}

export class BadRequestError extends ApiError {
constructor(error?: ErrorType, args?: ErrorArgs) {
export class BadRequestError<T = any> extends ApiError<T> {
constructor(error?: ErrorType, args?: ErrorArgs<T>) {
super(httpStatus.BAD_REQUEST, error == null ? errors.BAD_REQUEST : error, args);
this.name = 'BadRequestError';
}
}

export class NotFoundError extends ApiError {
constructor(error?: ErrorType, args?: ErrorArgs) {
export class NotFoundError<T = any> extends ApiError<T> {
constructor(error?: ErrorType, args?: ErrorArgs<T>) {
super(httpStatus.NOT_FOUND, error == null ? errors.RESOURCE_NOT_FOUND : error, args);
this.name = 'NotFoundError';
}
}

export class ForbiddenError extends ApiError {
constructor(error?: ErrorType, args?: ErrorArgs) {
export class ForbiddenError<T = any> extends ApiError<T> {
constructor(error?: ErrorType, args?: ErrorArgs<T>) {
super(httpStatus.FORBIDDEN, error == null ? errors.FORBIDDEN : error, args);
this.name = 'ForbiddenError';
}
}

export class InternalServerError extends ApiError {
constructor(error?: ErrorType, args?: ErrorArgs) {
export class InternalServerError<T = any> extends ApiError<T> {
constructor(error?: ErrorType, args?: ErrorArgs<T>) {
super(httpStatus.INTERNAL_SERVER_ERROR, error == null ? errors.INTERNAL_ERROR : error, args);
this.name = 'InternalServerError';
}
}

export class UnauthorizedError extends ApiError {
constructor(error?: ErrorType, args?: ErrorArgs) {
export class UnauthorizedError<T = any> extends ApiError<T> {
constructor(error?: ErrorType, args?: ErrorArgs<T>) {
super(httpStatus.UNAUTHORIZED, error == null ? errors.UNAUTHORIZED : error, args);
this.name = 'UnauthorizedError';
}
}

export class ValidationError extends ApiError {
constructor(error?: ErrorType, args?: ErrorArgs) {
export class ValidationError<T = any> extends ApiError<T> {
constructor(error?: ErrorType, args?: ErrorArgs<T>) {
super(httpStatus.BAD_REQUEST, error == null ? errors.INVALID_INPUT : error, args);
this.name = 'ValidationError';
}
}

export class AuthenticationError extends ApiError {
constructor(error?: ErrorType, args?: ErrorArgs) {
export class AuthenticationError<T = any> extends ApiError<T> {
constructor(error?: ErrorType, args?: ErrorArgs<T>) {
super(httpStatus.BAD_REQUEST, error == null ? errors.AUTHENTICATION_FAILED : error, args);
this.name = 'AuthenticationError';
}
Expand All @@ -88,9 +88,9 @@ export interface ErrorType {
i18n?: string;
}

export interface ErrorArgs {
export interface ErrorArgs<T> {
message?: string;
status?: number;
detail?: any;
detail?: T;
stack?: any;
}

0 comments on commit ba775e3

Please sign in to comment.