[erlang-questions] better error message when using term_to_binary with [safe] ?
Sun Mar 13 21:35:56 CET 2011
When you have a large binary with a complex erlang term that contains many atoms
it can be hard to guess which atom cause the function to fail.
I can write a function that traverses the binary and check every single atom but
I would expect binary_to_term
to just tell why it fails. Unless it is very hard to implement for some reason.
From: Harsh J <>
To: Pablo Platt <>
Sent: Sun, March 13, 2011 10:21:31 PM
Subject: Re: [erlang-questions] better error message when using term_to_binary
with [safe] ?
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
> 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