Skip to content

Commit

Permalink
Fix error propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
cichacz committed Dec 7, 2023
1 parent b574783 commit 5011205
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 16 deletions.
4 changes: 2 additions & 2 deletions example_app/lib/example_app.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]}

_ ->
Expand Down
2 changes: 1 addition & 1 deletion example_app/lib/example_app/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/neo4ex/bolt_protocol.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)}"

Expand Down Expand Up @@ -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} ->
Expand Down
19 changes: 8 additions & 11 deletions test/neo4ex/bolt_protocol_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand All @@ -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

0 comments on commit 5011205

Please sign in to comment.