什么是响应在没有足够权限的情况下删除资源的请求而在 rest API 中引发的正确异常?

what is Correct Exception to raise in a rest API in response to request to delete a resource without enough permission?

我正在创建休息 API,使用 ASP.net 核心和 bit-framework
我们希望允许客户端能够删除 他们自己创建的资源

问题:

如果客户端要求删除由另一个客户端创建的资源,

  1. 在 API 中提出的最佳异常是什么?

  2. return最正确的 HTTP 状态码是什么?

  3. Bit.Owin.Exceptions命名空间中实现的所有异常是:\

    1. BadRequestException
    2. ResourceNotFoundException
    3. AppException
    4. DomainLogicException

    我应该在我的 API 中坚持使用这个例外列表吗?这个例外列表是否会包含更多例外以涵盖更多场景?


  1. 我认为这些状态代码之一必须 returned,但哪一个套件更适合我们的条件?:

    • 403 禁止
    • 405 不允许
    • 409资源冲突

我不熟悉您使用的框架。但是让我给你我的 2 美分。从 API 消费者的角度来看,403 状态代码对于您问题中描述的情况似乎是一个相当合理的选择:

6.5.3. 403 Forbidden

The 403 (Forbidden) status code indicates that the server understood the request but refuses to authorize it. A server that wishes to make public why the request has been forbidden can describe that reason in the response payload (if any). [...]

或者,如果您打算隐藏资源的存在,抛出映射到 404:

的异常

An origin server that wishes to "hide" the current existence of a forbidden target resource MAY instead respond with a status code of 404 (Not Found).

基于,您可以根据以下文档创建自己的异常类型:

https://docs.bit-framework.com/introduction/web-api#exception-handling

public class CanNotDeleteOtherClientResourceException : Exception, IKnownException, IHttpStatusCodeAwareException
{
    public CanNotDeleteOtherClientResourceException(string message)
        : base(message)
    {
    }

    public HttpStatusCode StatusCode { get; set; } = HttpStatusCode.Forbidden;
}