xmlwrapp
Lightweight C++ XML parsing library
Loading...
Searching...
No Matches
errors.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2013 Vaclav Slavik <vslavik@gmail.com>
3 * All Rights Reserved
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 * 3. Neither the name of the Author nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33/**
34 @file
35
36 This file contains errors-handling classes: xml::exception and
37 xml::error_handler and derived classes.
38 */
39
40#ifndef _xmlwrapp_errors_h_
41#define _xmlwrapp_errors_h_
42
43// xmlwrapp includes
44#include "xmlwrapp/export.h"
45
46#include <stdexcept>
47#include <string>
48#include <list>
49
50XMLWRAPP_MSVC_SUPPRESS_DLL_MEMBER_WARN
51
52/// XML library namespace
53namespace xml
54{
55
56class error_messages;
57
58/**
59 This exception class is thrown by xmlwrapp for all runtime XML-related
60 errors.
61
62 @note C++ runtime may still thrown other errors when used from xmlwrapp.
63 Also, std::bad_alloc() is thrown in out-of-memory situations.
64
65 @since 0.7.0
66 */
67class XMLWRAPP_INLINE_API exception : public std::runtime_error
68{
69public:
70 explicit exception(const std::string& what_arg)
71 : std::runtime_error(what_arg)
72 {}
73 explicit exception(const error_messages& what_arg);
74};
75
76
77/**
78 The xml::error_handler class is used to handle libxml2 errors and warnings
79 emitted during parsing, validation etc.
80
81 Although you can derive a custom handler from it, the specializations
82 included in xmlwrapp should suffice for most uses: throw_on_error and
83 throw_on_error_or_warning throw on issues, error_messages collects errors
84 and warnings without throwing.
85
86 @since 0.7.0
87 */
88class XMLWRAPP_API error_handler
89{
90public:
91 virtual ~error_handler() = default;
92
93 /// Called by xmlwrapp to report an error.
94 virtual void on_error(const std::string& msg) = 0;
95
96 /// Called by xmlwrapp to report a warning.
97 virtual void on_warning(const std::string& msg) = 0;
98};
99
100
101/**
102 An error handler that ignores both errors and warnings.
103
104 @see ignore_errors
105 */
107{
108public:
109 void on_error(const std::string&) override {}
110 void on_warning(const std::string&) override {}
111};
112
113/**
114 Specialization of error_handler that throws on any error.
115
116 @see throw_on_error
117 */
119{
120public:
121 void on_error(const std::string& msg) override { throw exception(msg); }
122 void on_warning(const std::string&) override {}
123};
124
125/**
126 Specialization of error_handler that throws on any error or warning.
127
128 @see throw_on_error_or_warning
129 */
131{
132public:
133 void on_warning(const std::string& msg) override { on_error(msg); }
134};
135
136/// Error handler ignoring all errors, its use is strongly discouraged.
137extern XMLWRAPP_API error_handler_ignore_errors ignore_errors;
138
139/// Error handler object that throws on any error.
140extern XMLWRAPP_API error_handler_throw_on_error throw_on_error;
141
142/// Error handler object that throws on any error or warning.
143extern XMLWRAPP_API error_handler_throw_on_error_or_warning throw_on_error_or_warning;
144
145
146/**
147 Single message in error_messages.
148
149 @since 0.7.0
150 */
152{
153public:
154 /// A type for different type of errors
156 {
157 type_error, ///< error
158 type_warning ///< warning
159 };
160
161 /**
162 Create a new xml::error_message object.
163
164 @param err_msg The error message.
165 @param msg_type The error type.
166 */
167 error_message(const std::string& err_msg, message_type msg_type)
168 : message_(err_msg), type_(msg_type)
169 {}
170
171 /// Get the error message type.
172 message_type type() const { return type_; }
173
174 /// Get the error message.
175 const std::string& message() const { return message_; }
176
177private:
178 std::string message_;
179 message_type type_;
180};
181
182
183/**
184 The xml::error_messages class is used to store all the error messages
185 which are collected while parsing or validating an XML document.
186
187 @since 0.7.0
188 */
189class XMLWRAPP_API error_messages : public error_handler
190{
191public:
192 /// A type to store multiple messages
193 using messages_type = std::list<error_message>;
194
195 error_messages() = default;
196
197 /// Get the error messages.
198 const messages_type& messages() const { return messages_; }
199
200 /**
201 Convenience function to find if there are any messages at all.
202 */
203 bool empty() const
204 {
205 return messages_.empty();
206 }
207
208 /**
209 Check if there are warnings in the error messages.
210
211 @return true if there is at least one warning in the error messages.
212 It does not consider errors.
213 */
214 bool has_warnings() const { return has_warnings_; }
215
216 /**
217 Check if there are any errors.
218 */
219 bool has_errors() const { return has_errors_; }
220
221
222 /**
223 Convert error messages into a single printable string.
224
225 The returned string is typically multiline, with the messages
226 separated with newlines ('\n').
227 */
228 std::string print() const;
229
230
231
232 // Implementation of error_handler methods:
233 void on_error(const std::string& msg) override;
234 void on_warning(const std::string& msg) override;
235
236protected:
237 /// Called by print() to format a single message.
238 virtual std::string format_for_print(const error_message& msg) const;
239
240private:
241 messages_type messages_;
242
243 bool has_errors_{false};
244 bool has_warnings_{false};
245};
246
247
248inline exception::exception(const error_messages& what_arg)
249 : std::runtime_error(what_arg.print())
250{
251}
252
253
254} // namespace xml
255
256XMLWRAPP_MSVC_RESTORE_DLL_MEMBER_WARN
257
258#endif // _xmlwrapp_errors_h_
An error handler that ignores both errors and warnings.
Definition errors.h:107
void on_error(const std::string &) override
Called by xmlwrapp to report an error.
Definition errors.h:109
void on_warning(const std::string &) override
Called by xmlwrapp to report a warning.
Definition errors.h:110
Specialization of error_handler that throws on any error or warning.
Definition errors.h:131
void on_warning(const std::string &msg) override
Called by xmlwrapp to report a warning.
Definition errors.h:133
Specialization of error_handler that throws on any error.
Definition errors.h:119
void on_error(const std::string &msg) override
Called by xmlwrapp to report an error.
Definition errors.h:121
void on_warning(const std::string &) override
Called by xmlwrapp to report a warning.
Definition errors.h:122
The xml::error_handler class is used to handle libxml2 errors and warnings emitted during parsing,...
Definition errors.h:89
virtual void on_error(const std::string &msg)=0
Called by xmlwrapp to report an error.
virtual void on_warning(const std::string &msg)=0
Called by xmlwrapp to report a warning.
Single message in error_messages.
Definition errors.h:152
message_type
A type for different type of errors.
Definition errors.h:156
@ type_warning
warning
Definition errors.h:158
@ type_error
error
Definition errors.h:157
message_type type() const
Get the error message type.
Definition errors.h:172
error_message(const std::string &err_msg, message_type msg_type)
Create a new xml::error_message object.
Definition errors.h:167
const std::string & message() const
Get the error message.
Definition errors.h:175
The xml::error_messages class is used to store all the error messages which are collected while parsi...
Definition errors.h:190
bool has_errors() const
Check if there are any errors.
Definition errors.h:219
bool has_warnings() const
Check if there are warnings in the error messages.
Definition errors.h:214
const messages_type & messages() const
Get the error messages.
Definition errors.h:198
std::string print() const
Convert error messages into a single printable string.
void on_warning(const std::string &msg) override
Called by xmlwrapp to report a warning.
void on_error(const std::string &msg) override
Called by xmlwrapp to report an error.
std::list< error_message > messages_type
A type to store multiple messages.
Definition errors.h:193
virtual std::string format_for_print(const error_message &msg) const
Called by print() to format a single message.
bool empty() const
Convenience function to find if there are any messages at all.
Definition errors.h:203
This exception class is thrown by xmlwrapp for all runtime XML-related errors.
Definition errors.h:68
XML library namespace.
Definition attributes.h:55
error_handler_throw_on_error throw_on_error
Error handler object that throws on any error.
error_handler_throw_on_error_or_warning throw_on_error_or_warning
Error handler object that throws on any error or warning.
error_handler_ignore_errors ignore_errors
Error handler ignoring all errors, its use is strongly discouraged.