MasterJoe
MasterJoe

Reputation: 2335

Is it okay to return 404 when child resources do not exist?

Imagine a web app which lets you manage chat room servers & the chat rooms in them. Each server can host zero or more chatrooms. When a server has zero chat rooms, then the back end api (app/server7/chatrooms) of the app returns a 404 to the UI. The UI then shows "No chat rooms in this server, click here to create some." But, when there are some chat rooms, it returns list of rooms and information about them.

This seems like bad design to me. Say if we create chat rooms in a server, but the back end does not actually create them, or the chatrooms resource gets deleted etc. Then, the UI will wrongly assume that there are no chatrooms.

I believe that chatrooms resource should be created when a server is created. Also, the chatrooms call should return a list of zero elements instead of 404.

Is my reasoning correct ?

Upvotes: 1

Views: 77

Answers (1)

Gabriel Avellaneda
Gabriel Avellaneda

Reputation: 742

As per your definition of the relationship between server and chatrooms, a server will always have a definition of the number of chatrooms (0 or more, not null), so in this specific case it could be best to create the chatrooms resource at the time you create the server resource and to return 0 when the user calls your chatrooms api endpoint.

This will allow you to:

  • Not be confused that the API endpoint is not working because of a change in its structure.
  • Not be confused with the case that actually the server does not exist. For example api/server/_some_inexistent_server_id/chatrooms.

On the other hand, if you want to stay with 404 errors probably will be good to return a response body with more details.

Upvotes: 1

Related Questions