xmerl's error handling s?cks

Joakim G. jocke@REDACTED
Wed Mar 16 11:32:42 CET 2011


I use xmerl to parse an XML file according to an XML schema.
It works like a charm but the error handling was a painful
experience indeed.

I use the following:

====

load_xml(SchemaFilename, ConfigFilename) ->
    case catch xmerl_scan:file(ConfigFilename,
                               [{quiet, true},
                                {namespace_conformant, true},
                                {validation, schema},
                                {schemaLocation, [{"foo",
SchemaFilename}]}]) of
        {'EXIT', {fatal, Error}} -> Error;
        {ValidElement, []} -> {ok, ValidElement}
    end.

====

Then I wrote format_error/1 to take care of the errors:

====

format_error(
  {{failed_schema_validation,
    [{_, _, {undefined,
             {internal_error,
              {{badmatch,
                #xsd_state{errors = [{_ , _, {value_not_valid, Value,
                                              _SimpleTypes}}|_]}},
               _StackTrace}}}}]},
   {file, Filename},
   {line, Line},
   {col, Col}}) ->
    io_lib:format("~s: ~w: ~w: bad value: ~s", [Filename, Line, Col,
Value]);
format_error({{failed_schema_validation, [{type, ErrorCause, Value}|_]},
              {file, Filename},
              {line, Line},
              {col, Col}}) ->
    io_lib:format("~s: ~w: ~w: ~w: ~s",
                  [Filename, Line, Col, ErrorCause, Value]);
format_error({{failed_schema_validation, [{_, _, Error}|_]},
              {file, Filename},
              {line, Line},
              {col, Col}}) ->
    io_lib:format("~s: ~w: ~w: ~s",
                  [Filename, Line, Col,
                   lists:flatten(xmerl_xsd:format_error(Error))]);
format_error({Error,
              {file, Filename},
              {line, Line},
              {col, Col}}) ->
    io_lib:format("~s: ~w: ~w: syntax error: ~p", [Filename, Line, Col,
Error]

====

It still hurts figuring this out the hard way.
I probably do something wrong.

Cheers
/Jocke


More information about the erlang-questions mailing list