Skip to content

Surface transport errors from the HTTP client (fixes #184)#349

Open
TowyTowy wants to merge 1 commit into
mautic:mainfrom
TowyTowy:fix/184-curl-error-feedback
Open

Surface transport errors from the HTTP client (fixes #184)#349
TowyTowy wants to merge 1 commit into
mautic:mainfrom
TowyTowy:fix/184-curl-error-feedback

Conversation

@TowyTowy

@TowyTowy TowyTowy commented Jul 9, 2026

Copy link
Copy Markdown

Fixes #184.

When the PSR-18 HTTP client cannot reach the API (DNS failure, connection timeout, TLS error, ...), the failure previously surfaced only as a generic "unexpected status code (0)" message or leaked an implementation-specific exception, making connection problems hard to diagnose.

AbstractAuth::makeRequest() now catches Psr\Http\Client\ClientExceptionInterface around sendRequest() and rethrows a new Mautic\Exception\ConnectionException whose message includes the target URL and the underlying transport error (for cURL-based clients, the original cURL error message and number). The original exception is kept as getPrevious().

Backwards compatibleConnectionException extends AbstractApiException (→ \Exception), so existing catch (\Exception) callers keep working and just get a better message. Adds a hermetic unit test; PHPStan, php-cs-fixer and Rector all pass.

@escopecz requested a PR for this in the issue thread.

When the PSR-18 HTTP client could not reach the API (DNS failure,
connection timeout, TLS error, ...) the failure surfaced only as a
generic "The response has unexpected status code (0)" message, or leaked
as an implementation-specific exception. That made connection problems
very hard to diagnose, as reported in mautic#184.

AbstractAuth::makeRequest now catches Psr\Http\Client\ClientExceptionInterface
around the sendRequest() call and rethrows a new
Mautic\Exception\ConnectionException whose message includes the target
URL and the underlying transport error. For cURL-based clients this
carries the original cURL error message and number. The original
exception is preserved as the previous exception for further inspection.

Backwards compatible: ConnectionException extends AbstractApiException
(and therefore \Exception), so Api::makeRequest and any caller catching
\Exception keep working and now receive the richer message.

Requested by @escopecz in the issue thread.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@promptless-for-oss

Copy link
Copy Markdown

Promptless prepared a documentation update related to this change.

Triggered by api-library PR #349

The PHP API library now throws a new ConnectionException when a request can't reach Mautic (DNS failure, connection timeout, TLS error), with a message that surfaces the target URL and the underlying transport error. The existing developer docs only documented server-side JSON error responses, so this adds a "Handling connection errors with the API library" subsection with a try/catch example.

Review: docs PR #561

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle better curl calls - give better error feedback

3 participants