def _handle_json_response()

in client/securedrop_client/sdk/__init__.py [0:0]


    def _handle_json_response(self, stdout_bytes: bytes) -> JSONResponse:
        try:
            result = json.loads(stdout_bytes.decode())
        except json.decoder.JSONDecodeError as err:
            raise BaseError("Unable to parse stdout JSON") from err

        if result["status"] == http.HTTPStatus.GATEWAY_TIMEOUT:
            raise RequestTimeoutError
        elif result["status"] == http.HTTPStatus.BAD_GATEWAY:
            raise ServerConnectionError
        elif result["status"] == http.HTTPStatus.FORBIDDEN:
            raise AuthError("Forbidden")
        elif result["status"] == http.HTTPStatus.BAD_REQUEST:
            # FIXME: return a more generic error
            raise ReplyError("bad request")
        elif (
            str(result["status"]).startswith(("4", "5"))
            and result["status"] != http.HTTPStatus.NOT_FOUND
        ):
            # We exclude 404 since if we encounter a 404, it means that an
            # item is missing. In that case we return to the caller to
            # handle that with an appropriate message. However, if the error
            # is not a 404, then we raise.
            logger.error(f"API error: status={result['status']}")
            raise BaseError(f"Unknown error, status: {result['status']}")

        data = json.loads(result["body"])
        return JSONResponse(data=data, status=result["status"], headers=result["headers"])