Setup exceptions structure

This commit is contained in:
rijkvanzanten
2020-06-29 13:09:40 -04:00
parent 21467dcf43
commit 9fd2fe891f
5 changed files with 47 additions and 0 deletions

12
src/exceptions/base.ts Normal file
View File

@@ -0,0 +1,12 @@
import { Code } from './types';
export class BaseException extends Error {
status: number;
code: Code;
constructor(message: string, status: number, code: Code) {
super(message);
this.status = status;
this.code = code;
}
}

3
src/exceptions/index.ts Normal file
View File

@@ -0,0 +1,3 @@
export * from './not-found';
export * from './base';
export * from './invalid-query';

View File

@@ -0,0 +1,7 @@
import { BaseException } from './base';
export class InvalidQueryException extends BaseException {
constructor(message: string) {
super(message, 400, 'INVALID_QUERY');
}
}

View File

@@ -0,0 +1,19 @@
import { BaseException } from './base';
export class RouteNotFound extends BaseException {
constructor(path: string) {
super(`Route ${path} doesn't exist.`, 404, 'ROUTE_NOT_FOUND');
}
}
export class CollectionNotFoundException extends BaseException {
constructor(collection: string) {
super(`Collection ${collection} can't be found.`, 404, 'COLLECTION_NOT_FOUND');
}
}
export class FieldNotFoundException extends BaseException {
constructor(field: string) {
super(`Field ${field} can't be found.`, 404, 'FIELD_NOT_FOUND');
}
}

6
src/exceptions/types.ts Normal file
View File

@@ -0,0 +1,6 @@
export type Code =
| 'COLLECTION_NOT_FOUND'
| 'FIELD_NOT_FOUND'
| 'ROUTE_NOT_FOUND'
| 'NOT_FOUND'
| 'INVALID_QUERY';