-module(glome@core@error). -compile(no_auto_import). -export([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 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, GQU} | {error, list(gleam@dynamic:decode_error())}) -> {ok, GQU} | {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, GQZ} | {error, gleam@dynamic:decode_error()}) -> {ok, GQZ} | {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, GRE} | {error, nerf@websocket:connect_error()}) -> {ok, GRE} | {error, glome_error()}. map_connection_error(Error) -> gleam@result:map_error(Error, fun(Conn_error) -> case Conn_error of {connection_refused, Status, Headers} -> _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, Reason} -> _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).