From 9addb470fcb2a0eed5179cc0d4c08659e21c37e2 Mon Sep 17 00:00:00 2001 From: Daniel Harvey Date: Sun, 22 Mar 2020 09:19:41 +0800 Subject: [PATCH] Document model lifecycle callbacks error reporting Document the reporting of server error vs usage errors for model lifecycle callbacks using `flaverr` --- concepts/ORM/Lifecyclecallbacks.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/concepts/ORM/Lifecyclecallbacks.md b/concepts/ORM/Lifecyclecallbacks.md index 9dc018bd5..22f800f33 100644 --- a/concepts/ORM/Lifecyclecallbacks.md +++ b/concepts/ORM/Lifecyclecallbacks.md @@ -30,6 +30,10 @@ The `afterDestroy` lifecycle callback will only be run on `.destroy()` queries t - afterDestroy: fn(destroyedRecord, proceed) +### Error Handling + +Errors reporting via `proceed(err)` result in `res.serverError()` (HTTP 500) or `res.badRequest()` (HTTP 400) for blueprints based upon the use of `flaverr`. See example below. + ### Example If you want to hash a password before saving in the database, you might use the `beforeCreate` lifecycle callback. @@ -55,6 +59,11 @@ module.exports = { beforeCreate: function (valuesToSet, proceed) { + // Check password complexity + if(!meetsComplexityRequirements(valuesToSet.password)) { + let msg = 'Password does not meet complexity requirements'; + return proceed(flaverr({ name: 'UsageError', code:'E_PASSWORD_ERROR', details:msg }, new Error(msg))); + } // Hash password sails.helpers.passwords.hashPassword(valuesToSet.password).exec((err, hashedPassword)=>{ if (err) { return proceed(err); }