Improve responder messages

This commit is contained in:
powermaker450 2024-08-18 23:17:31 -04:00
parent bb67c4582d
commit 6041aea8ed
2 changed files with 25 additions and 3 deletions

View file

@ -26,7 +26,11 @@ http
try { try {
temp = JSON.parse(chunk); temp = JSON.parse(chunk);
} catch (err) { } catch (err) {
console.error(origin + "Chunk is not valid JSON"); logger.error("Recieved chunk was not valid JSON!");
res.writeHead(400, contentType);
res.write(response.JsonError("recieved chunk was not valid JSON"));
res.end();
return; return;
} }
@ -43,7 +47,7 @@ http
); );
res.writeHead(201, contentType); res.writeHead(201, contentType);
res.write(response.success("Review was sent")); res.write(response.success("review was sent"));
res.end(); res.end();
}); });
}) })
@ -58,7 +62,7 @@ http
logger.log(`${sender} cannot ${req.method} to ${req.url}`); logger.log(`${sender} cannot ${req.method} to ${req.url}`);
res.writeHead(400, contentType); res.writeHead(400, contentType);
res.write(JSON.stringify({ error: { type: "generic", error: `Cannot ${req.method} to ${req.url}` }})); res.write(response.requestError(`Cannot ${req.method} to ${req.url}`));
res.end(); res.end();
} }
}); });

View file

@ -17,4 +17,22 @@ export class Responder {
} }
}); });
} }
public requestError(errorMessage: string): string {
return JSON.stringify({
error: {
type: "RequestError",
message: errorMessage
}
});
}
public JsonError(errorMessage: string): string {
return JSON.stringify({
error: {
type: "JsonError",
message: errorMessage
}
});
}
} }