如何区分故意的,控制器生成的404与REST API中的实际错误?

时间:2022-10-09 23:31:28

In a JSON-REST service architecture (following these patterns for methods and response codes) we often need to generate a deliberate 404 response - for example, if GET /users/123 is routed to a controller, which is then unable to find a User entity with ID 123, we return a 404 response, which in many cases will include a JSON payload with an error message/code/etc.

在JSON-REST服务体系结构中(遵循方法和响应代码的这些模式),我们经常需要生成有意的404响应 - 例如,如果GET / users / 123被路由到控制器,然后无法找到用户ID为123的实体,我们返回404响应,在许多情况下,它将包含带有错误消息/代码/等的JSON有效负载。

Now, when we provide a client for a specific API, we want the client to behave differently under different conditions. For example, if we point the client to the wrong host, we might get a 404 not found from that host - as opposed to the 404 we might get for an invalid User ID if we do reach the service.

现在,当我们为特定API提供客户端时,我们希望客户端在不同条件下的行为不同。例如,如果我们将客户端指向错误的主机,我们可能会从该主机中找不到404 - 而不是我们在达到该服务时可能获得的无效用户ID 404。

In this case, a "404 User ID not found" is not an error, as far as the client is concerned - as opposed to any other "404 Not Found", which should cause the client to throw an exception.

在这种情况下,就客户端而言,“找不到404用户ID”不是错误 - 而不是任何其他“404 Not Found”,这应该导致客户端抛出异常。

My question is, how do you distinguish between these 404 errors?

我的问题是,你如何区分这404错误?

Solely based on the response?

仅基于回应?

By adding a header to indicate a valid response?

通过添加标题来指示有效的响应?

Or some other way?

或者其他一些方式?

1 个解决方案

#1


1  

It is OK to return 404 in both cases. As 4xx codes are client relevant codes, it is also OK to return content even if there was an error.

在两种情况下都可以返回404。由于4xx代码是客户端相关代码,即使出现错误也可以返回内容。

Now, deciding what kind of 404 it was can be decided based on the body of the response. Remember, that the response should carry a mime-type that is compatible with the Accept header the client supplied. So if the client "knows" your specific error-describing format, your server can answer with a more detailed description.

现在,可以根据响应的主体来决定它是什么类型的404。请记住,响应应该带有与客户端提供的Accept标头兼容的mime类型。因此,如果客户端“知道”您的特定错误描述格式,您的服务器可以回答更详细的描述。

This way both the server can decide whether the client would understand a detailed response with the 404, and the client also understands when it just got a regular 404, or one with a message it can process.

这样,服务器可以决定客户端是否理解404的详细响应,并且客户端也可以理解它何时获得常规404,或者具有可以处理的消息的客户端。

This would be both semantically correct, and compatible with HTTP.

这在语义上都是正确的,并且与HTTP兼容。

#1


1  

It is OK to return 404 in both cases. As 4xx codes are client relevant codes, it is also OK to return content even if there was an error.

在两种情况下都可以返回404。由于4xx代码是客户端相关代码,即使出现错误也可以返回内容。

Now, deciding what kind of 404 it was can be decided based on the body of the response. Remember, that the response should carry a mime-type that is compatible with the Accept header the client supplied. So if the client "knows" your specific error-describing format, your server can answer with a more detailed description.

现在,可以根据响应的主体来决定它是什么类型的404。请记住,响应应该带有与客户端提供的Accept标头兼容的mime类型。因此,如果客户端“知道”您的特定错误描述格式,您的服务器可以回答更详细的描述。

This way both the server can decide whether the client would understand a detailed response with the 404, and the client also understands when it just got a regular 404, or one with a message it can process.

这样,服务器可以决定客户端是否理解404的详细响应,并且客户端也可以理解它何时获得常规404,或者具有可以处理的消息的客户端。

This would be both semantically correct, and compatible with HTTP.

这在语义上都是正确的,并且与HTTP兼容。