Switched all new API runtime_error exceptions to exceptions that derive from YAML::Exception

This commit is contained in:
beder
2012-05-14 22:12:31 -05:00
parent cfb7d46246
commit b20e0a5e54
4 changed files with 38 additions and 9 deletions

View File

@@ -57,7 +57,10 @@ namespace YAML
const char * const INVALID_SCALAR = "invalid scalar"; const char * const INVALID_SCALAR = "invalid scalar";
const char * const KEY_NOT_FOUND = "key not found"; const char * const KEY_NOT_FOUND = "key not found";
const char * const BAD_CONVERSION = "bad conversion";
const char * const BAD_DEREFERENCE = "bad dereference"; const char * const BAD_DEREFERENCE = "bad dereference";
const char * const BAD_SUBSCRIPT = "operator[] call on a scalar";
const char * const BAD_PUSHBACK = "appending to a non-sequence";
const char * const UNMATCHED_GROUP_TAG = "unmatched group tag"; const char * const UNMATCHED_GROUP_TAG = "unmatched group tag";
const char * const UNEXPECTED_END_SEQ = "unexpected end sequence token"; const char * const UNEXPECTED_END_SEQ = "unexpected end sequence token";
@@ -148,12 +151,37 @@ namespace YAML
return TypedKeyNotFound <T> (mark, key); return TypedKeyNotFound <T> (mark, key);
} }
class BadConversion: public RepresentationException {
public:
BadConversion()
: RepresentationException(Mark::null(), ErrorMsg::BAD_CONVERSION) {}
};
template<typename T>
class TypedBadConversion: public BadConversion {
public:
TypedBadConversion()
: BadConversion() {}
};
class BadDereference: public RepresentationException { class BadDereference: public RepresentationException {
public: public:
BadDereference() BadDereference()
: RepresentationException(Mark::null(), ErrorMsg::BAD_DEREFERENCE) {} : RepresentationException(Mark::null(), ErrorMsg::BAD_DEREFERENCE) {}
}; };
class BadSubscript: public RepresentationException {
public:
BadSubscript()
: RepresentationException(Mark::null(), ErrorMsg::BAD_SUBSCRIPT) {}
};
class BadPushback: public RepresentationException {
public:
BadPushback()
: RepresentationException(Mark::null(), ErrorMsg::BAD_PUSHBACK) {}
};
class EmitterException: public Exception { class EmitterException: public Exception {
public: public:
EmitterException(const std::string& msg_) EmitterException(const std::string& msg_)

View File

@@ -61,7 +61,7 @@ namespace YAML
return *pNode; return *pNode;
return pMemory->create_node(); return pMemory->create_node();
case NodeType::Scalar: case NodeType::Scalar:
throw std::runtime_error("Can't call operator[] on a scalar"); throw BadSubscript();
} }
for(node_map::const_iterator it=m_map.begin();it!=m_map.end();++it) { for(node_map::const_iterator it=m_map.begin();it!=m_map.end();++it) {
@@ -89,7 +89,7 @@ namespace YAML
convert_to_map(pMemory); convert_to_map(pMemory);
break; break;
case NodeType::Scalar: case NodeType::Scalar:
throw std::runtime_error("Can't call operator[] on a scalar"); throw BadSubscript();
} }
for(node_map::const_iterator it=m_map.begin();it!=m_map.end();++it) { for(node_map::const_iterator it=m_map.begin();it!=m_map.end();++it) {

View File

@@ -10,6 +10,7 @@
#include "yaml-cpp/node/iterator.h" #include "yaml-cpp/node/iterator.h"
#include "yaml-cpp/node/detail/memory.h" #include "yaml-cpp/node/detail/memory.h"
#include "yaml-cpp/node/detail/node.h" #include "yaml-cpp/node/detail/node.h"
#include "yaml-cpp/exceptions.h"
#include <string> #include <string>
namespace YAML namespace YAML
@@ -102,12 +103,12 @@ namespace YAML
const T operator()() const { const T operator()() const {
if(!node.m_pNode) if(!node.m_pNode)
throw std::runtime_error("Unable to convert to type"); throw TypedBadConversion<T>();
T t; T t;
if(convert<T>::decode(node, t)) if(convert<T>::decode(node, t))
return t; return t;
throw std::runtime_error("Unable to convert to type"); throw TypedBadConversion<T>();
} }
}; };
@@ -118,7 +119,7 @@ namespace YAML
const std::string operator()() const { const std::string operator()() const {
if(node.Type() != NodeType::Scalar) if(node.Type() != NodeType::Scalar)
throw std::runtime_error("Unable to convert to string, not a scalar"); throw TypedBadConversion<std::string>();
return node.Scalar(); return node.Scalar();
} }
}; };

View File

@@ -1,8 +1,8 @@
#include "yaml-cpp/node/detail/node_data.h" #include "yaml-cpp/node/detail/node_data.h"
#include "yaml-cpp/node/detail/memory.h" #include "yaml-cpp/node/detail/memory.h"
#include "yaml-cpp/node/detail/node.h" #include "yaml-cpp/node/detail/node.h"
#include "yaml-cpp/exceptions.h"
#include <sstream> #include <sstream>
#include <stdexcept>
namespace YAML namespace YAML
{ {
@@ -161,7 +161,7 @@ namespace YAML
} }
if(m_type != NodeType::Sequence) if(m_type != NodeType::Sequence)
throw std::runtime_error("Can't push_back to a non-sequence node"); throw BadPushback();
m_sequence.push_back(&node); m_sequence.push_back(&node);
} }
@@ -177,7 +177,7 @@ namespace YAML
convert_to_map(pMemory); convert_to_map(pMemory);
break; break;
case NodeType::Scalar: case NodeType::Scalar:
throw std::runtime_error("Can't call operator[] on a scalar"); throw BadSubscript();
} }
insert_map_pair(key, value); insert_map_pair(key, value);
@@ -208,7 +208,7 @@ namespace YAML
convert_to_map(pMemory); convert_to_map(pMemory);
break; break;
case NodeType::Scalar: case NodeType::Scalar:
throw std::runtime_error("Can't call operator[] on a scalar"); throw BadSubscript();
} }
for(node_map::const_iterator it=m_map.begin();it!=m_map.end();++it) { for(node_map::const_iterator it=m_map.begin();it!=m_map.end();++it) {