aboutsummaryrefslogtreecommitdiff
path: root/src/errors
diff options
context:
space:
mode:
authorTom Harley2018-03-03 16:42:16 +0000
committerTom Harley2018-03-03 16:42:16 +0000
commit652d328dc1d6605687cbd6858a3d27da1c126f7e (patch)
tree93bab0513665714c3f20948e5f39428dea7c9ca0 /src/errors
parentFix minor things (diff)
downloadmedicine-652d328dc1d6605687cbd6858a3d27da1c126f7e.tar.gz
medicine-652d328dc1d6605687cbd6858a3d27da1c126f7e.zip

Implement handling of unauthorised requests

Diffstat (limited to 'src/errors')
-rw-r--r--src/errors/errorware.ts24
1 files changed, 16 insertions, 8 deletions
diff --git a/src/errors/errorware.ts b/src/errors/errorware.ts
index 45a76b3..e2355de 100644
--- a/src/errors/errorware.ts
+++ b/src/errors/errorware.ts
@@ -10,6 +10,13 @@ export interface ErrorResponseBlock {
}
export class RequestError extends Error {
+ public static is(test: Object): test is RequestError {
+ const err: RequestError = test as RequestError;
+ return err.code !== undefined
+ && err.name !== undefined
+ && err.responseBlock !== undefined;
+ }
+
public code: number;
public userMessage?: string;
// tslint:disable-next-line:no-any
@@ -41,16 +48,17 @@ export class RequestError extends Error {
}
}
-type Errorware =
+export type Errorware =
(err: Error, req: Request, res: Response, next: NextFunction) => void;
export const errorHandler: Errorware =
(err: Error, req: Request, res: Response, next: NextFunction): void => {
- if ((err as RequestError).responseBlock) {
- const rerr: RequestError = err as RequestError;
- res.status(rerr.code)
- .json(rerr.responseBlock());
- } else {
- res.send(err);
- }
+ console.log(err);
+ if (RequestError.is(err)) {
+ console.log('fewkuihfklsfv');
+ res.status(err.code)
+ .json(err.responseBlock());
+ } else {
+ res.send(err);
+ }
};