Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strongloop: how do you return an error if Operation Hook fails?

How do you return an error inside an operation hook?

Use case is sending a push notification after saving a new model instance.

I observe the 'after save' event, send the push. If this fails for whatever reason, I want to send a 500 response code. How do I do that?

I am unable to find documentation as to what the ctx object actually is or contains.

Customer.observe('after save', function(ctx, next) {

  //model saved, but sending push failed for whatever reason, and I want to now send a 500 error back to the user
  //how?  what's inside ctx? how do you send back a response?  
  next();
});
like image 699
user798719 Avatar asked May 29 '15 05:05

user798719


2 Answers

I believe it's something along these lines:

var error = new Error();
error.status = 500;
next(error);
like image 70
Are Almaas Avatar answered Oct 25 '22 08:10

Are Almaas


Extending the previous answer, as I cannot add comments yet.

You can provide more information to the error response with:

var error = new Error();
error.status = 401;
error.message = 'Authorization Required';
error.code = 'AUTHORIZATION_REQUIRED';

This will return something like:

{
   "error": {
      "name": "Error",
      "status": 401,
      "message": "Authorization Required",
      "code": "AUTHORIZATION_REQUIRED",
      "stack": "Error: Authorization Required\n    at ..."
   }
}
like image 21
Shyri Avatar answered Oct 25 '22 08:10

Shyri