-
Notifications
You must be signed in to change notification settings - Fork 268
/
error.ex
41 lines (33 loc) · 934 Bytes
/
error.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
defmodule Postgrex.Error do
defexception [:message, :postgres, :connection_id]
@type t :: %Postgrex.Error{}
@metadata [:table, :column, :constraint]
def message(e) do
if kw = e.postgres do
"#{kw[:severity]} #{kw[:pg_code]} (#{kw[:code]}): #{kw[:message]}"
<> build_metadata(kw)
<> build_detail(kw)
else
e.message
end
end
defp build_metadata(kw) do
metadata = for k <- @metadata, v = kw[k], do: "\n #{k}: #{v}"
case metadata do
[] -> ""
_ -> "\n" <> Enum.join(metadata)
end
end
defp build_detail(kw) do
if v = kw[:detail], do: "\n\n" <> v, else: ""
end
def exception([postgres: fields]) do
fields = Enum.into(fields, %{})
|> Map.put(:pg_code, fields[:code])
|> Map.update!(:code, &Postgrex.ErrorCode.code_to_name/1)
%Postgrex.Error{postgres: fields}
end
def exception(arg) do
super(arg)
end
end