dim0_0n
dim0_0n

Reputation: 2504

E_UNKNOWN error in response from beforeCreate and other lifecycle callbacks

In my sails.js application's model I want to sent "Bad Request" error code 400 from lifecycle callback. Here is my beforeCreate function:

beforeCreate: function (values, next) {
    if (!values.email) {
      next();
    } else{
      var err = {
        httpCode: 400,
        message: 'email not defined.'
      };
      return next(err);
    }
  }

Instead of sending 400 error it sends 500 error. Here is the output:

error: Sending 500 ("Server Error") response: 
 Error (E_UNKNOWN) :: Encountered an unexpected error
    at new WLError (/app/node_modules/sails/node_modules/waterline/lib/waterline/error/WLError.js:25:15)
    at duckType (/app/node_modules/sails/node_modules/waterline/lib/waterline/error/index.js:66:10)
    at errorify (/app/node_modules/sails/node_modules/waterline/lib/waterline/error/index.js:39:10)
    at wrappedCallback (/app/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:325:15)
    at callback.error (/app/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/normalize.js:42:31)
    at _switch (/app/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/factory.js:56:28)
    at /app/node_modules/sails/node_modules/waterline/lib/waterline/query/dql/create.js:73:23
    at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:726:13
    at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:52:16
    at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:264:21
    at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:44:16
    at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:723:17
    at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:167:37
    at /app/node_modules/sails/node_modules/waterline/lib/waterline/query/validate.js:73:23
    at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:726:13
    at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:52:16
    at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:264:21
    at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:44:16
    at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:723:17
    at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:167:37
    at /app/node_modules/sails/node_modules/waterline/lib/waterline/query/validate.js:35:27
    at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:52:16

Details:  { httpCode: 400, message: 'email not defined.' }

How can I achieve my goal?

Upvotes: 0

Views: 537

Answers (1)

selftaught91
selftaught91

Reputation: 7481

so as you can see the last line of the error that you received it is

Details: { httpCode: 400, message: 'email not defined.' }

so when we send an error object in the lifecycle callback of sails then waterline wraps its own implementation over the error we send

in the response we get a string in the format given

Details: { httpCode: 400, message: 'email not defined.' } //this is string

This string is present in the details property of error object

so

suppose your lifecycle callback is

beforeValidate: function(values,cb){
 cb(JSON.stringify({ httpCode: 400,
            code: 997,
            message: 'username already exists',
            context: 'user create',
            custom: true }));
}

try to stringify the error object that you are sending

In the controller you can recieve this error as string in the format

Details : {error object defined by you}

Now in the controller

// this error object is received inside the callback or catch of promise

if(error.hasOwnProperty('details')){
                var indexStart=error.details.indexOf('{');
                //this is where we are extracting the error code that we set inside lifecycle callback
                var data =error.details.substring(indexStart);
                res.json(500,JSON.parse(data));
            }else{
                res.json(500,{message : "your message",status : 'your status',data : error});
      }

Upvotes: 1

Related Questions