RazorpayEx.Error (razorpay_ex v0.1.3)

Copy Markdown View Source

Error handling for Razorpay API.

This module provides structured error handling for Razorpay API responses. It converts Razorpay error codes into specific error types and provides detailed error information.

Error Structure

Razorpay errors include:

  • code: Error code (e.g., "BAD_REQUEST_ERROR")
  • description: Human-readable error description
  • field: Field that caused the error (if applicable)
  • step: Step in which error occurred
  • reason: Reason for the error
  • metadata: Additional error metadata
  • http_status_code: HTTP status code
  • http_body: Raw HTTP response body

Examples

case RazorpayEx.Payment.fetch("invalid_id") do
  {:ok, payment} ->
    # Handle success
  {:error, %RazorpayEx.Error{code: "BAD_REQUEST_ERROR"} = error} ->
    IO.puts("Bad request: #{error.description}")
  {:error, error} ->
    IO.puts("Unknown error: #{inspect(error)}")
end

Summary

Functions

Creates an error from a Razorpay error response.

Returns a string representation of the error.

Types

t()

@type t() :: %RazorpayEx.Error{
  code: String.t(),
  description: String.t(),
  field: String.t() | nil,
  http_body: String.t() | nil,
  http_status_code: integer(),
  metadata: map() | nil,
  reason: String.t() | nil,
  step: String.t() | nil
}

Functions

from_map(error_map, status_code)

@spec from_map(map(), integer()) :: t()

Creates an error from a Razorpay error response.

Parameters

  • error_map: Error map from Razorpay API
  • status_code: HTTP status code

Examples

error = Razorpay.Error.from_map(%{
  "code" => "BAD_REQUEST_ERROR",
  "description" => "Invalid payment id"
}, 400)

message(error)

@spec message(t()) :: String.t()

Returns a string representation of the error.

new(type, description, status_code \\ 0)

@spec new(atom() | String.t(), String.t(), integer()) :: t()

Creates a new error.

Parameters

  • type: Error type atom or string
  • description: Error description
  • status_code: HTTP status code (default: 0)

Examples

error = RazorpayEx.Error.new(:network_error, "Connection failed")
error = RazorpayEx.Error.new("BAD_REQUEST", "Invalid input", 400)