From 5011205f604c0336cc5e4606a8e47e8a3f7eddb0 Mon Sep 17 00:00:00 2001 From: Michal Sewczyk Date: Thu, 7 Dec 2023 12:37:14 +0100 Subject: [PATCH] Fix error propagation --- example_app/lib/example_app.ex | 4 ++-- example_app/lib/example_app/application.ex | 2 +- lib/neo4ex/bolt_protocol.ex | 4 ++-- test/neo4ex/bolt_protocol_test.exs | 19 ++++++++----------- 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/example_app/lib/example_app.ex b/example_app/lib/example_app.ex index 6e915b7..2ef3b2c 100644 --- a/example_app/lib/example_app.ex +++ b/example_app/lib/example_app.ex @@ -22,7 +22,7 @@ defmodule ExampleApp do results |> Enum.reject(fn msg -> match?(%Success{}, msg) end) |> Enum.map(fn [%Node{properties: properties}] -> - properties = Map.new(properties, fn {k, v} -> {String.to_existing_atom(k), v} end) + properties = Map.new(properties, fn {k, v} -> {String.to_atom(k), v} end) struct(Customer, properties) end) end @@ -38,7 +38,7 @@ defmodule ExampleApp do |> Connector.stream(fn msg, acc -> case msg do [%Node{properties: properties}] -> - properties = Map.new(properties, fn {k, v} -> {String.to_existing_atom(k), v} end) + properties = Map.new(properties, fn {k, v} -> {String.to_atom(k), v} end) {:cont, [struct(Customer, properties) | acc]} _ -> diff --git a/example_app/lib/example_app/application.ex b/example_app/lib/example_app/application.ex index b669162..396b426 100644 --- a/example_app/lib/example_app/application.ex +++ b/example_app/lib/example_app/application.ex @@ -9,7 +9,7 @@ defmodule ExampleApp.Application do def start(_type, _args) do children = [ ExampleApp.Connector, - {Bolt.Sips, Application.get_env(:bolt_sips, Bolt)}, + {Bolt.Sips, Application.get_env(:bolt_sips, Bolt)} ] # See https://hexdocs.pm/elixir/Supervisor.html diff --git a/lib/neo4ex/bolt_protocol.ex b/lib/neo4ex/bolt_protocol.ex index 9ee5b12..9b32130 100644 --- a/lib/neo4ex/bolt_protocol.ex +++ b/lib/neo4ex/bolt_protocol.ex @@ -26,7 +26,7 @@ defmodule Neo4ex.BoltProtocol do Goodbye } - alias Neo4ex.BoltProtocol.Structure.Message.Summary.{Success, Ignored, Failure} + alias Neo4ex.BoltProtocol.Structure.Message.Summary.{Success, Failure} @user_agent "Neo4ex/#{Application.spec(:neo4ex, :vsn)}" @@ -142,7 +142,7 @@ defmodule Neo4ex.BoltProtocol do case handle_fetch(q, cursor, opts, sckt) do {:cont, data, sckt} -> {[data], {:ok, q, cursor, sckt}} {:halt, success, sckt} -> {[success], {:halt, q, cursor, sckt}} - other -> other + {:error, exception, _} -> raise exception end {:halt, q, cursor, sckt} -> diff --git a/test/neo4ex/bolt_protocol_test.exs b/test/neo4ex/bolt_protocol_test.exs index b4fbe9d..d8ac1ee 100644 --- a/test/neo4ex/bolt_protocol_test.exs +++ b/test/neo4ex/bolt_protocol_test.exs @@ -116,7 +116,7 @@ defmodule Neo4ex.BoltProtocolTest do BoltProtocol.handle_execute(query, %{}, [], socket) end - test "returns error if stream gets interrupted", %{socket: socket, query: query} do + test "raises error if stream gets interrupted", %{socket: socket, query: query} do message = %Record{data: ["message"]} encoded_message = Encoder.encode(message, "4.0.0") success_message = %Success{metadata: %{"t_first" => 1}} @@ -137,27 +137,24 @@ defmodule Neo4ex.BoltProtocolTest do |> expect(:send, fn _, _ -> :ok end) |> expect_message(encoded_success_message) - assert {:error, DBConnection.ConnectionError.exception(inspect(:closed)), socket} == - BoltProtocol.handle_execute(query, %{}, [], socket) + assert_raise DBConnection.ConnectionError, fn -> + BoltProtocol.handle_execute(query, %{}, [], socket) + end end - test "returns error if stream returns failure", %{socket: socket, query: query} do + test "throws error if stream returns failure", %{socket: socket, query: query} do message = %Failure{metadata: %{"message" => "failure"}} encoded_failure_message = Encoder.encode(message, "4.0.0") - success_message = %Success{metadata: %{"t_first" => 1}} - encoded_success_message = Encoder.encode(success_message, "4.0.0") SocketMock # query |> expect(:send, fn _, _ -> :ok end) - # pull results - |> expect(:send, fn _, _ -> :ok end) - # summary message - |> expect_message(encoded_success_message) # summary message |> expect_message(encoded_failure_message) - assert {:error, query, [message], socket} == BoltProtocol.handle_execute(query, %{}, [], socket) + assert_raise DBConnection.ConnectionError, fn -> + BoltProtocol.handle_execute(query, %{}, [], socket) + end end end end