Add more robust error handling to server
Changes:
- Use the proper RESTful HTTP status codes for all endpoints (e.g. 204 rather than 200 when the response body is empty)
- Add consistent and more helpful error messages
- Handle invalid route parameters by checking if jamid returns empty objects (e.g. invalid contact IDs)
- Use res.send() rather than res.json() for consistency
- Handle three new signals
GitLab: #111
Change-Id: I1d48dc4629995ab9a96bb2086a9aa91f81889598
diff --git a/server/src/routers/nameserver-router.ts b/server/src/routers/nameserver-router.ts
index 6e49b52..2ead8ee 100644
--- a/server/src/routers/nameserver-router.ts
+++ b/server/src/routers/nameserver-router.ts
@@ -35,13 +35,13 @@
const result = await jamid.lookupUsername(req.params.username, res.locals.accountId);
switch (result.state) {
case 0:
- res.json(result);
+ res.send(result);
break;
case 1:
- res.sendStatus(HttpStatusCode.BadRequest);
+ res.status(HttpStatusCode.BadRequest).send('Invalid username');
break;
default:
- res.sendStatus(HttpStatusCode.NotFound);
+ res.status(HttpStatusCode.NotFound).send('No such username found');
break;
}
})
@@ -53,13 +53,13 @@
const result = await jamid.lookupAddress(req.params.address, res.locals.accountId);
switch (result.state) {
case 0:
- res.json(result);
+ res.send(result);
break;
case 1:
- res.sendStatus(HttpStatusCode.BadRequest);
+ res.status(HttpStatusCode.BadRequest).send('Invalid address');
break;
default:
- res.sendStatus(HttpStatusCode.NotFound);
+ res.status(HttpStatusCode.NotFound).send('No such address found');
break;
}
})