Skip to content

Commit

Permalink
Return error info on tx errors (#40)
Browse files Browse the repository at this point in the history
* Return error info on tx submission and evaluation error.
* Update CHANGELOG
  • Loading branch information
caike authored Sep 4, 2024
1 parent cb1f0e2 commit a8ceb09
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed

- Errors on tx submission and tx evaluation now return complete information from Ogmios.

## [v0.5.0](https://github.com/wowica/xogmios/releases/tag/v0.5.0) (2024-08-16)

### Added
Expand Down
4 changes: 2 additions & 2 deletions lib/xogmios/tx_submission/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ defmodule Xogmios.TxSubmission.Server do
{:ok, state}
end

defp handle_message(%{"error" => %{"message" => message}}, state) do
GenServer.reply(state.caller, {:error, message})
defp handle_message(%{"error" => error_info}, state) do
GenServer.reply(state.caller, {:error, error_info})
{:ok, state}
end

Expand Down
36 changes: 35 additions & 1 deletion test/support/tx_submission/test_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ defmodule TxSubmission.TestHandler do

@valid_tx_evaluation_cbor %{
"method" => "evaluateTransaction",
"params" => %{"transaction" => %{"cbor" => "valid-cbor-value"}}
"params" => %{"transaction" => %{"cbor" => "valid-cbor-value-evaluate"}}
}

@invalid_tx_evaluation_cbor %{
"method" => "evaluateTransaction",
"params" => %{"transaction" => %{"cbor" => "invalid-cbor-value-evaluate"}}
}

@impl true
Expand Down Expand Up @@ -91,6 +96,35 @@ defmodule TxSubmission.TestHandler do

{:reply, {:text, payload}, state}

{:ok, @invalid_tx_evaluation_cbor} ->
payload =
Jason.encode!(%{
"error" => %{
"code" => -32_602,
"data" => %{
"allegra" =>
"invalid or incomplete value of type 'Transaction': Size mismatch when decoding Object / Array. Expected 3, but found 4.",
"alonzo" =>
"invalid or incomplete value of type 'Transaction': expected list len or indef",
"babbage" =>
"invalid or incomplete value of type 'Transaction': Failed to decode AuxiliaryData",
"conway" =>
"invalid or incomplete value of type 'Transaction': Failed to decode AuxiliaryData",
"mary" =>
"invalid or incomplete value of type 'Transaction': Size mismatch when decoding Object / Array. Expected 3, but found 4.",
"shelley" =>
"invalid or incomplete value of type 'Transaction': Size mismatch when decoding Object / Array. Expected 3, but found 4."
},
"message" =>
"Invalid transaction; It looks like the given transaction wasn't well-formed. Note that I try to decode the transaction in every possible era and it was malformed in ALL eras. Yet, I can't pinpoint the exact issue for I do not know in which era / format you intended the transaction to be. The 'data' field, therefore, contains errors for each era."
},
"id" => nil,
"jsonrpc" => "2.0",
"method" => "evaluateTransaction"
})

{:reply, {:text, payload}, state}

result ->
Logger.error("Did not match #{inspect(result)}")
{:reply, {:text, payload}, state}
Expand Down
28 changes: 25 additions & 3 deletions test/tx_submission_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule Xogmios.TxSubmissionTest do
use ExUnit.Case

@ws_url TestServer.get_url()
@eras ["allegra", "alonzo", "babbage", "conway", "mary", "shelley"]

setup_all do
{:ok, _server} = TestServer.start(handler: TxSubmission.TestHandler)
Expand Down Expand Up @@ -38,10 +39,18 @@ defmodule Xogmios.TxSubmissionTest do
assert {:ok, %{"transaction" => %{"id" => _id}}} =
DummyClient.submit_tx(_cbor = "valid-cbor-value")

assert {:error, reason} =
assert {:error, info} =
DummyClient.submit_tx(_cbor = "invalid-cbor-value")

assert reason =~ "Invalid transaction"
assert info["code"] == -32_602
assert data = info["data"]

for era <- Map.keys(data) do
assert era in @eras
assert data[era] =~ "invalid or incomplete value of"
end

assert info["message"] =~ "Invalid transaction"
end

test "transaction evaluation" do
Expand All @@ -50,6 +59,19 @@ defmodule Xogmios.TxSubmissionTest do
Process.sleep(1_000)

assert {:ok, [%{"budget" => _budget, "validator" => _validator}]} =
DummyClient.evaluate_tx(_cbor = "valid-cbor-value")
DummyClient.evaluate_tx(_cbor = "valid-cbor-value-evaluate")

assert {:error, info} =
DummyClient.evaluate_tx(_cbor = "invalid-cbor-value-evaluate")

assert info["code"] == -32_602
assert data = info["data"]

for era <- Map.keys(data) do
assert era in @eras
assert data[era] =~ "invalid or incomplete value of"
end

assert info["message"] =~ "Invalid transaction"
end
end

0 comments on commit a8ceb09

Please sign in to comment.