1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2025-06-20 16:45:16 +00:00

Handle errors

This commit is contained in:
2019-11-23 13:24:24 +01:00
parent 293752b0f4
commit 1ddf156cc4
3 changed files with 26 additions and 4 deletions

View File

@ -12,6 +12,8 @@ import { checkMail } from "../utils/StringUtils";
export class RequestHandler {
private client : APIClient = null;
private responseSent = false;
public constructor(private req : Request, private response : Response) {}
@ -113,6 +115,10 @@ export class RequestHandler {
* @param message The message to send
*/
public error(code : number, message : string) {
if(this.responseSent)
return;
this.response.status(code).send({
error: {
code: code,
@ -120,6 +126,8 @@ export class RequestHandler {
}
});
this.responseSent = true;
throw Error("Could not complete request!");
}
@ -129,6 +137,9 @@ export class RequestHandler {
* @param message Message associated to success
*/
public success(message: string) {
this.responseSent = true;
this.response.send({
success: message
});
@ -140,6 +151,9 @@ export class RequestHandler {
* @param data The data to send back to the server
*/
public send(data: any) {
this.responseSent = true;
this.response.send(data);
}
}