Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add more info to errors #295

Merged
merged 3 commits into from
Jun 6, 2024
Merged

Conversation

thalesmg
Copy link
Contributor

@thalesmg thalesmg commented Jun 4, 2024

Minor change to replace function_clause with a bit more info to track down the error.

Before the change:

[error] input-config:
  '$hcVal' =>
      [#{'$hcTyp' => object,
         '$hcVal' =>
             #{<<"key">> =>
                   #{'$hcTyp' => string,'$hcVal' => <<"ts">>,
                     '$hcMeta' =>
                         #{line => 45,
                           filename =>
                               "/home/thales/dev/emqx/emqx/_build/emqx-enterprise/rel/emqx/data/configs/cluster.hocon"}},
               <<"value">> =>
                   #{'$hcTyp' => string,
                     '$hcVal' => <<"concat([topic, '/', a.b])">>,
                     '$hcMeta' =>
                         #{line => 46,
                           filename =>
                               "/home/thales/dev/emqx/emqx/_build/emqx-enterprise/rel/emqx/data/configs/cluster.hocon"}}},
         '$hcMeta' =>
             #{line => 46,
               filename =>
                   "/home/thales/dev/emqx/emqx/_build/emqx-enterprise/rel/emqx/data/configs/cluster.hocon"}}],
  '$hcMeta' =>
      #{line => 43,
        filename =>
            "/home/thales/dev/emqx/emqx/_build/emqx-enterprise/rel/emqx/data/configs/cluster.hocon"}}
  path => "message_transformation.transformations.1",
  exception => function_clause,field => <<"transform">>} <----------------------------------------

escript: exception error: #{reason => failed_to_check_field,
                   path => "message_transformation.transformations.1",
                   exception => function_clause,field => <<"transform">>}
  in function  emqx_message_transformation_schema:fields/1
     called as emqx_message_transformation_schema:fields(transform_kv222)
  in call from hocon_schema:fields_and_meta/2 (hocon_schema.erl, line 278)
  in call from hocon_schema:fields/2 (hocon_schema.erl, line 271)
  in call from hocon_tconf:map_field/4 (hocon_tconf.erl, line 584)
  in call from hocon_tconf:map_one_field_non_hidden/4 (hocon_tconf.erl, line 492)
  in call from hocon_tconf:do_map_array/5 (hocon_tconf.erl, line 801)
  in call from hocon_tconf:'-map_field/4-fun-4-'/4 (hocon_tconf.erl, line 622)
  in call from hocon_tconf:map_one_field_non_hidden/4 (hocon_tconf.erl, line 492)

After the change:

...
  path => "message_transformation.transformations.1",
  exception =>
      #{reason => function_clause, <-----------------------------------------------------
        stacktrace =>
            [{emqx_message_transformation_schema,fields,
                 [operation_wrong_ref],
                 [{file,"emqx_message_transformation_schema.erl"},{line,39}]},
             {hocon_schema,fields_and_meta,2,
                 [{file,"hocon_schema.erl"},{line,278}]},
             {hocon_schema,fields,2,[{file,"hocon_schema.erl"},{line,271}]},
             {hocon_tconf,map_field,4,[{file,"hocon_tconf.erl"},{line,581}]},
             {hocon_tconf,map_one_field_non_hidden,4,
                 [{file,"hocon_tconf.erl"},{line,492}]},
             {hocon_tconf,do_map_array,5,
                 [{file,"hocon_tconf.erl"},{line,798}]},
             {hocon_tconf,'-map_field/4-fun-4-',4,
                 [{file,"hocon_tconf.erl"},{line,619}]},
             {hocon_tconf,map_one_field_non_hidden,4,
                 [{file,"hocon_tconf.erl"},{line,492}]}],
        kind => error},
  field => <<"operations">>}

escript: exception throw: #{reason => failed_to_check_field,
                   path => "message_transformation.transformations.1",
                   exception =>
                       #{reason => function_clause,
                         stacktrace =>
                             [{emqx_message_transformation_schema,fields,
                                  [operation_wrong_ref],
                                  [{file,
                                       "emqx_message_transformation_schema.erl"},
                                   {line,39}]},
                              {hocon_schema,fields_and_meta,2,
                                  [{file,"hocon_schema.erl"},{line,278}]},
                              {hocon_schema,fields,2,
                                  [{file,"hocon_schema.erl"},{line,271}]},
                              {hocon_tconf,map_field,4,
                                  [{file,"hocon_tconf.erl"},{line,581}]},
                              {hocon_tconf,map_one_field_non_hidden,4,
                                  [{file,"hocon_tconf.erl"},{line,492}]},
                              {hocon_tconf,do_map_array,5,
                                  [{file,"hocon_tconf.erl"},{line,798}]},
                              {hocon_tconf,'-map_field/4-fun-4-',4,
                                  [{file,"hocon_tconf.erl"},{line,619}]},
                              {hocon_tconf,map_one_field_non_hidden,4,
                                  [{file,"hocon_tconf.erl"},{line,492}]}],
                         kind => error},
                   field => <<"operations">>}

Fields when is_list(Fields) ->
maybe_add_desc(Mod, Name, #{fields => Fields});
Fields ->
ensure_struct_meta(Fields)
catch
error:function_clause ->
throw({missing_struct_def, Mod, Name})
Copy link
Member

@zmstone zmstone Jun 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catch all kinds of errors

erlang:raise with error reason enriched and with original stackstace.
function_clause may happen in deeper stack under Mod:fields

Before the change:

```
[error] input-config:
  '$hcVal' =>
      [#{'$hcTyp' => object,
         '$hcVal' =>
             #{<<"key">> =>
                   #{'$hcTyp' => string,'$hcVal' => <<"ts">>,
                     '$hcMeta' =>
                         #{line => 45,
                           filename =>
                               "/home/thales/dev/emqx/emqx/_build/emqx-enterprise/rel/emqx/data/configs/cluster.hocon"}},
               <<"value">> =>
                   #{'$hcTyp' => string,
                     '$hcVal' => <<"concat([topic, '/', a.b])">>,
                     '$hcMeta' =>
                         #{line => 46,
                           filename =>
                               "/home/thales/dev/emqx/emqx/_build/emqx-enterprise/rel/emqx/data/configs/cluster.hocon"}}},
         '$hcMeta' =>
             #{line => 46,
               filename =>
                   "/home/thales/dev/emqx/emqx/_build/emqx-enterprise/rel/emqx/data/configs/cluster.hocon"}}],
  '$hcMeta' =>
      #{line => 43,
        filename =>
            "/home/thales/dev/emqx/emqx/_build/emqx-enterprise/rel/emqx/data/configs/cluster.hocon"}}
  path => "message_transformation.transformations.1",
  exception => function_clause,field => <<"transform">>} <----------------------------------------

escript: exception error: #{reason => failed_to_check_field,
                   path => "message_transformation.transformations.1",
                   exception => function_clause,field => <<"transform">>}
  in function  emqx_message_transformation_schema:fields/1
     called as emqx_message_transformation_schema:fields(transform_kv222)
  in call from hocon_schema:fields_and_meta/2 (hocon_schema.erl, line 278)
  in call from hocon_schema:fields/2 (hocon_schema.erl, line 271)
  in call from hocon_tconf:map_field/4 (hocon_tconf.erl, line 584)
  in call from hocon_tconf:map_one_field_non_hidden/4 (hocon_tconf.erl, line 492)
  in call from hocon_tconf:do_map_array/5 (hocon_tconf.erl, line 801)
  in call from hocon_tconf:'-map_field/4-fun-4-'/4 (hocon_tconf.erl, line 622)
  in call from hocon_tconf:map_one_field_non_hidden/4 (hocon_tconf.erl, line 492)

```

After the change:

```
[error] input-config:
  '$hcVal' =>
      [#{'$hcTyp' => object,
         '$hcVal' =>
             #{<<"key">> =>
                   #{'$hcTyp' => string,'$hcVal' => <<"ts">>,
                     '$hcMeta' =>
                         #{line => 45,
                           filename =>
                               "/home/thales/dev/emqx/emqx/_build/emqx-enterprise/rel/emqx/data/configs/cluster.hocon"}},
               <<"value">> =>
                   #{'$hcTyp' => string,
                     '$hcVal' => <<"concat([topic, '/', a.b])">>,
                     '$hcMeta' =>
                         #{line => 46,
                           filename =>
                               "/home/thales/dev/emqx/emqx/_build/emqx-enterprise/rel/emqx/data/configs/cluster.hocon"}}},
         '$hcMeta' =>
             #{line => 46,
               filename =>
                   "/home/thales/dev/emqx/emqx/_build/emqx-enterprise/rel/emqx/data/configs/cluster.hocon"}},
       #{'$hcTyp' => object,
         '$hcVal' =>
             #{<<"key">> =>
                   #{'$hcTyp' => string,'$hcVal' => <<"bah">>,
                     '$hcMeta' =>
                         #{line => 49,
                           filename =>
                               "/home/thales/dev/emqx/emqx/_build/emqx-enterprise/rel/emqx/data/configs/cluster.hocon"}},
               <<"value">> =>
                   #{'$hcTyp' => string,
                     '$hcVal' => <<"schema_decode('avro', 'bah')">>,
                     '$hcMeta' =>
                         #{line => 50,
                           filename =>
                               "/home/thales/dev/emqx/emqx/_build/emqx-enterprise/rel/emqx/data/configs/cluster.hocon"}}},
         '$hcMeta' =>
             #{line => 50,
               filename =>
                   "/home/thales/dev/emqx/emqx/_build/emqx-enterprise/rel/emqx/data/configs/cluster.hocon"}},
       #{'$hcTyp' => object,
         '$hcVal' =>
             #{<<"key">> =>
                   #{'$hcTyp' => string,'$hcVal' => <<"bah">>,
                     '$hcMeta' =>
                         #{line => 53,
                           filename =>
                               "/home/thales/dev/emqx/emqx/_build/emqx-enterprise/rel/emqx/data/configs/cluster.hocon"}},
               <<"value">> =>
                   #{'$hcTyp' => string,
                     '$hcVal' => <<"schema_encode('avro', 'bah')">>,
                     '$hcMeta' =>
                         #{line => 54,
                           filename =>
                               "/home/thales/dev/emqx/emqx/_build/emqx-enterprise/rel/emqx/data/configs/cluster.hocon"}}},
         '$hcMeta' =>
             #{line => 54,
               filename =>
                   "/home/thales/dev/emqx/emqx/_build/emqx-enterprise/rel/emqx/data/configs/cluster.hocon"}}],
  '$hcMeta' =>
      #{line => 43,
        filename =>
            "/home/thales/dev/emqx/emqx/_build/emqx-enterprise/rel/emqx/data/configs/cluster.hocon"}}
  path => "message_transformation.transformations.1",
  exception =>
      #{reason => function_clause, <-----------------------------------------------------
        stacktrace =>
            [{emqx_message_transformation_schema,fields,
                 [operation_wrong_ref],
                 [{file,"emqx_message_transformation_schema.erl"},{line,39}]},
             {hocon_schema,fields_and_meta,2,
                 [{file,"hocon_schema.erl"},{line,278}]},
             {hocon_schema,fields,2,[{file,"hocon_schema.erl"},{line,271}]},
             {hocon_tconf,map_field,4,[{file,"hocon_tconf.erl"},{line,581}]},
             {hocon_tconf,map_one_field_non_hidden,4,
                 [{file,"hocon_tconf.erl"},{line,492}]},
             {hocon_tconf,do_map_array,5,
                 [{file,"hocon_tconf.erl"},{line,798}]},
             {hocon_tconf,'-map_field/4-fun-4-',4,
                 [{file,"hocon_tconf.erl"},{line,619}]},
             {hocon_tconf,map_one_field_non_hidden,4,
                 [{file,"hocon_tconf.erl"},{line,492}]}],
        kind => error},
  field => <<"operations">>}

escript: exception throw: #{reason => failed_to_check_field,
                   path => "message_transformation.transformations.1",
                   exception =>
                       #{reason => function_clause,
                         stacktrace =>
                             [{emqx_message_transformation_schema,fields,
                                  [operation_wrong_ref],
                                  [{file,
                                       "emqx_message_transformation_schema.erl"},
                                   {line,39}]},
                              {hocon_schema,fields_and_meta,2,
                                  [{file,"hocon_schema.erl"},{line,278}]},
                              {hocon_schema,fields,2,
                                  [{file,"hocon_schema.erl"},{line,271}]},
                              {hocon_tconf,map_field,4,
                                  [{file,"hocon_tconf.erl"},{line,581}]},
                              {hocon_tconf,map_one_field_non_hidden,4,
                                  [{file,"hocon_tconf.erl"},{line,492}]},
                              {hocon_tconf,do_map_array,5,
                                  [{file,"hocon_tconf.erl"},{line,798}]},
                              {hocon_tconf,'-map_field/4-fun-4-',4,
                                  [{file,"hocon_tconf.erl"},{line,619}]},
                              {hocon_tconf,map_one_field_non_hidden,4,
                                  [{file,"hocon_tconf.erl"},{line,492}]}],
                         kind => error},
                   field => <<"operations">>}
```
@thalesmg thalesmg force-pushed the better-error-msg1 branch from 850c7e9 to 68432a3 Compare June 5, 2024 12:25
src/hocon_schema.erl Outdated Show resolved Hide resolved
src/hocon_schema.erl Outdated Show resolved Hide resolved
thalesmg and others added 2 commits June 5, 2024 17:30
Co-authored-by: zmstone <zmstone@gmail.com>
@thalesmg thalesmg merged commit 76171b8 into emqx:master Jun 6, 2024
3 checks passed
@thalesmg thalesmg deleted the better-error-msg1 branch June 6, 2024 12:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants