Error handling for DB

This commit is contained in:
Richard Ramos 2019-11-18 10:05:18 -04:00
parent 74378d2a8d
commit 5b85a69d2d
2 changed files with 80 additions and 62 deletions

View File

@ -27,6 +27,7 @@ class Controller {
// TODO: handle subscriptions to particular events
try {
const subscriber = await Subscribers.findOne({
dappId,
address
@ -68,7 +69,9 @@ class Controller {
token: t.token
});
}
} catch (err) {
return next(err);
}
return res.status(200).send("OK");
};
}
@ -96,10 +99,14 @@ class Controller {
// TODO: handle unsubscribe to particular events
try {
await Subscribers.deleteOne({
dappId,
address
});
} catch (err) {
return next(err);
}
return res.status(200).send("OK");
};
@ -116,6 +123,7 @@ class Controller {
return next(new BadRequest(errors.array()));
}
try {
const verification = await Verifications.findOne({
token
}).populate("subscriber");
@ -136,6 +144,9 @@ class Controller {
} else {
return next(new BadRequest("Invalid verification token"));
}
} catch (err) {
return next(err);
}
return res.status(200).send("OK");
};
@ -152,6 +163,7 @@ class Controller {
return next(new BadRequest(errors.array()));
}
try {
const subscriber = await Subscribers.findOne({
dappId,
address,
@ -159,6 +171,9 @@ class Controller {
});
return res.status(200).json({ isUser: subscriber ? true : false });
} catch (err) {
return next(err);
}
};
}
}

View File

@ -39,6 +39,9 @@ events.on("db:connected", () => {
const response = { error: err.message };
if (err instanceof BadRequest && err.details) {
response.details = err.details;
} else {
console.error(err);
response.error = "Service unavailable";
}
res.status(err.statusCode).json(response);
});