Skip to content

Commit

Permalink
print query statement in error log
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Rychlewski authored and Greg Rychlewski committed Jun 5, 2022
1 parent a986aee commit 5c54bb9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
21 changes: 16 additions & 5 deletions lib/myxql/error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,22 @@ defmodule MyXQL.Error do
}

@impl true
def message(%{mysql: %{code: code, name: nil}, message: message}) do
"(#{code}) " <> message
def message(e) do
if map = e.mysql do
IO.iodata_to_binary([
[?(, Integer.to_string(map.code), ?)],
build_name(map),
e.message,
build_query(e.statement)
])
else
e.message
end
end

def message(%{mysql: %{code: code, name: name}, message: message}) do
"(#{code}) (#{name}) " <> message
end
defp build_name(%{name: nil}), do: [?\s]
defp build_name(%{name: name}), do: [?\s, ?(, Atom.to_string(name), ?), ?\s]

defp build_query(nil), do: []
defp build_query(query_statement), do: ["\n\n query: ", query_statement]
end
7 changes: 7 additions & 0 deletions test/myxql_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,13 @@ defmodule MyXQLTest do
assert Enum.to_list(columns["x"]) == [1, 2, 3]
assert Enum.to_list(columns["y"]) == ["a", "b", "c"]
end

test "query statement is printed in error", c do
{:error, e} = MyXQL.query(c.conn, "SELECT not_a_column FROM integers", [])
error_log = MyXQL.Error.message(e)
assert error_log =~ "query: SELECT not_a_column FROM integers"
assert error_log =~ "(1054) Unknown column"
end
end

describe ":prepare option" do
Expand Down

0 comments on commit 5c54bb9

Please sign in to comment.