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) {, {
    restCode: 'ApiError',
    statusCode, statusCode,
    message: 'Api Error Occurred',
    constructorOpt: ServiceError,
    body: {
      message: 'Api Error Occurrede',
      errors: respObject.toJSON()
  }); = 'CustomApiError';

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

However on the calling function of test():

test().catch(function(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?



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.


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


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.