/
exceptions.ex
83 lines (61 loc) 路 2.34 KB
/
exceptions.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
defmodule Redix.Error do
@moduledoc """
Error returned by Redis.
This exception represents semantic errors returned by Redis: for example,
non-existing commands or operations on keys with the wrong type (`INCR
not_an_integer`).
"""
defexception [:message]
@typedoc """
The type for this exception struct.
"""
@type t() :: %__MODULE__{message: binary()}
end
defmodule Redix.ConnectionError do
@moduledoc """
Error in the connection to Redis.
This exception represents errors in the connection to Redis: for example,
request timeouts, disconnections, and similar.
## Exception fields
See `t:t/0`.
## Error reasons
The `:reason` field can assume a few Redix-specific values:
* `:closed`: when the connection to Redis is closed (and Redix is
reconnecting) and the user attempts to talk to Redis
* `:disconnected`: when the connection drops while a request to Redis is in
flight.
* `:timeout`: when Redis doesn't reply to the request in time.
"""
@typedoc """
The type for this exception struct.
This exception has the following public fields:
* `:reason` - the error reason. It can be one of the Redix-specific
reasons described in the "Error reasons" section below, or any error
reason returned by functions in the `:gen_tcp` module (see the
[`:inet.posix/0`](http://www.erlang.org/doc/man/inet.html#type-posix) type) or
`:ssl` module.
"""
@type t() :: %__MODULE__{reason: atom}
defexception [:reason]
@impl true
def message(%__MODULE__{reason: reason}) do
format_reason(reason)
end
# :inet.format_error/1 doesn't format closed messages.
defp format_reason(:tcp_closed), do: "TCP connection closed"
defp format_reason(:ssl_closed), do: "SSL connection closed"
# Manually returned by us when the connection is closed and someone tries to
# send a command to Redis.
defp format_reason(:closed), do: "the connection to Redis is closed"
if System.otp_release() >= "26" do
defp format_reason(reason), do: reason |> :inet.format_error() |> List.to_string()
else
defp format_reason(reason) do
case :inet.format_error(reason) do
~c"unknown POSIX error" = message when is_atom(reason) -> "#{message}: #{reason}"
~c"unknown POSIX error" -> inspect(reason)
message -> List.to_string(message)
end
end
end
end