yamerl/doc/user-guide/recipe-error-handling.md
2012-12-11 19:05:23 +01:00

1.1 KiB

yamerl Recipe: Error handling

yamerl throws an exception when an error occurs.

  1. Start the yamerl application. This is a mandatory step.
application:start(yamerl).
  1. You're now ready to parse a serialized document:
  • To parse an in-memory string or binary:
-include_lib("yamerl/include/yamerl_errors.hrl").

% ...

try
  Documents = yamerl_constr:string("Hello!"),
  % Documents is a list of constructed documents.
  Documents
catch
  throw:#yamerl_exception{errors = Errors} ->
    % Do something with the exception.
    Errors
end.

As you can see, the #yamerl_exception{} record embeds all encountered errors:

#yamerl_exception{
  errors = [] % List of errors.
}.

Errors are records where the two first members are always:

  1. type, either error or warning;
  2. text, a human-readable error message.

Following members depend on the error record. Two records are currently defined:

  • #yamerl_invalid_option{};
  • #yamerl_parsing_error{}.

For further informations, see: