Skip to content

Expose full API error fields: source, step, reason, metadata#417

Open
rzp-slash[bot] wants to merge 2 commits into
masterfrom
fix/expose-full-error-fields
Open

Expose full API error fields: source, step, reason, metadata#417
rzp-slash[bot] wants to merge 2 commits into
masterfrom
fix/expose-full-error-fields

Conversation

@rzp-slash

@rzp-slash rzp-slash Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Problem

The Razorpay API error response includes source, step, reason, and metadata fields alongside code, description, and field. The PHP SDK was silently discarding the first four, making them inaccessible to callers.

Changes

  • src/Errors/Error.php — Added $field, $source, $step, $reason, $metadata properties to the base class with a unified constructor and getters (getField(), getSource(), getStep(), getReason(), getMetadata()). All error types (BadRequestError, GatewayError, ServerError) now inherit these.
  • src/Errors/BadRequestError.php — Simplified; $field and getField() now live in the base Error class (backwards-compatible — getField() still works on BadRequestError instances).
  • src/Request.phpprocessError() now extracts all five optional fields and passes them through to the exception constructor.

Backwards compatibility

  • getField() on BadRequestError continues to work — method moved to base class, same behaviour.
  • GatewayError and ServerError constructors unchanged in call sites; new params default to null.
  • throwServerError() still calls the 3-arg form — no change needed.

Usage after this fix

try {
    $api->payment->fetch('pay_invalid');
} catch (\Razorpay\Api\Errors\BadRequestError $e) {
    echo $e->getField();    // e.g. "amount"
    echo $e->getSource();   // e.g. "business"
    echo $e->getStep();     // e.g. "payment_initiation"
    echo $e->getReason();   // e.g. "input_validation_failed"
    echo $e->getMetadata(); // array or null
}

🤖 Generated with Claude Code

All error types now capture and expose the full set of fields returned
by the Razorpay API error object (source, step, reason, metadata, field)
via getters on the base Error class. Previously only code, description,
and field (on BadRequestError) were accessible to callers.

Co-authored-by: ankitdas13 <ankit.das@razorpay.com>
… scoping, metadata type safety

- Fix: guard $description access with ?? '' — verifyErrorFormat only checked
  'code', so a missing 'description' key produced a null exception message
- Fix: call parent::__construct($message) so Exception chaining (getPrevious)
  works correctly
- Fix: move $field back to BadRequestError only — field is a validation-domain
  concept and does not belong on GatewayError / ServerError
- Fix: replace 8 positional params with array $context on base Error, removing
  fragile positional coupling for future API schema additions
- Fix: getMetadata() guards against non-array values with is_array() check
- Fix: use ?? operator and extract $err = $body['error'] to eliminate
  repeated key lookups and isset/ternary verbosity

Co-authored-by: ankitdas13 <ankit.das@razorpay.com>
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