Consider this very simple class for querying accounts:
@RestResource(urlMapping='/accounts/*')
global with sharing class AccountRestService {
@HttpGet
global static List<Account> getAccounts() {
List<Account> accounts = [SELECT Id, Name FROM Account];
return accounts;
}
}
Easy - I do a GET and get a list of all my accounts.
Now - for some reason this query might fail and we want to correctly signal this to the one using the interface. So in this case we want to return a 500 with the error message as our body.
@RestResource(urlMapping='/accounts/*')
global with sharing class AccountRestService {
@HttpGet
global static List<Account> getAccounts() {
List<Account> accounts;
try {
accounts = [SELECT Id, Name FROM Account];
} catch (Exception e) {
RestResponse res = RestContext.response;
res.responseBody = Blob.valueOf(e.getMessage());
res.statusCode = 500;
}
return accounts;
}
}
This looks nice - however the return statement will overwrite our response in case of an error which is clearly not what we want. Of course you can make it void and do the returning of the data (and the serialization) all by yourself - but that seems a lot of work for something that's done and should be done by default. Also - all the examples I found so far do have return values but do not handle these cases in any way.
Am I missing something obvious here? Since I never know how my methods will eventually evolve that just leaves me with all void methods since I cannot change the signature of global methods in a managed package.