-
Notifications
You must be signed in to change notification settings - Fork 0
Cleanup Route Handler Messages #39
Description
For each of the following Route Handlers:
-
codexapp_api.js -
routes/create.js -
routes/cssRouter.js -
routes/export.js -
routes/index.js -
routes/jsRouter.js -
routes/resource.js -
routes/view.js -
routes/api/create.js -
routes/api/db.js -
routes/api/detect.js -
routes/api/export.js -
routes/api/getToken.js -
routes/api/view.js
Need to be checked to make sure:
Routes make sense.
No extra or unused routes.
Handlers return useful errors.
Every handler function (in the form app.get(...), app.post(...), app.all(...)) should look like this:
app.all('/', async (req, res) => {
try {
// OTHER CODE HERE
} catch (err) {
res.status(400)
.send(`There was an error with your request: ${err}`)
.end();
} // ^^ Catches any sort of error and responds appropriately.
});Obviously, don't copy this exactly, and adapt the message to make sense for the error being handled. Also, if possible, all route handlers should use the same error message.
Handlers have default routes
Every route handler file should have a default route, which is the route used when the request does not match any of the expected parameters. Doing this is pretty straightforward. Simply add something like this after the last handler function:
app.all('/', async (req, res) => {
res.status(400)
.send("Error, ROUTENAME only supports SUPPORTED ROUTES")
.end();
}); // ^^ Informs user on how to make requests correctlyAgain, don't copy this exactly, but make sure it fits with the route you are working on. This shouldn't be very difficult. If the route you're working on already has a default handler, check it and make sure the supported routes are still accurate, as many of these messages haven't been updated with the rest of the API changes.