Can all of this be done in a single line?
if (!user) {
const error = new Error('Invalid user.')
error.data = someObject
error.code = 401
throw error
}
Here's an example (with data and code properties populated)
if (!user) throw new Error('Invalid user.')