Skip to main content

src/molt@error.erl

-module(molt@error).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/molt/error.gleam").
-export([describe_error/1, not_found/1, not_found_path/1, not_found_path2/2, not_found_path3/3]).
-export_type([molt_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 molt_error() :: {parse_error, binary(), integer()} |
    invalid_source_encoding |
    {invalid_path, binary()} |
    {value_parse_error, gleam@option:option(binary()), binary()} |
    invalid_document |
    {not_found, binary(), binary()} |
    {already_exists, binary(), molt@types:index_entry()} |
    {index_out_of_range, binary(), integer(), integer()} |
    {value_index_out_of_range, integer(), integer()} |
    {value_key_not_found, binary()} |
    {type_mismatch, gleam@option:option(binary()), binary(), binary()} |
    {invalid_toml_value, binary(), binary()} |
    {invalid_operation, binary(), gleam@option:option(binary())} |
    {update_error, binary()}.

-file("src/molt/error.gleam", 62).
?DOC(" Describe an error as a human-readable string.\n").
-spec describe_error(molt_error()) -> binary().
describe_error(Error) ->
    case Error of
        {parse_error, Message, Offset} ->
            <<<<<<"Parsing failed at offset "/utf8,
                        (erlang:integer_to_binary(Offset))/binary>>/binary,
                    ": "/utf8>>/binary,
                Message/binary>>;

        invalid_source_encoding ->
            <<"The raw source provided for parsing is not valid UTF-8 data"/utf8>>;

        {invalid_path, Message@1} ->
            <<"Invalid path: "/utf8, Message@1/binary>>;

        {value_parse_error, {some, Expected}, Text} ->
            <<<<<<"Error parsing value for type "/utf8, Expected/binary>>/binary,
                    " but got text: "/utf8>>/binary,
                Text/binary>>;

        {value_parse_error, none, Text@1} ->
            <<"Error parsing value for any valid TOML type from text: "/utf8,
                Text@1/binary>>;

        invalid_document ->
            <<"The operation is not allowed because the document has validation errors"/utf8>>;

        {not_found, Path, At} when Path =/= At ->
            <<<<<<<<"Nothing found at path "/utf8, Path/binary>>/binary,
                        " (resolution stopped at "/utf8>>/binary,
                    At/binary>>/binary,
                ")."/utf8>>;

        {not_found, Path@1, _} ->
            <<<<"Nothing found at path "/utf8, Path@1/binary>>/binary,
                "."/utf8>>;

        {already_exists, Path@2, Current} ->
            <<<<<<<<"An item with type "/utf8,
                            (molt@internal@utils:index_entry_to_string(Current))/binary>>/binary,
                        " already exists at path "/utf8>>/binary,
                    Path@2/binary>>/binary,
                "."/utf8>>;

        {index_out_of_range, Path@3, Index, Length} ->
            <<<<<<<<<<"Index "/utf8, (erlang:integer_to_binary(Index))/binary>>/binary,
                            " out of range for array of size "/utf8>>/binary,
                        (erlang:integer_to_binary(Length))/binary>>/binary,
                    " at path "/utf8>>/binary,
                Path@3/binary>>;

        {value_index_out_of_range, Index@1, Length@1} ->
            <<<<<<"Index "/utf8, (erlang:integer_to_binary(Index@1))/binary>>/binary,
                    " out of range for value array of size "/utf8>>/binary,
                (erlang:integer_to_binary(Length@1))/binary>>;

        {value_key_not_found, Key} ->
            <<<<"Key \""/utf8, Key/binary>>/binary,
                "\" not found in value table"/utf8>>;

        {type_mismatch, none, Expected@1, Got} ->
            <<<<<<"Expected type "/utf8, Expected@1/binary>>/binary,
                    " but got type "/utf8>>/binary,
                Got/binary>>;

        {type_mismatch, {some, Path@4}, Expected@2, Got@1} ->
            <<<<<<<<<<"Expected type "/utf8, Expected@2/binary>>/binary,
                            " at path "/utf8>>/binary,
                        Path@4/binary>>/binary,
                    ", but got type "/utf8>>/binary,
                Got@1/binary>>;

        {invalid_toml_value, Path@5, Text@2} ->
            <<<<<<"Invalid TOML value \""/utf8, Text@2/binary>>/binary,
                    "\" at path "/utf8>>/binary,
                Path@5/binary>>;

        {invalid_operation, Operation, none} ->
            <<<<"Operation "/utf8, Operation/binary>>/binary,
                " is not valid."/utf8>>;

        {invalid_operation, Operation@1, {some, Reason}} ->
            <<<<<<<<"Operation "/utf8, Operation@1/binary>>/binary,
                        " is not valid: "/utf8>>/binary,
                    Reason/binary>>/binary,
                "."/utf8>>;

        {update_error, Message@2} ->
            <<"Update error: "/utf8, Message@2/binary>>
    end.

-file("src/molt/error.gleam", 121).
?DOC(false).
-spec not_found(binary()) -> molt_error().
not_found(Str) ->
    {not_found, Str, Str}.

-file("src/molt/error.gleam", 126).
?DOC(false).
-spec not_found_path(list(molt@types:path_segment())) -> molt_error().
not_found_path(Segments) ->
    _pipe = molt@internal@utils:path_to_string(Segments),
    not_found(_pipe).

-file("src/molt/error.gleam", 132).
?DOC(false).
-spec not_found_path2(
    list(molt@types:path_segment()),
    list(molt@types:path_segment())
) -> molt_error().
not_found_path2(Remaining, Visited) ->
    Visited@1 = lists:reverse(Visited),
    _pipe = molt@internal@utils:path_to_string(Remaining),
    {not_found, _pipe, molt@internal@utils:path_to_string(Visited@1)}.

-file("src/molt/error.gleam", 143).
?DOC(false).
-spec not_found_path3(
    list(molt@types:path_segment()),
    list(molt@types:path_segment()),
    list(molt@types:path_segment())
) -> molt_error().
not_found_path3(Segments, Rest, Visited) ->
    Visited@1 = lists:reverse(Visited),
    _pipe = lists:append([Visited@1, Segments, Rest]),
    _pipe@1 = molt@internal@utils:path_to_string(_pipe),
    {not_found, _pipe@1, molt@internal@utils:path_to_string(Visited@1)}.