Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Access to Real cURL Error Code in cpr::Error #1108

Closed
Deatha69 opened this issue Sep 14, 2024 · 3 comments
Closed

Allow Access to Real cURL Error Code in cpr::Error #1108

Deatha69 opened this issue Sep 14, 2024 · 3 comments
Assignees
Milestone

Comments

@Deatha69
Copy link

Is your feature request related to a problem?

Currently, the cpr::Error object converts cURL error codes into cpr::ErrorCode using the cpr::Error::getErrorCodeForCurlError method. However, the original cURL error code is discarded after instantiation, and there is no way to retrieve it.

I ran into this limitation in my project when the public key pinned to a domain was changed unexpectedly by Cloudflare, and the app has stopped connecting to the server all of the sudden. CPR converted the cURL error CURLE_SSL_PINNEDPUBKEYNOTMATCH into cpr::ErrorCode::INTERNAL_ERROR, which did not provide useful information about the underlying issue. In this case, access to the original cURL error would have made it much easier to diagnose the problem.

This is just one example, but it shows that retaining the real cURL error code could be valuable for debugging specific issues.

Possible Solution

I propose adding a property for the real cURL error code to the Error object to allow easy access. This would be helpful in situations where the current conversion to cpr::ErrorCode is too generic and doesn't provide enough information for specific error codes.

class Error {
  public:
    ErrorCode code = ErrorCode::OK;
    std::string message{};
    std::int32_t curl_code = CURLE_OK; // Real cURL code

    Error() = default;

    Error(const std::int32_t& curl_code, std::string&& p_error_message) 
        : code{getErrorCodeForCurlError(curl_code)}, message(std::move(p_error_message)), curl_code{curl_code} {} // Instantiate curl_code property

    explicit operator bool() const {
        return code != ErrorCode::OK;
    }

  private:
    static ErrorCode getErrorCodeForCurlError(std::int32_t curl_code);
};

Alternatives

No response

Additional Context

No response

@COM8
Copy link
Member

COM8 commented Sep 20, 2024

@Deatha69 thanks for submitting this suggestion. I agree, the current cpr error codes are too generic. This needs to change. Instead of just forwarding curl error codes, I decided to make cpr error codes far more verbose by adding all missing curl error codes except the ones for unsupported protocols e.g. FTP.
Here is the PR: #1110

Does this fulfil your needs?

@Deatha69
Copy link
Author

Deatha69 commented Sep 20, 2024

@Deatha69 thanks for submitting this suggestion. I agree, the current cpr error codes are too generic. This needs to change. Instead of just forwarding curl error codes, I decided to make cpr error codes far more verbose by adding all missing curl error codes except the ones for unsupported protocols e.g. FTP. Here is the PR: #1110

Does this fulfil your needs?

Looks great! While I wanted to avoid breaking changes, I agree that it didn't really address the lack of error codes in cpr, so I guess it needed to be done. I also doubt there will be many changes to the CURLE enum in the future, and if there are, they likely won’t be significant.

@COM8 COM8 self-assigned this Sep 22, 2024
@COM8 COM8 closed this as completed Sep 22, 2024
@COM8
Copy link
Member

COM8 commented Sep 22, 2024

Will be a part of the 1.11.0 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants