2

I have a RESTful web application with an endpoint that allows the user to delete a particular resource by providing its id:

DELETE /rules/{id}

In some cases, one rule may reference another rule. If Rule A references Rule B, and a user attempts to delete Rule B, I'm not allowing that.

In my server code, I'd like the delete method in my manager (one layer deeper than the REST method) to throw an exception when a rule cannot be deleted because it's being referenced by another rule. My REST method will then have a catch block for this exception and formulate the appropriate HTTP response.

What is an appropriate exception for my manager method to throw in this case?

Matt
  • 23,363
  • 39
  • 111
  • 152
  • 3
    Would you consider it a fit for an `IllegalStateException`? I'm not sure in this context if you consider it to be an "illegal state" or not. – Carcigenicate Feb 06 '17 at 18:51
  • 1
    Just make your own exception. `public class RuleDeleteException extends Exception` – jseashell Feb 06 '17 at 18:51
  • @Carcigenicate I guess that's workable if you consider "In Use" as a state. It's a good thought... – Matt Feb 06 '17 at 18:57
  • @j.seashell Yes, that's always an option. I was hoping there was a standard exception for cases like these. – Matt Feb 06 '17 at 18:58
  • 1
    What J. said, but I think I would consider having `RuleDeleteException` extend `IOException` instead of just `Exception`. Given it's a REST app, it might be easier in the long run to have everything be an IO exception if you want to make a high level common error handler. – markspace Feb 06 '17 at 18:58
  • I would say `IllegalStateException`, but that is an unchecked exception, meaning it shouldn't be used to indicate a transient problem (I.e. it might succeed if you try exactly the same thing later). A checked exception is more appropriate. – Andy Turner Feb 06 '17 at 19:03

0 Answers0