Repository: Significant-Gravitas/Auto-GPT
File: autogpt_platform/backend/backend/blocks/http.py (Python)

Code before:
                    # we should send it as plain text instead
                    input_data.json_format = False

        try:
            response = requests.request(
                input_data.method.value,
                input_data.url,
                headers=input_data.headers,
                json=body if input_data.json_format else None,
                data=body if not input_data.json_format else None,
            )
            result = response.json() if input_data.json_format else response.text

            if response.ok:
                yield "response", result
            elif 400 <= response.status_code < 500:
                yield "client_error", result
            elif 500 <= response.status_code < 600:
                yield "server_error", result
            else:
                logger.warning(f"Unexpected status code: {response.status_code}")
                yield "error", f"Unexpected status code: {response.status_code}"

        except req.exceptions.HTTPError as e:
            # Handle HTTP errors from raise_for_status()
            if e.response:
                status_code = e.response.status_code

                if 400 <= status_code < 500:
                    result = (
                        e.response.json()
                        if input_data.json_format and hasattr(e.response, "json")
                        else str(e)
                    )
                    yield "client_error", result
                elif 500 <= status_code < 600:
                    result = (
                        e.response.json()
                        if input_data.json_format and hasattr(e.response, "json")
                        else str(e)
                    )
                    yield "server_error", result
                else:
                    yield "error", str(e)

        except req.exceptions.RequestException as e:
            # Handle other request-related exceptions
            yield "error", str(e)

        except Exception as e:
            # Catch any other unexpected exceptions

Review comment (suggestion):
This second check seems redundant. If an `HTTPError` is raised, that means an HTTP response with a status code >=400 was received.

Code after:
                    # we should send it as plain text instead
                    input_data.json_format = False

        try:
            response = requests.request(
                input_data.method.value,
                input_data.url,
                headers=input_data.headers,
                json=body if input_data.json_format else None,
                data=body if not input_data.json_format else None,
            )
            result = response.json() if input_data.json_format else response.text
            yield "response", result

        except HTTPError as e:
            # Handle error responses
            try:
                result = e.response.json() if input_data.json_format else str(e)
            except json.JSONDecodeError:
                result = str(e)

            if 400 <= e.response.status_code < 500:
                yield "client_error", result
            elif 500 <= e.response.status_code < 600:
                yield "server_error", result
            else:
                error_msg = (
                    "Unexpected status code "
                    f"{e.response.status_code} '{e.response.reason}'"
                )
                logger.warning(error_msg)
                yield "error", error_msg

        except RequestException as e:
            # Handle other request-related exceptions
            yield "error", str(e)

        except Exception as e:
            # Catch any other unexpected exceptions
            yield "error", str(e)