From b20e0a5e54b7c2aa7acc10ca7203ca1509ad7b2b Mon Sep 17 00:00:00 2001 From: beder Date: Mon, 14 May 2012 22:12:31 -0500 Subject: [PATCH] Switched all new API runtime_error exceptions to exceptions that derive from YAML::Exception --- include/yaml-cpp/exceptions.h | 28 ++++++++++++++++++++++++++++ include/yaml-cpp/node/detail/impl.h | 4 ++-- include/yaml-cpp/node/impl.h | 7 ++++--- src/node_data.cpp | 8 ++++---- 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/include/yaml-cpp/exceptions.h b/include/yaml-cpp/exceptions.h index 394d586..04babd7 100644 --- a/include/yaml-cpp/exceptions.h +++ b/include/yaml-cpp/exceptions.h @@ -57,7 +57,10 @@ namespace YAML const char * const INVALID_SCALAR = "invalid scalar"; 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_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 UNEXPECTED_END_SEQ = "unexpected end sequence token"; @@ -148,11 +151,36 @@ namespace YAML return TypedKeyNotFound (mark, key); } + class BadConversion: public RepresentationException { + public: + BadConversion() + : RepresentationException(Mark::null(), ErrorMsg::BAD_CONVERSION) {} + }; + + template + class TypedBadConversion: public BadConversion { + public: + TypedBadConversion() + : BadConversion() {} + }; + class BadDereference: public RepresentationException { public: BadDereference() : 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 { public: diff --git a/include/yaml-cpp/node/detail/impl.h b/include/yaml-cpp/node/detail/impl.h index 5dd7a06..e3d17af 100644 --- a/include/yaml-cpp/node/detail/impl.h +++ b/include/yaml-cpp/node/detail/impl.h @@ -61,7 +61,7 @@ namespace YAML return *pNode; return pMemory->create_node(); 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) { @@ -89,7 +89,7 @@ namespace YAML convert_to_map(pMemory); break; 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) { diff --git a/include/yaml-cpp/node/impl.h b/include/yaml-cpp/node/impl.h index e5c1693..00ae1f6 100644 --- a/include/yaml-cpp/node/impl.h +++ b/include/yaml-cpp/node/impl.h @@ -10,6 +10,7 @@ #include "yaml-cpp/node/iterator.h" #include "yaml-cpp/node/detail/memory.h" #include "yaml-cpp/node/detail/node.h" +#include "yaml-cpp/exceptions.h" #include namespace YAML @@ -102,12 +103,12 @@ namespace YAML const T operator()() const { if(!node.m_pNode) - throw std::runtime_error("Unable to convert to type"); + throw TypedBadConversion(); T t; if(convert::decode(node, t)) return t; - throw std::runtime_error("Unable to convert to type"); + throw TypedBadConversion(); } }; @@ -118,7 +119,7 @@ namespace YAML const std::string operator()() const { if(node.Type() != NodeType::Scalar) - throw std::runtime_error("Unable to convert to string, not a scalar"); + throw TypedBadConversion(); return node.Scalar(); } }; diff --git a/src/node_data.cpp b/src/node_data.cpp index 3bf6540..6f82f7b 100644 --- a/src/node_data.cpp +++ b/src/node_data.cpp @@ -1,8 +1,8 @@ #include "yaml-cpp/node/detail/node_data.h" #include "yaml-cpp/node/detail/memory.h" #include "yaml-cpp/node/detail/node.h" +#include "yaml-cpp/exceptions.h" #include -#include namespace YAML { @@ -161,7 +161,7 @@ namespace YAML } 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); } @@ -177,7 +177,7 @@ namespace YAML convert_to_map(pMemory); break; case NodeType::Scalar: - throw std::runtime_error("Can't call operator[] on a scalar"); + throw BadSubscript(); } insert_map_pair(key, value); @@ -208,7 +208,7 @@ namespace YAML convert_to_map(pMemory); break; 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) {