fflib-apex-common icon indicating copy to clipboard operation
fflib-apex-common copied to clipboard

fflib_SObjectDomain error(Exception e)

Open cropredyHelix opened this issue 6 years ago • 0 comments

A common pattern is:

opp.AccountId.addError(
   error('You must provide an Account for Opportunities for existing Customers.',
            opp, Opportunity.AccountId) );

Note: SFDC addError supports these signatures:

  • addError(String errorMsg)
  • addError(String errorMsg Boolean escape)
  • addError(Exception e)

You can easily do the example from Andy's blog because fflib_SObjectDomain error(...) returns a String so the first and second flavors of SFDC addError are supported.

But you can't directly do the third flavor which comes up when domain classes call service classes

  try {
     OpportunitiesService.doSomething(args);
  }
  catch (OpportunitiesService.MyException e) {
    for (Opportunity o : (Opportunity[]) Records) {  // assume we're failing entire batch, could be more fine-grained
      o.addError(error(e),o);  // error doesn't take exception as an arg
   }
 }

Instead, you need to do something like this:

o.addError(error(String.valueOf(e),o));  // gets typename and message only

or

o.addError(error(String.valueOf(e) + ' ' + e.getStackTraceString(),o));

The ask:

Add two new signatures for error(..), still returning String

error(Exception e, Sobject sobject)
error(Exception e, Sobject sobject, Boolean includeStackTrace)

If you think this is worthy, I can add it to my list of undone Pull Requests :-)

cropredyHelix avatar Dec 21 '18 00:12 cropredyHelix