If the page itself doesn't exist, I'll obviously use 404.
What you said there is a bit confusing but I will have to assume that you're developing an API backend.
The problem is that whoever is consuming the API endpoint may be confused in two ways:
- They may think the 404 returned is because the endpoint(resource) wasn't reached, Or
- They might think that the item or user being requested wasn't found.
So the trick is, how are they going to know which is the right assumption?
Well, the answer is simple. Always try to attach a body to any errors that you returned from code. Errors that are returned by the server automatically do not have a body. So try to attach a body which you can document so that they can be able to use the content of the body to differentiate between code returned errors and server errors.
But in a nutshell, 404 is the right status to return, but try to attach a body to it indicating why 404 was returned.
An example could be:
// For illustration I'm just gonna use C#
Return NotFound(new { errorMessage: "Item requested was not found" });
Here, NotFound
returns a 404 statuscode and the parameter is an object like
{ errorMessage: "some reason for the error"}
. This way, you can always check if your error returned a body, and the you know it's returned from your code. Otherwise, the resource(link) wasn't found.