2

Is it fine to throw an exception on the server side when the requested resource was not found?

the client receives a 404 not found. My concern is whether it is fine/wrong to throw an exception on the server side when this happens.

DesirePRG
  • 6,122
  • 15
  • 69
  • 114
  • How would you define "wrong"? Does it violate REST? Does it violate OO? Is it considered a good/bad practice when using framework X? Without this clarification the question is pretty subjective. – biziclop Apr 21 '17 at 12:17
  • What framework are you using to create your REST application? – cassiomolin Apr 21 '17 at 12:18
  • 1
    As long as you handle the exception appropriately, i.e. cleanup any used resources and restore the server to a known good state, it's a valid way to handle the issue but by no means the only one. – twain249 Apr 21 '17 at 12:19
  • @DesirePRG It uses the JAX-RS API, doesn't it? – cassiomolin Apr 22 '17 at 16:23

4 Answers4

3

It's hard to assume what your are trying to do with the level of details you added to your question.

However, if you handle the exceptions properly, there may be nothing wrong with that approach. Have a look at the approaches used by some frameworks:

JAX-RS

Spring MVC

Community
  • 1
  • 1
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
1

Basically it is not ideal to throw reserved status codes of exception. You should handle this exceptions internally and prepare your own code with meaning full message that client should know the actual problem.

Anurag Dadheech
  • 629
  • 1
  • 5
  • 14
0

I would say add a filter to capture 404 and add custom information about the 404 details.

In case of pure REST implementation, any resource-id missing and malformed URL will return 404. As far as REST contract, both cases are correct to have 404 response. But more details on what type of resource is missing will help the client side consuming it to take appropriate actions.

Related discussion: return-404-when-a-rest-resource-is-not-found

Community
  • 1
  • 1
arunk2
  • 2,246
  • 3
  • 23
  • 35
0

From perspective of semantics: Exception should be thrown if condition is such that condition is unrecoverable and devs must be notified about it.

Server cannot resolve auth request in the beginning of a session - this is serious enough situation and exception is appropriate.

User didn't fill out obligatory field and tried sending a form. This problem can be fixed and an exception would be a bad design.

Stepan
  • 1,391
  • 18
  • 40
  • Then why does java define http://docs.oracle.com/javaee/7/api/javax/ws/rs/NotFoundException.html – DesirePRG Apr 21 '17 at 14:04
  • Server cannot auth user because database with userIDs isn't found - this is a disaster: app can't do anything because of "Resource not found" situation. It is time to throw an exception. – Stepan Apr 21 '17 at 17:40
  • If ads stopped working on your partner's site - this is also a "resource not found" case. But this is NOT a disaster. Rather, this is a blessing. Don't trow exception in this case. – Stepan Apr 21 '17 at 17:41