Return different errors for invalid ID length and no review being found

This commit is contained in:
powermaker450 2024-09-15 10:20:06 -04:00
parent 91480fea64
commit 7b59fc5e4e
2 changed files with 29 additions and 7 deletions

View file

@ -22,20 +22,33 @@ export class MessagesResponder extends ApiRoute {
}); });
this.server.get(`${this.routeName}/:id`, (req: IdRequest, res: Response) => { this.server.get(`${this.routeName}/:id`, (req: IdRequest, res: Response) => {
// "req.params.id" is the review ID that was receieved
// "result" is the review that is identified by that ID, if any
const receiver = req.headers["user-agent"]; const receiver = req.headers["user-agent"];
const result = data.getReviewById(req.params.id); const result = data.getReviewById(req.params.id);
let err = false;
if (req.params.id.length === 6) {
if (Object.keys(result).length) { if (Object.keys(result).length) {
res.writeHead(200, typeJson); res.writeHead(200, typeJson);
res.write(JSON.stringify(result)); res.write(JSON.stringify(result));
res.end(); res.end();
} else { } else {
res.writeHead(404, typeJson); res.writeHead(404, typeJson);
res.write(Responder.requestError("review not found")); res.write(Responder.notFoundError("review not found"));
res.end(); res.end();
err = true;
}
} else {
res.writeHead(400, typeJson);
res.write(Responder.requestError("review id must be 6 characters"));
res.end();
err = true;
} }
this.logger.log(`${Logger.emp(receiver)} <~ "${req.path}"`); // If an error was returned to the client, mark their user agent red in the logs
this.logger.log(`${err ? Logger.err(receiver) : Logger.emp(receiver)} <~ "${req.path}"`);
}); });
this.complete(); this.complete();

View file

@ -42,4 +42,13 @@ export class Responder {
}, },
}); });
} }
public static notFoundError(errorMessage: string): string {
return JSON.stringify({
error: {
type: "notFoundError",
message: errorMessage
}
});
}
} }