Skip to content

Commit

Permalink
Add create(...) method in HttpError class to build specific Error by …
Browse files Browse the repository at this point in the history
…status code
  • Loading branch information
efabris committed May 22, 2020
1 parent 90f3325 commit 781a171
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/framework/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,40 @@ export class HttpError extends Error implements ValidationError {
]
: err.errors;
}

public static create(err: {
status: number;
path: string;
name: string;
message?: string;
errors?: ValidationErrorItem[];
}):
| InternalServerError
| UnsupportedMediaType
| RequestEntityToLarge
| BadRequest
| MethodNotAllowed
| NotFound
| Unauthorized
| Forbidden {
if (err.status == 400) {
return new BadRequest(err);
} else if (err.status == 401) {
return new Unauthorized(err);
} else if (err.status == 403) {
return new Forbidden(err);
} else if (err.status == 404) {
return new NotFound(err);
} else if (err.status == 405) {
return new MethodNotAllowed(err);
} else if (err.status == 413) {
return new RequestEntityToLarge(err);
} else if (err.status == 415) {
return new UnsupportedMediaType(err);
} else {
return new InternalServerError(err);
}
}
}

export class NotFound extends HttpError {
Expand Down

0 comments on commit 781a171

Please sign in to comment.