Sunday, 2 September 2012

Handling Exceptions In REST API wrappers in salesforce and setting proper HTTP error in header of HTTP response sent from Salesforce

To set the proper error code in the response packet returned from salesforce there is default HTTP error code table

The documentation of valid http error codes supported is in the http response is accessed on clicking below Documentation on HTTP error codes

To handle this in code here is a below simple example that demonstrates how to set the HTTP error codes in salesforce

@RestResource(urlMapping='/Account/*')
global with sharing class accountrest {
@HttpGet
global static Account getMerchandiseById() {
RestRequest req = RestContext.request;
RestResponse res=RestContext.response;
Account result;
String accId = req.requestURI.substring(
req.requestURI.lastIndexOf('/')+1);
 try{
  result =[SELECT Name,AccountNumber,NumberofLocations__c FROM Account WHERE Id = :accId];
}
catch(exception e){
System.debug(e.getMessage());
res.statusCode=400;//clear that error was due to account ID missing and hence BAD request
}
return result;
}

Introducing Lightning Base Components

Lightning Base Components are great addition to the platform and in fact revolutionary .One of the concerns around lightning component ...