Module: A2A::Errors::ErrorUtils

Defined in:
lib/a2a/errors.rb

Overview

Utility methods for error handling

Class Method Summary collapse

Class Method Details

.exception_to_json_rpc_error(exception, request_id: nil) ⇒ Hash

Convert an exception to a JSON-RPC error response

Parameters:

  • exception (Exception)

    The exception to convert

  • request_id (String, Integer, nil) (defaults to: nil)

    The request ID

Returns:

  • (Hash)

    JSON-RPC error response



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/a2a/errors.rb', line 213

def self.exception_to_json_rpc_error(exception, request_id: nil)
  if exception.is_a?(A2AError)
    A2A::Protocol::JsonRpc.build_error_response(
      code: exception.code,
      message: exception.message,
      data: exception.data,
      id: request_id
    )
  else
    # Map standard Ruby exceptions to JSON-RPC errors
    case exception
    when ArgumentError
      A2A::Protocol::JsonRpc.build_error_response(
        code: A2A::Protocol::JsonRpc::INVALID_PARAMS,
        message: exception.message,
        id: request_id
      )
    when NoMethodError
      A2A::Protocol::JsonRpc.build_error_response(
        code: A2A::Protocol::JsonRpc::METHOD_NOT_FOUND,
        message: "Method not found",
        id: request_id
      )
    else
      A2A::Protocol::JsonRpc.build_error_response(
        code: A2A::Protocol::JsonRpc::INTERNAL_ERROR,
        message: exception.message,
        id: request_id
      )
    end
  end
end

.from_json_rpc_code(code, message, data: nil) ⇒ A2AError

Create an A2A error from a JSON-RPC error code

Parameters:

  • code (Integer)

    The error code

  • message (String)

    The error message

  • data (Object, nil) (defaults to: nil)

    Additional error data

Returns:

  • (A2AError)

    The appropriate error instance



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/a2a/errors.rb', line 253

def self.from_json_rpc_code(code, message, data: nil)
  case code
  when A2A::Protocol::JsonRpc::PARSE_ERROR
    ParseError.new(message, data: data)
  when A2A::Protocol::JsonRpc::INVALID_REQUEST
    InvalidRequest.new(message, data: data)
  when A2A::Protocol::JsonRpc::METHOD_NOT_FOUND
    MethodNotFound.new(message, data: data)
  when A2A::Protocol::JsonRpc::INVALID_PARAMS
    InvalidParams.new(message, data: data)
  when A2A::Protocol::JsonRpc::INTERNAL_ERROR
    InternalError.new(message, data: data)
  when A2A::Protocol::JsonRpc::TASK_NOT_FOUND
    TaskNotFound.new(message, data: data)
  when A2A::Protocol::JsonRpc::TASK_NOT_CANCELABLE
    TaskNotCancelable.new(message, data: data)
  when A2A::Protocol::JsonRpc::INVALID_TASK_STATE
    InvalidTaskState.new(message, data: data)
  when A2A::Protocol::JsonRpc::AUTHENTICATION_REQUIRED
    AuthenticationRequired.new(message, data: data)
  when A2A::Protocol::JsonRpc::AUTHORIZATION_FAILED
    AuthorizationFailed.new(message, data: data)
  when A2A::Protocol::JsonRpc::RATE_LIMIT_EXCEEDED
    RateLimitExceeded.new(message, data: data)
  when A2A::Protocol::JsonRpc::AGENT_UNAVAILABLE
    AgentUnavailable.new(message, data: data)
  when A2A::Protocol::JsonRpc::PROTOCOL_VERSION_MISMATCH
    ProtocolVersionMismatch.new(message, data: data)
  when A2A::Protocol::JsonRpc::CAPABILITY_NOT_SUPPORTED
    CapabilityNotSupported.new(message, data: data)
  when A2A::Protocol::JsonRpc::RESOURCE_EXHAUSTED
    ResourceExhausted.new(message, data: data)
  else
    A2AError.new(message, code: code, data: data)
  end
end