Skip to content

Enhances API Response Handling - #65

Merged
SergkeiM merged 1 commit into
masterfrom
feature/exceptions
Aug 14, 2026
Merged

SergkeiM merged 1 commit into
masterfrom
feature/exceptions

Conversation

@SergkeiM

Copy link
Copy Markdown
Owner

Updates API response parsing and error detection mechanisms:

  • Redefines "successful" responses to include all 2xx HTTP status codes, accurately reflecting Cloudflare's varied successful responses (e.g., 201, 204).
  • Introduces new methods (errors(), messages(), hasErrors()) to identify and process Cloudflare's envelope errors, which appear in the response body even when the HTTP status is 2xx (e.g., success: false).
  • Extends the json() method to support dot notation for easier access to deeply nested data within the response payload.

Changes

  • The successful() method now evaluates to true for any status code between 200 and 299.
  • Added errors(), messages(), and hasErrors() methods to the ResponseInterface and Response class for direct access to Cloudflare's operational error and message lists.
  • Implemented dot notation support in json() to simplify fetching nested data (e.g., $response->json('result.meta.step')).
  • The internal get helper method has been made private, removing it from the public contract.

Benefits

  • Provides a more accurate and robust determination of API operation success, differentiating between HTTP success and Cloudflare-reported issues within the payload.
  • Simplifies the process of checking for specific Cloudflare API errors, especially in bulk operations.
  • Improves developer experience by streamlining access to complex JSON response structures.

Testing

  • Added comprehensive tests for various 2xx status codes to ensure the successful() method behaves as expected.
  • Introduced tests for handling empty body responses on 204 No Content status.
  • Included tests to verify the functionality of dot notation in json() for nested data.
  • Added extensive tests for errors(), messages(), and hasErrors() under various scenarios, including 2xx statuses with envelope errors and when the envelope is absent.
  • Confirmed that 3xx redirect statuses now consistently throw RequestException.

Documentation

  • Updated the usage.md documentation to reflect the new definition of a successful response, the capabilities of dot notation in json(), and the usage of the new envelope error methods.

Checklist

  • Does your PR follow the contributing guidelines?
  • Have you added new tests or updated existing ones to cover the changes?
  • Have you updated the documentation accordingly?
  • Does your PR introduce a breaking change?
  • Do the changes require a release notes update?

Cloudflare's API often returns a 2xx status with `"success": false` and lists of `errors` or `messages` in the body. This change introduces `errors()`, `messages()`, and `hasErrors()` methods to easily inspect these envelope details.

The `successful()` method now correctly identifies all 2xx HTTP statuses, aligning with various API response patterns (e.g., 201, 202, 204).

Additionally, the `json()` method now supports dot notation for accessing nested values more conveniently.
@SergkeiM
SergkeiM merged commit ec5bbfb into master Aug 14, 2026
8 checks passed
@SergkeiM
SergkeiM deleted the feature/exceptions branch August 14, 2026 20:52
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.

1 participant