File: error.rst

package info (click to toggle)
svgpp 1.3.0%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,548 kB
  • sloc: cpp: 30,523; makefile: 165; python: 73; xml: 48; sh: 36
file content (103 lines) | stat: -rw-r--r-- 3,509 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
.. _error-handling:

Error handling
=================

Returned values
-----------------------

In most cases when SVG++ methods returns ``bool`` values, they can be used as an alternative to exceptions
- if method returns ``false``, then calling method immediately returns with ``false`` result and so on.

Error reporting is controlled by *Error Policy*. ``policy::error::raise_exception`` used by default
throws exception objects, derived from ``std::exception`` and ``boost::exception``. 
In this case only ``true`` may be returned as a result code.

Default Error Handling
----------------------------

Default ``policy::error::raise_exception`` uses Boost.Exception for transporting arbitrary 
data to the catch site.

``boost::error_info`` uses tags ``tag::error_info::xml_element`` and ``tag::error_info::xml_attribute`` 
to pass information about place in SVG document where error occured alongside with the exception object.
Value type depends on XML parser and *XML Policy* used.

Example of SVG++ exception handling when RapidXML NS parser is used::

  typedef rapidxml_ns::xml_node<> const * XMLElement;

  try
  {
    /* ... */
    document_traversal</* ... */>::load_document(/* ... */);
  }
  catch(svgpp::exception_base const & e)
  {
    typedef boost::error_info<svgpp::tag::error_info::xml_element, XMLElement> element_error_info;
    std::cerr << "Error reading SVG";
    if (XMLElement const * element = boost::get_error_info<element_error_info>(e))
      std::cerr 
        << " in element '" 
        << std::string((*element)->name(), (*element)->name() + (*element)->name_size())
        << "'";
    std::cerr << ": " << e.what() << "\n";
  }

.. _error_policy:

Error Policy Concept
---------------------------

::

  struct error_policy
  {
    typedef /* ... */ context_type;

    template<class XMLElement, class ElementName>
    static bool unknown_element(
      context_type const &, 
      XMLElement const & element, 
      ElementName const & name);

    template<class XMLAttributesIterator, class AttributeName>
    static bool unknown_attribute(context_type &, 
      XMLAttributesIterator const & attribute, 
      AttributeName const & name,
      BOOST_SCOPED_ENUM(detail::namespace_id) namespace_id,
      tag::source::attribute);

    template<class XMLAttributesIterator, class AttributeName>
    static bool unknown_attribute(context_type &, 
      XMLAttributesIterator const & attribute, 
      AttributeName const & name,
      tag::source::css);

    static bool unexpected_attribute(context_type &, 
      detail::attribute_id id, tag::source::attribute);
  
    template<class AttributeTag>
    static bool required_attribute_not_found(context_type &, AttributeTag);

    template<class AttributeTag, class AttributeValue>
    static bool parse_failed(context_type &, AttributeTag,
      AttributeValue const & value);

    template<class XMLElement>
    static bool unexpected_element(context_type &, XMLElement const & element);

    template<class AttributeTag>
    static bool negative_value(context_type &, AttributeTag);

    typedef /* ... */ intercepted_exception_type;

    template<class XMLElement>
    static bool add_element_info(intercepted_exception_type & e, 
      XMLElement const & element);
  };

If *Error Policy* method returns ``true``, then SVG++ continues SVG processing skipping the part with the error. 
In some cases it may lead to problems in further processing. 

If method returns ``false``, then processing immediately stops.