Current section
Files
Jump to
Current section
Files
lib/beanstream/error.ex
defmodule Beanstream.Error do
defstruct [
status: nil,
status_code: nil,
code: 0,
category: 0,
message: nil,
details: nil,
reference: nil,
]
@type t :: %Beanstream.Error{
status: atom,
status_code: pos_integer,
code: integer,
category: integer,
message: String.t,
details: list(map),
reference: String.t,
}
@spec new(map) :: Beanstream.Error
def new(resp) do
status = [
status: status(resp.status_code),
status_code: resp.status_code
]
result = if is_binary(resp.body) do # failed to parse JSON
status
else
resp.body
|> Map.take(~w(code category message details reference))
|> Enum.map(fn({k, v}) -> {String.to_atom(k), v} end)
|> Keyword.merge(status)
end
{:error, struct(Beanstream.Error, result)}
end
# translate to the same atoms as Plug.Conn.Status
defp status(code) do
case code do
400 -> :bad_request
401 -> :unauthorized
402 -> :payment_required
403 -> :forbidden
404 -> :not_found
405 -> :method_not_allowed
500 -> :internal_server_error
_ -> nil
end
end
end