Current section
Files
Jump to
Current section
Files
src/glome@core@error.erl
-module(glome@core@error).
-compile(no_auto_import).
-export([json_decode_to_dynamic_decode_error/1, stringify_decode_error/1, json_decode_error/1, map_decode_errors/1, map_decode_error/1, map_connection_error/1]).
-export_type([glome_error/0]).
-type glome_error() :: {websocket_connection_error, binary()} |
{json_decode_error, binary()} |
{authentication_error, binary()} |
{deserialization_error, glome_error(), binary()} |
{entity_id_format_error, binary()} |
{call_service_error, binary()} |
not_allowed_http_method |
{bad_request, binary()} |
{not_found, binary()} |
loop_nil.
-spec json_decode_to_dynamic_decode_error(gleam@json:decode_error()) -> list(gleam@dynamic:decode_error()).
json_decode_to_dynamic_decode_error(Json_error) ->
case Json_error of
unexpected_end_of_input ->
[{decode_error, <<"more input"/utf8>>, <<"end of input"/utf8>>, []}];
{unexpected_byte, Byte, Position} ->
[{decode_error,
<<"other byte"/utf8>>,
Byte,
[gleam@int:to_string(Position)]}];
{unexpected_sequence, Byte@1, Position@1} ->
[{decode_error,
<<"other byte"/utf8>>,
Byte@1,
[gleam@int:to_string(Position@1)]}];
{unexpected_format, List} ->
List
end.
-spec stringify_decode_error(gleam@dynamic:decode_error()) -> binary().
stringify_decode_error(Error) ->
gleam@string:concat(
[<<"expected: "/utf8>>,
erlang:element(2, Error),
<<" got instead: "/utf8>>,
erlang:element(3, Error),
<<" at path: "/utf8>>,
gleam@string:join(erlang:element(4, Error), <<"."/utf8>>)]
).
-spec json_decode_error(binary()) -> glome_error().
json_decode_error(Reason) ->
{json_decode_error,
gleam@string:concat(
[<<"could not decode json due to: [ "/utf8>>, Reason, <<" ]"/utf8>>]
)}.
-spec map_decode_errors({ok, GSE} | {error, list(gleam@dynamic:decode_error())}) -> {ok,
GSE} |
{error, glome_error()}.
map_decode_errors(Errors) ->
gleam@result:map_error(
Errors,
fun(Decode_errors) ->
_pipe = gleam@list:map(Decode_errors, fun stringify_decode_error/1),
_pipe@1 = gleam@string:join(_pipe, <<"ln"/utf8>>),
json_decode_error(_pipe@1)
end
).
-spec map_decode_error({ok, GSJ} | {error, gleam@dynamic:decode_error()}) -> {ok,
GSJ} |
{error, glome_error()}.
map_decode_error(Error) ->
gleam@result:map_error(
Error,
fun(Decode_error) ->
_pipe = stringify_decode_error(Decode_error),
json_decode_error(_pipe)
end
).
-spec map_connection_error({ok, GSO} | {error, nerf@websocket:connect_error()}) -> {ok,
GSO} |
{error, glome_error()}.
map_connection_error(Error) ->
gleam@result:map_error(Error, fun(Conn_error) -> case Conn_error of
{connection_refused, Status, _@1} ->
_pipe = gleam@string:concat(
[<<"connection from homeassistant refused,\n"/utf8>>,
<<"server responded with status [ "/utf8>>,
gleam@int:to_string(Status),
<<" ]"/utf8>>]
),
{websocket_connection_error, _pipe};
{connection_failed, _@2} ->
_pipe@1 = gleam@string:concat(
[<<"connection to homeassistant failed,\n"/utf8>>,
<<"due to: [ "/utf8>>,
<<"unknown connection error"/utf8>>,
<<" ]"/utf8>>]
),
{websocket_connection_error, _pipe@1}
end end).