[erlang-questions] better error message when using term_to_binary with [safe] ?
Sun Mar 13 21:21:31 CET 2011
On Mon, Mar 14, 2011 at 12:43 AM, Pablo Platt <> wrote:
> When using binary_to_term with the [safe] option, with a non existing atom it
> just throws an error
> but doesn't tell you what's wrong.
> Is it possible that the error message will tell me what atom was missing?
> 1> binary_to_term(<<131,100,0,6,109,121,97,116,111,109>>, [safe]).
> ** exception error: bad argument
> in function binary_to_term/2
> called as binary_to_term(<<131,100,0,6,109,121,97,116,111,109>>,[safe])
As the docs for this function note (in the manual), binary_to_term/2
will fail with a badarg if safe is specified and unsafe data is
decoded. The trace also contains the exact binary data it was called
with (which you can use to know what it really was, while debugging).
It shall always show the exact binary content, even if used with a name:
1> A = <<131,100,0,6,109,121,97,116,111,109>>.
2> binary_to_term(A, [safe]).
** exception error: bad argument
in function binary_to_term/2
called as binary_to_term(<<131,100,0,6,109,121,97,116,111,109>>,[safe])
> What is the best way to tell erlang what are the 'safe' atoms in my program?
By simply making the erlang runtime know it is not a 'new' atom.
> I'm currently defining and calling a trusted_atoms/0 function that just returns
> a list of all atoms I need.
> Is there a 'cleaner' way to do that?
> init() ->
> A = trusted_atoms().
> trusted_atoms() ->
> [atom1, atom2 atom3].
This is good enough I think. But I'll let others answer as to what'd be best.
More information about the erlang-questions