Current section
Files
Jump to
Current section
Files
src/envie@error.erl
-module(envie@error).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/envie/error.gleam").
-export([format/1, format_all/1, format_parse_errors/1, format_load_error/1]).
-export_type([error/0, load_error/0, parse_error/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type error() :: {missing, binary()} |
{invalid_value, binary(), binary(), binary()} |
{decode_error, binary(), binary()}.
-type load_error() :: {file_not_found, binary()} |
{read_error, binary(), binary()} |
{parse_error, list(parse_error())}.
-type parse_error() :: {invalid_syntax, integer(), binary(), binary()} |
{invalid_escape, integer(), binary()}.
-file("src/envie/error.gleam", 35).
?DOC(" Format a single `Error` into a human-readable message.\n").
-spec format(error()) -> binary().
format(Error) ->
case Error of
{missing, Name} ->
<<Name/binary, ": missing required environment variable"/utf8>>;
{invalid_value, Name@1, Value, Reason} ->
Short = case string:length(Value) > 50 of
true ->
<<(gleam@string:slice(Value, 0, 47))/binary, "..."/utf8>>;
false ->
Value
end,
<<<<<<<<Name@1/binary, ": invalid value \""/utf8>>/binary,
Short/binary>>/binary,
"\" — "/utf8>>/binary,
Reason/binary>>;
{decode_error, Name@2, Details} ->
<<<<Name@2/binary, ": decode error — "/utf8>>/binary,
Details/binary>>
end.
-file("src/envie/error.gleam", 50).
?DOC(" Format a list of `Error` values, one per line.\n").
-spec format_all(list(error())) -> binary().
format_all(Errors) ->
_pipe = Errors,
_pipe@1 = gleam@list:map(_pipe, fun format/1),
gleam@string:join(_pipe@1, <<"\n"/utf8>>).
-file("src/envie/error.gleam", 70).
-spec format_parse_error(parse_error()) -> binary().
format_parse_error(Error) ->
case Error of
{invalid_syntax, Line, Content, Reason} ->
Short = case string:length(Content) > 40 of
true ->
<<(gleam@string:slice(Content, 0, 37))/binary, "..."/utf8>>;
false ->
Content
end,
<<<<<<<<<<<<"Line "/utf8, (erlang:integer_to_binary(Line))/binary>>/binary,
": "/utf8>>/binary,
Reason/binary>>/binary,
" (\""/utf8>>/binary,
Short/binary>>/binary,
"\")"/utf8>>;
{invalid_escape, Line@1, Sequence} ->
<<<<<<"Line "/utf8, (erlang:integer_to_binary(Line@1))/binary>>/binary,
": invalid escape sequence: "/utf8>>/binary,
Sequence/binary>>
end.
-file("src/envie/error.gleam", 64).
?DOC(" Format a list of `ParseError` values, one per line.\n").
-spec format_parse_errors(list(parse_error())) -> binary().
format_parse_errors(Errors) ->
_pipe = Errors,
_pipe@1 = gleam@list:map(_pipe, fun format_parse_error/1),
gleam@string:join(_pipe@1, <<"\n"/utf8>>).
-file("src/envie/error.gleam", 55).
?DOC(" Format a `LoadError` into a human-readable message.\n").
-spec format_load_error(load_error()) -> binary().
format_load_error(Error) ->
case Error of
{file_not_found, Path} ->
<<"File not found: "/utf8, Path/binary>>;
{read_error, Path@1, Reason} ->
<<<<<<"Failed to read "/utf8, Path@1/binary>>/binary, ": "/utf8>>/binary,
Reason/binary>>;
{parse_error, Errors} ->
<<"Parse errors:\n"/utf8, (format_parse_errors(Errors))/binary>>
end.