Error Handling - Usergrid NPM Module and JS Promise Chains

rmishra
Participant V

Hello,

I am leveraging the usergrid module (version "~0.10.11") to access Apigee Baas from Apigee Edge. Also using bluebird module (version "^3.5.1") to create a promise chain. Everything works when it works but when there is an HTTP error (e.g. 404 if the entity is not found), i am not able to capture the details of the http error.

Here, is my code

const express = require('express')
    app = express(),
    Promise = require('bluebird'), 
    Usergrid = require('usergrid');
    request = require("request");
    util = require('util');


// Initialize Usergrid
var userGridClient = new Usergrid.client({
                orgName:'***',
                appName:'***',
                authType:Usergrid.AUTH_CLIENT_ID,
                clientId:'***',
                clientSecret:'***',
                  URI: '***',
                logging: true, //optional - turn on logging, off by default
                buildCurl: true //optional - turn on curl commands, off by default
            });


var userGridRequest = Promise.promisify(userGridClient.request, {context:userGridClient});


app.get('/', function(req, resp) {
       getEntities(req, resp);
});




function getEntities(req, res) {


var userGridRequestOptions = {
      'method':'GET',
      'endpoint':'XX',
      'type': 'XX'
    }


return userGridRequest(userGridRequestOptions).then(function(response){


        //Function goes here


      }).catch(function(error){
        console.log(error)
        res.jsonp(500, {
                    'error' : JSON.stringify(error)
                    });
      })


}




// Listen for requests until the server is stopped
var port = process.env.PORT || 9000;
app.listen(port, function(){
  console.log('The server is listening on port %d', port);
});

When an HTTP error occurs, the code goes inside the "catch" block, instead of the usual response. The error details in the catch block have no information about the HTTP error.

Now, Usergrid uses the 'request' library, but when i use the request library directly.

var request2 = Promise.promisify(require("request"));

 request2('http://host.domain.com/sqe')
             .then(function(response){
               console.log('response is '+response.body);
             }).catch(function(error){
               console.log('error is '+error.response);
             });

Prints 
response is {"error":"not_allowed","timestamp":1512144588339,"duration":0,"error_description":"HTTP 405 Method Not Allowed","exception":"javax.ws.rs.NotAllowedException"}


The code with the usual request library works just as i expect.

Has anyone tried usergrid with promise chains? What am i missing?

0 1 222
1 REPLY 1

I don't know what you're missing. I don't know whether the problem is in the usergrid module, or in the Bluebird promisify wrapper around the usergrid module.

In any case you have a choice - diagnose and debug the interaction between bluebird and the usergrid module, or ... just write your own promise-enabled wrapper for the UG requests. UG is just a REST resource, so you should be able to write a thin yet usable wrapper pretty simply.