Description
At the moment, server.js
has several try/catch blocks that do "error filtering":
try {
const data = await getData()
ctx.body = data
} catch (err) {
if (err.message.startsWith('invalid tree ref')) {
ctx.throw(404, 'unknown ref')
} else {
throw err
}
}
There's some redundancy there and it decreases readability. There has to be a better way. I think it is to have lib/GitSheets.js
throw custom errors, e.g. NotFoundError
, and have server.js
map these to status codes in a single place (the error handler function).
That still leaves the errors that originate beneath lib/GitSheets.js
(from hologit, gitsheets, or git). I expect we'll still have to do some string matching on that, but it can be at the lib level rather than the server level. So we'll essentially translate invalid tree ref
to NotFoundError
, and the server and CLI just need to decide what to do with a NotFoundError
.
We can use create-error for custom errors or probably just do it manually.