Restify returns undefined is not a function in error object

I have a below Restify custom error that is being thrown to my catch block of BlueBird Promise.

var test = function() {
  respObject = { hello: { world: 'aasas' } }; 
  throw new restify.errors.ServiceError(respObject, 422);
}

Then in ServiceError:

function ServiceError(respObject, statusCode) {
  restify.RestError.call(this, {
    restCode: 'ApiError',
    statusCode, statusCode,
    message: 'Api Error Occurred',
    constructorOpt: ServiceError,
    body: {
      message: 'Api Error Occurrede',
      errors: respObject.toJSON()
    }
  });

  this.name = 'CustomApiError';
}

util.inherits(ServiceError, restify.RestError);
restify.errors.ServiceError = ServiceError;

However on the calling function of test():

test().catch(function(err) {
  console.log(err);
});

It's returning undefined is not a function. Is there a reason why it's not returning err object to above calling function under catch block?

Answers:

Answer

The problem isn't with Restify, it's with your test function. You're calling test().catch, but test() doesn't return anything—i.e. it returns undefined. You're essentially calling undefined.catch, which doesn't exist.

If you want to call catch on the result of test, it needs to return a promise.

Answer

I found the answer. It was respObject.toJSON() that was causing undefined is not a function because respObject does not have toJSON() function :)

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.