If anyone is using RunKit’s to create endpoints and finding the error handling less than satisfactory, with a lot of cases of exceptions resulting in timeouts or an opaque { "error": "unexpected_error", ... }
being returned rather than returning useful error messages with information about the error and stacktrace, I’ve found a workaround where I manually use Domains to catch the errors:
function endpoint (req, res) {
// do your thing
}
const domain = require('domain')
exports.endpoint = function (req, res) {
const d = domain.create()
d.on('error', function (e) {
res.statusCode = 500
res.end(`${e.stack}\n`)
})
d.add(req)
d.add(res)
d.run(endpoint, req, res)
}
For more about RunKit’s faulty use of Domains and why this workaround has to do the things it does, see: https://runkit.com/laughinghan/58337152a62a08001304609a