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

Code before:
                try:
                    # Try to parse as JSON first
                    body = json.loads(body)
                except json.JSONDecodeError:
                    # If it's not valid JSON and just plain text,
                    # 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:

Review comment (bug):
can we log more than this? this is no more help than the error message itself :')

To debug, the request URL at the very least would be helpful

Code after:
                try:
                    # Try to parse as JSON first
                    body = json.loads(body)
                except json.JSONDecodeError:
                    # If it's not valid JSON and just plain text,
                    # 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)