Skip to content
This repository has been archived by the owner on Aug 6, 2021. It is now read-only.

[patch] Document model lifecycle callbacks error reporting #1304

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions concepts/ORM/Lifecyclecallbacks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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); }
Expand Down