samedi 31 janvier 2015

What's a good pattern or style in REST Resource classes to encapsulate error handling?

I've run into this pattern several times. In pseudo-code:


public class BlahResource { if (thisError) buildResponse(BAD_REQUEST); if (thatError) buildResponse(CONFLICT); ... doSomething(); return buildResponse(SUCCESS); }


Doesn't look too bad, but when you have a million error conditions to handle, too much business logic accretes into the Resource class and the noise quickly overwhelms the story of what the code is actually doing.


Since in a resource the error conditions are returning something, an exception thrown from a service doesn't seem right to me. And a status object encapsulating different combinations of return conditions and setups seems like overkill.


Is there something obvious I'm missing in how this can be coded in a logical, clear way? Possibly something functional/Guava/lambda-based that I'm missing, or just a regular OO solution.


Aucun commentaire:

Enregistrer un commentaire