The error() function is now intended as the primary error factory with improved defaults for common error codes to make usage more simpler/concise and errors more consistent.
The error.db function is intended as replacement for all DB query error handling, with some important additional benefits:
Single .catch(...):
Makes error handling easier to reason about
Removes the need for always passing-thro already wrapped errors in subsequent catches (old error(), error.http(), error.db() would pass-thro GenericErrors, making use of those probably unexpected if used in other places...)
Does not relay on non-standard Bluebird .catch(filter, handler) (see #10)
return db.one(`SELECT * FROM "user" WHERE "email" = $1`, [email])
.catch(error.db({
noData: 'user.not_found',
}))
await db.query(`
UPDATE "user"
SET "email" = $2
WHERE "id" = $1
`, [id, email])
.catch(error.db({
user_email_key: 'user.duplicate',
}))
await db.query('DELETE FROM password_token WHERE user_id = $1', [userId])
.catch(error.db) // ensure DB errors are wrapped in a DatabaseError
Please, let me know if you have any concern or objections.
Error Creation
The
error()
function is now intended as the primary error factory with improved defaults for common error codes to make usage more simpler/concise and errors more consistent.DB Error Handling
The
error.db
function is intended as replacement for all DB query error handling, with some important additional benefits:.catch(...)
:error()
,error.http()
,error.db()
would pass-throGenericError
s, making use of those probably unexpected if used in other places...).catch(filter, handler)
(see #10)Please, let me know if you have any concern or objections.