Improve error messages.

This commit is contained in:
Hermann Kraus 2012-03-12 01:09:26 +01:00
parent 502773bea6
commit 02d8a98b3f
13 changed files with 84 additions and 64 deletions

View file

@ -32,16 +32,16 @@ class xml_node;
class config_error : public std::exception
{
public:
config_error() {}
config_error(std::string const& what, xml_node const* node = 0, std::string const& filename="");
config_error(unsigned line_number, std::string const& filename, std::string const& what);
config_error(std::string const& what);
config_error(std::string const& what, xml_node const& node);
config_error(std::string const& what, unsigned line_number, std::string const& filename);
virtual ~config_error() throw() {}
virtual const char * what() const throw();
void append_context(const std::string & ctx, xml_node const* node = 0, std::string const& filename="") const;
void append_context(const std::string & ctx) const;
void append_context(const std::string & ctx, xml_node const& node) const;
void append_context(xml_node const& node) const;
protected:
mutable std::string what_;
mutable unsigned line_number_;

View file

@ -26,7 +26,6 @@
// mapnik
#include <mapnik/config.hpp>
#include <mapnik/global.hpp>
#include <mapnik/config_error.hpp>
// boost
#include <boost/utility.hpp>

View file

@ -86,8 +86,9 @@ public:
typedef std::map<std::string, xml_attribute> attribute_map;
xml_node(xml_tree &tree, std::string name, unsigned line=0, bool text_node = false);
std::string name() const;
std::string text() const;
std::string const& name() const;
std::string const& text() const;
std::string const& filename() const;
bool is_text() const;
bool is(std::string const& name) const;
@ -128,7 +129,7 @@ private:
bool text_node_;
unsigned line_;
mutable bool processed_;
static std::string xml_text;
};
} //ns mapnik

View file

@ -38,7 +38,7 @@ class xml_tree
public:
xml_tree(std::string const& encoding="utf8");
void set_filename(std::string fn);
std::string filename() const;
std::string const& filename() const;
xml_node &root();
private:
xml_node node_;

View file

@ -3,19 +3,21 @@
namespace mapnik
{
config_error::config_error(std::string const& what, xml_node const* node, std::string const& filename)
: what_( what ), line_number_(0), file_(filename), node_name_(), msg_()
config_error::config_error(std::string const& what)
: what_(what), line_number_(0), file_(), node_name_(), msg_()
{
if (node)
{
node_name_ = node->name();
line_number_ = node->line();
}
}
config_error::config_error(unsigned line_number, std::string const& filename, std::string const& what)
: what_( what ), line_number_(line_number), file_(filename), node_name_(), msg_()
config_error::config_error(std::string const& what, xml_node const& node)
: what_(what), line_number_(node.line()), file_(node.filename()), node_name_(node.name()), msg_()
{
}
config_error::config_error(std::string const& what, unsigned line_number, std::string const& filename)
: what_(what), line_number_(line_number), file_(filename), node_name_(), msg_()
{
}
@ -32,14 +34,22 @@ config_error::config_error(unsigned line_number, std::string const& filename, st
return msg_.c_str();
}
void config_error::append_context(const std::string & ctx, xml_node const* node, std::string const& filename) const
void config_error::append_context(std::string const& ctx) const
{
what_ += " " + ctx;
}
void config_error::append_context(std::string const& ctx, xml_node const& node) const
{
what_ += " " + ctx;
if (node)
{
if (!line_number_) line_number_ = node->line();
if (node_name_.empty()) node_name_ = node->name();
if (file_.empty()) file_ = filename;
}
append_context(ctx);
append_context(node);
}
void config_error::append_context(xml_node const& node) const
{
if (!line_number_) line_number_ = node.line();
if (node_name_.empty()) node_name_ = node.name();
if (file_.empty()) file_ = node.filename();
}
}

View file

@ -51,7 +51,7 @@ void registry::register_name(std::string name, from_xml_function_ptr ptr, bool o
node_ptr registry::from_xml(xml_node const& xml)
{
std::map<std::string, from_xml_function_ptr>::const_iterator itr = map_.find(xml.name());
if (itr == map_.end()) throw config_error("Unknown element '" + xml.name() + "'", &xml);
if (itr == map_.end()) throw config_error("Unknown element '" + xml.name() + "'", xml);
return itr->second(xml);
}
} //ns formatting

View file

@ -92,7 +92,7 @@ public:
os << ": " << std::endl << error->message;
// remove CR
std::string msg = os.str().substr(0, os.str().size() - 1);
throw config_error(error->line, error->file, msg);
throw config_error(msg, error->line, error->file);
}
}
@ -139,7 +139,7 @@ public:
{
os << ": " << std::endl << error->message;
}
throw config_error(error->line, error->file, os.str());
throw config_error(os.str(), error->line, error->file);
}
int iXIncludeReturn = xmlXIncludeProcessFlags(doc, options_);

View file

@ -51,6 +51,7 @@
#include <mapnik/text_placements/dummy.hpp>
#include <mapnik/symbolizer.hpp>
#include <mapnik/rule.hpp>
#include <mapnik/config_error.hpp>
// boost
#include <boost/optional.hpp>
@ -312,7 +313,7 @@ void map_parser::parse_map(Map & map, xml_node const& pt, std::string const& bas
}
catch (const config_error & ex)
{
ex.append_context("", &map_node, filename_);
ex.append_context(map_node);
throw;
}
@ -416,7 +417,7 @@ void map_parser::parse_map_include(Map & map, xml_node const& include)
}
}
} catch (const config_error & ex) {
ex.append_context("", &include, filename_);
ex.append_context(include);
throw;
}
@ -447,7 +448,7 @@ void map_parser::parse_style(Map & map, xml_node const& sty)
map.insert_style(name, style);
} catch (const config_error & ex) {
ex.append_context(std::string("in style '") + name + "'", &sty, filename_);
ex.append_context(std::string("in style '") + name + "'", sty);
throw;
}
}
@ -462,7 +463,7 @@ void map_parser::parse_metawriter(Map & map, xml_node const& pt)
writer = metawriter_create(pt);
map.insert_metawriter(name, writer);
} catch (const config_error & ex) {
ex.append_context(std::string("in meta writer '") + name + "'", &pt, filename_);
ex.append_context(std::string("in meta writer '") + name + "'", pt);
}
}
@ -491,7 +492,7 @@ void map_parser::parse_fontset(Map & map, xml_node const& fset)
// when it's parsed
fontsets_.insert(pair<std::string, font_set>(name, fontset));
} catch (const config_error & ex) {
ex.append_context(std::string("in FontSet '") + name + "'", &fset, filename_);
ex.append_context(std::string("in FontSet '") + name + "'", fset);
throw;
}
}
@ -509,7 +510,7 @@ void map_parser::parse_font(font_set &fset, xml_node const& f)
}
else
{
throw config_error("Must have 'face-name' set", &f, filename_);
throw config_error("Must have 'face-name' set", f);
}
}
@ -652,7 +653,7 @@ void map_parser::parse_layer(Map & map, xml_node const& lay)
{
if (!name.empty())
{
ex.append_context(std::string(" encountered during parsing of layer '") + name + "'");
ex.append_context(std::string(" encountered during parsing of layer '") + name + "'", lay);
}
throw;
}
@ -748,7 +749,7 @@ void map_parser::parse_rule(feature_type_style & style, xml_node const& r)
{
if (!name.empty() )
{
ex.append_context(std::string("in rule '") + name + "'");
ex.append_context(std::string("in rule '") + name + "'", r);
}
throw;
}
@ -844,7 +845,7 @@ void map_parser::parse_point_symbolizer(rule & rule, xml_node const & sym)
}
catch (const config_error & ex)
{
ex.append_context("in PointSymbolizer");
ex.append_context("in PointSymbolizer", sym);
throw;
}
}
@ -958,7 +959,7 @@ void map_parser::parse_markers_symbolizer(rule & rule, xml_node const& sym)
}
catch (const config_error & ex)
{
ex.append_context("in MarkersSymbolizer");
ex.append_context("in MarkersSymbolizer", sym);
throw;
}
}
@ -1004,7 +1005,7 @@ void map_parser::parse_line_pattern_symbolizer( rule & rule, xml_node const & sy
}
catch (const config_error & ex)
{
ex.append_context("in LinePatternSymbolizer");
ex.append_context("in LinePatternSymbolizer", sym);
throw;
}
}
@ -1063,7 +1064,7 @@ void map_parser::parse_polygon_pattern_symbolizer( rule & rule,
}
catch (const config_error & ex)
{
ex.append_context("in PolygonPatternSymbolizer");
ex.append_context("in PolygonPatternSymbolizer", sym);
throw;
}
}
@ -1090,7 +1091,7 @@ void map_parser::parse_text_symbolizer( rule & rule, xml_node const& sym )
}
catch (const config_error & ex)
{
ex.append_context("in TextSymbolizer");
ex.append_context("in TextSymbolizer", sym);
throw;
}
}
@ -1195,7 +1196,7 @@ void map_parser::parse_shield_symbolizer(rule & rule, xml_node const& sym )
}
catch (const config_error & ex)
{
ex.append_context("in ShieldSymbolizer");
ex.append_context("in ShieldSymbolizer", sym);
throw;
}
}
@ -1293,7 +1294,7 @@ void map_parser::parse_line_symbolizer( rule & rule, xml_node const & sym )
}
catch (const config_error & ex)
{
ex.append_context("in LineSymbolizer");
ex.append_context("in LineSymbolizer", sym);
throw;
}
}
@ -1322,7 +1323,7 @@ void map_parser::parse_polygon_symbolizer( rule & rule, xml_node const & sym )
}
catch (const config_error & ex)
{
ex.append_context("in PolygonSymbolizer");
ex.append_context("in PolygonSymbolizer", sym);
throw;
}
}
@ -1349,7 +1350,7 @@ void map_parser::parse_building_symbolizer( rule & rule, xml_node const & sym )
}
catch (const config_error & ex)
{
ex.append_context("in BuildingSymbolizer");
ex.append_context("in BuildingSymbolizer", sym);
throw;
}
}
@ -1398,13 +1399,13 @@ void map_parser::parse_raster_symbolizer( rule & rule, xml_node const & sym )
}
catch (const config_error & ex)
{
ex.append_context("in RasterSymbolizer");
ex.append_context("in RasterSymbolizer", sym);
throw;
}
}
void map_parser::parse_raster_colorizer(raster_colorizer_ptr const& rc,
xml_node const& node )
xml_node const& node)
{
try
{
@ -1484,7 +1485,7 @@ void map_parser::parse_raster_colorizer(raster_colorizer_ptr const& rc,
}
catch (const config_error & ex)
{
ex.append_context("in RasterColorizer");
ex.append_context("in RasterColorizer", node);
throw;
}
}

View file

@ -61,7 +61,7 @@ metawriter_create(xml_node const& pt)
metawriter_inmem_ptr inmem = metawriter_inmem_ptr(new metawriter_inmem(properties));
writer = inmem;
} else {
throw config_error(string("Unknown type '") + type + "'", &pt);
throw config_error(string("Unknown type '") + type + "'", pt);
}
return writer;

View file

@ -21,6 +21,7 @@
*****************************************************************************/
#include <mapnik/palette.hpp>
#include <mapnik/config_error.hpp>
namespace mapnik
{

View file

@ -48,7 +48,7 @@ void registry::register_name(std::string name, from_xml_function_ptr ptr, bool o
text_placements_ptr registry::from_xml(std::string name, xml_node const& xml, fontset_map const& fontsets)
{
std::map<std::string, from_xml_function_ptr>::const_iterator itr = map_.find(name);
if (itr == map_.end()) throw config_error("Unknown placement-type '" + name + "'", &xml);
if (itr == map_.end()) throw config_error("Unknown placement-type '" + name + "'", xml);
return itr->second(xml, fontsets);
}
} //ns formatting

View file

@ -267,16 +267,16 @@ void char_properties::from_xml(xml_node const& sym, fontset_map const& fontsets)
fontset = itr->second;
} else
{
throw config_error("Unable to find any fontset named '" + *fontset_name_ + "'", &sym);
throw config_error("Unable to find any fontset named '" + *fontset_name_ + "'", sym);
}
}
if (!face_name.empty() && !fontset.get_name().empty())
{
throw config_error("Can't have both face-name and fontset-name", &sym);
throw config_error("Can't have both face-name and fontset-name", sym);
}
if (face_name.empty() && fontset.get_name().empty())
{
throw config_error("Must have face-name or fontset-name", &sym);
throw config_error("Must have face-name or fontset-name", sym);
}
}

View file

@ -29,6 +29,7 @@
#include <mapnik/line_symbolizer.hpp>
#include <mapnik/feature_type_style.hpp>
#include <mapnik/text_properties.hpp>
#include <mapnik/config_error.hpp>
//boost
#include <boost/lexical_cast.hpp>
@ -171,7 +172,7 @@ void xml_tree::set_filename(std::string fn)
file_ = fn;
}
std::string xml_tree::filename() const
std::string const& xml_tree::filename() const
{
return file_;
}
@ -255,20 +256,27 @@ xml_node::xml_node(xml_tree &tree, std::string name, unsigned line, bool text_no
}
std::string xml_node::name() const
std::string xml_node::xml_text = "<xmltext>";
std::string const& xml_node::name() const
{
if (!text_node_)
return name_;
else
return "<xmltext>";
return xml_text;
}
std::string xml_node::text() const
std::string const& xml_node::text() const
{
if (text_node_)
return name_;
else
return "NOT A TEXT NODE"; //TODO: throw
throw config_error("text() called on non-text node", *this);
}
std::string const& xml_node::filename() const
{
return tree_.filename();
}
bool xml_node::is_text() const
@ -416,9 +424,9 @@ T xml_node::get_value() const
boost::optional<T> result = fast_cast<T>(tree_, get_text());
if (!result)
{
throw config_error(std::string("Failed to parse value in node '") +
name_ + "'. Expected " + name_trait<T>::name() +
" but got '" + get_text() + "'");
throw config_error(std::string("Failed to parse value. Expected ")
+ name_trait<T>::name() +
" but got '" + get_text() + "'", *this);
}
return *result;
}