0

Utilizing a custom salesforce connect adapter, callouts are being made using the HTTPRequest and HTTPResponse classes.

40,000 requests to these webservices are being made and all but a few (2-5) are successful. On these few requests running into an exception in the connect controller, it is a null object exception on for res.getStatus: mapToReturnAsObject.put('responseStatus',res.getStatus());

Is it possible for a HTTP Response to come back without a response status?

Update:

global DataSourceConnection(DataSource.ConnectionParams connectionParams) {
    serviceReq = new HttpRequest();
    serviceReq.setHeader('Authorization', 'Bearer ' + tokenHere);
    serviceReq.setMethod('GET');
    serviceReq.setTimeout(reqTimeout);
    serviceReq.setHeader('ApplicationName', applicationName);
    serviceReq.setHeader('ApplicationID', applicationId);
    serviceReq.setHeader('ApplicationVersion', applicationVersion);
}
private List<Map<String,Object>> getRecords(){

Map<String, Object> mapToReturnAsObject = new Map<String, Object>();

serviceReq.setHeader('Header', Header);
serviceReq.setEndpoint(endPoint);
HttpResponse res;
try {
    res = new Http().send(serviceReq);
    system.debug(res.getBody());
}
catch {
    mapToReturnAsObject.put('responseStatus',res.getStatus());    
}
  • If you are getting a null exception on res.getStatus(), that necessarily means that you never received the response thus res was not initialized. As for your question, a HTTP Response will always have a status code, pass or fail. – Jayant Das Apr 5 at 17:59
  • It's in a try / catch. And in the try after setting the header and endpoint for the HTTP request, I am initializing: res = new Http().send(serviceReq); Is there any reason why initialization would fail? – S.B. Apr 5 at 18:11
  • 1
    Yes, if you new Http().send(serviceReq); threw an exception, res will never get initialized. And if you try to access res it will throw an error. Can you edit your question to provide some code as how are you calling the services and where you are getting the exception? – Jayant Das Apr 5 at 18:15
  • Updated with the connect controller being called through utilization of SOQL on an external object __x. – S.B. Apr 5 at 20:10
2

As for your question:

Is it possible for a HTTP Response to come back without a response status?

No. As defined by the RFC 2616, an HTTP Response will always consist of a response code.

Further, in your case here, res is declared outside your try/catch block. And thus if there was an exception thrown right during the callout and that there was no response ever received, your res will be always null and will throw a null exception.

The best way to address such scenario is to put a check on the response before using it in the catch block, in fact you don't need to use it in catch block. Just set it after the call, so you will always have a response if the callout was successful. And in the catch block, just use a custom status code which would identify that there was an exception, something as below.

HttpResponse res;
try {
    res = new Http().send(serviceReq);
    system.debug(res.getBody());

    // add the response status right here, because that will indicate callout was successful
    mapToReturnAsObject.put('responseStatus',res.getStatus());
}
catch (Exception e) {
    // no need to access res object here, if it came here then there was an exception
    // and that you can log the exception and add the exception message here

    system.debug(e.getStackTraceString());
    mapToReturnAsObject.put(
        'responseStatus', <put some custom message here> + e.getMessage());
}
  • Thanks for the elaborate answer, but I'm still having a hard time understanding why the res would ever run into exception when all of the variables, besides the header and token, that populate these headers come from a static custom setting. Even if the token or header were blank, could that cause the res request to fail? – S.B. Apr 5 at 20:35
  • Because the Exceptions are not very well documented in Salesforce, it could be possibly because of any unknown reasons. Say a network failure, or anything that we don't know of. Best is to log an exception see what caused it. In fact you should not be attempting to add a status code in an exception block but right after when you receive it. I will update my answer to reflect that. – Jayant Das Apr 5 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.