Refactor cURL error handling to preserve response data - #75
Merged
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR refactors the cURL error handling to preserve HTTP response data and include response codes in exceptions. The changes address the issue where response bodies were lost when HTTP errors occurred due to premature handle resets.
- Deferred
curl_easy_reset()to only execute after successful requests - Enhanced
CurlExceptionto optionally store response body data - Removed automatic failure on HTTP errors to allow response capture
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/curlWrapper.hpp | Added try-catch to preserve response body in CurlException and disabled auto-fail on HTTP errors |
| src/curlSingleHandler.hpp | Moved curl_easy_reset to only execute on success, preserving response data for error cases |
| shared/curlException.hpp | Enhanced CurlException class to store and provide access to HTTP response body |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
GabrielEValenzuela
force-pushed
the
dev/32416_curl_error_log
branch
from
October 9, 2025 16:39
1d5bfe1 to
88c09c2
Compare
…rror handling in cURL classes
GabrielEValenzuela
force-pushed
the
dev/32416_curl_error_log
branch
from
October 9, 2025 16:42
88c09c2 to
40acefc
Compare
…sponses as exceptions
2 tasks
MarcelKemp
requested changes
Oct 15, 2025
…body on HTTP errors
…error handling in cURLSingleHandler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes wazuh/wazuh#32416
Summary
This change refactors the cURL execution logic to improve error handling and diagnostics. Specifically, it allows the
CurlExceptionto carry the HTTP response code while preserving the response body for inspection in case of errors.Problem Statement
Previously, the cURL handler would reset the easy handle (
curl_easy_reset) immediately after an error, which cleared any buffered response data. This made it impossible to access the server’s response body in failure cases (e.g., 4xx/5xx responses).Additionally, the use of
OPT_FAILONERRORcausedcurl_easy_perform()to fail automatically on HTTP errors (≥ 400), without giving us a chance to read the full response.Observed behavior:
curl_easy_perform()returnedCURLE_HTTP_RETURNED_ERROR.Expected behavior:
Solution
The
execute()method in the cURL handler has been refactored to preserve the response context and provide richer exceptions.1. Defer
curl_easy_reset()The reset now occurs only on success, ensuring the response data remains accessible when an exception is thrown:
2. Preserve Response and HTTP Code on Errors
When
CURLE_HTTP_RETURNED_ERRORoccurs, the HTTP response code is retrieved and passed into theCurlException, allowing downstream consumers to handle errors based on status code:3. Remove
OPT_FAILONERRORThe following option is now commented out:
// this->setOptionLong(OPT_FAILONERROR, 1l);This prevents automatic failure on HTTP errors, letting the new
execute()logic handle them manually while still capturing the response body.Changes Made
curlSingleHandler.cppcurl_easy_reset()to only execute on successful requests.curlWrapper.hppCurlExceptionto accept an optional HTTP response code argument.Removed automatic fail-on-error option (
OPT_FAILONERROR) to allow full response capture on server errors.