from fastapi import HTTPException, status


class AppError(HTTPException):
    """
    应用程序自定义错误类
    """
    def __init__(
        self,
        detail: str,
        status_code: int = status.HTTP_400_BAD_REQUEST,
        headers: dict = None
    ):
        super().__init__(status_code=status_code, detail=detail, headers=headers)


# 常用错误
class NotFoundError(AppError):
    """
    资源未找到错误
    """
    def __init__(self, detail: str = "资源未找到"):
        super().__init__(detail=detail, status_code=status.HTTP_404_NOT_FOUND)


class UnauthorizedError(AppError):
    """
    未授权错误
    """
    def __init__(self, detail: str = "未授权"):
        super().__init__(
            detail=detail,
            status_code=status.HTTP_401_UNAUTHORIZED,
            headers={"WWW-Authenticate": "Bearer"}
        )


class ForbiddenError(AppError):
    """
    禁止访问错误
    """
    def __init__(self, detail: str = "禁止访问"):
        super().__init__(detail=detail, status_code=status.HTTP_403_FORBIDDEN)


class BadRequestError(AppError):
    """
    请求参数错误
    """
    def __init__(self, detail: str = "请求参数错误"):
        super().__init__(detail=detail, status_code=status.HTTP_400_BAD_REQUEST)


class ConflictError(AppError):
    """
    资源冲突错误
    """
    def __init__(self, detail: str = "资源冲突"):
        super().__init__(detail=detail, status_code=status.HTTP_409_CONFLICT)