Return different errors for invalid ID length and no review being found
This commit is contained in:
parent
91480fea64
commit
7b59fc5e4e
|
@ -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 (Object.keys(result).length) {
|
if (req.params.id.length === 6) {
|
||||||
res.writeHead(200, typeJson);
|
if (Object.keys(result).length) {
|
||||||
res.write(JSON.stringify(result));
|
res.writeHead(200, typeJson);
|
||||||
res.end();
|
res.write(JSON.stringify(result));
|
||||||
|
res.end();
|
||||||
|
} else {
|
||||||
|
res.writeHead(404, typeJson);
|
||||||
|
res.write(Responder.notFoundError("review not found"));
|
||||||
|
res.end();
|
||||||
|
err = true;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
res.writeHead(404, typeJson);
|
res.writeHead(400, typeJson);
|
||||||
res.write(Responder.requestError("review not found"));
|
res.write(Responder.requestError("review id must be 6 characters"));
|
||||||
res.end();
|
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();
|
||||||
|
|
|
@ -42,4 +42,13 @@ export class Responder {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static notFoundError(errorMessage: string): string {
|
||||||
|
return JSON.stringify({
|
||||||
|
error: {
|
||||||
|
type: "notFoundError",
|
||||||
|
message: errorMessage
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue