error handling enhancements, recursive var subst

This commit is contained in:
Daniel Larimer 2013-01-27 22:45:46 -05:00
parent c3659eedfc
commit 763ae69982
8 changed files with 53 additions and 17 deletions

View file

@ -13,7 +13,7 @@ namespace fc {
class error_frame { class error_frame {
public: public:
error_frame( const fc::string& file, uint64_t line, const fc::string& method, const fc::string& desc, fc::value m ); error_frame( const fc::string& file, uint64_t line, const fc::string& method, const fc::string& desc, fc::value m );
error_frame(){} error_frame():file("unknown-file"),line(0){}
error_frame(const error_frame& ); error_frame(const error_frame& );
error_frame(error_frame&& ); error_frame(error_frame&& );
@ -55,6 +55,7 @@ namespace fc {
}; };
fc::string substitute( const fc::string& format, const fc::value& keys ); fc::string substitute( const fc::string& format, const fc::value& keys );
fc::value recursive_substitute( const value& in, const fc::value& keys );
} // namespace fc } // namespace fc

View file

@ -189,9 +189,7 @@ namespace fc {
~key_val(){ } ~key_val(){ }
key_val& operator=( key_val&& k ) { key_val& operator=( key_val&& k ) {
slog( "swap key");
fc_swap( key, k.key ); fc_swap( key, k.key );
slog( "swap val");
fc_swap( val, k.val ); fc_swap( val, k.val );
return *this; return *this;
} }

View file

@ -181,8 +181,7 @@ namespace fc {
int idx = 0; int idx = 0;
for( auto i = a.fields.begin(); i != a.fields.end(); ++i ) { for( auto i = a.fields.begin(); i != a.fields.end(); ++i ) {
try { try {
m_out.push_back(T()); m_out.push_back( value_cast<T>( *i ) );
unpack( *i, m_out.back() );
} catch( fc::error_report& er ) { } catch( fc::error_report& er ) {
throw FC_REPORT_PUSH( er, "Error parsing array index ${index} to ${type}", throw FC_REPORT_PUSH( er, "Error parsing array index ${index} to ${type}",
fc::value().set("index", idx).set("type",fc::get_typename<T>::name()) ); fc::value().set("index", idx).set("type",fc::get_typename<T>::name()) );
@ -234,9 +233,7 @@ namespace fc {
template<typename Member> template<typename Member>
void operator()( Member& m ) { void operator()( Member& m ) {
try { try {
//m = value_cast<Member>(_val[idx]); m = value_cast<Member>(_val[idx]);
unpack( _val[idx], m );
// m = value_cast<Member>(_val[idx]);
} catch ( fc::error_report& er ) { } catch ( fc::error_report& er ) {
throw FC_REPORT_PUSH( er, "Error parsing tuple element ${index}", fc::value().set("index",idx) ); throw FC_REPORT_PUSH( er, "Error parsing tuple element ${index}", fc::value().set("index",idx) );
} }
@ -332,9 +329,6 @@ namespace fc {
} }
template<typename T> template<typename T>
T value::cast()const { T value::cast()const {
T tmp; return value_cast<T>(*this);
unpack(*this,tmp);
return tmp;
// return unpackvalue_cast<T>(*this);
} }
} }

View file

@ -135,6 +135,28 @@ fc::string substitute( const fc::string& format, const fc::value& keys ) {
return ss.str(); return ss.str();
} }
/**
* Performs variable substitution on all strings in keys or values of 'in' with
* values from keys.
*/
fc::value recursive_substitute( const value& in, const fc::value& keys ) {
fc::value out;
if( in.is_string() ) {
return fc::substitute( in.cast<string>(), keys );
}
else if( in.is_object() ) {
for( auto itr = in.begin(); itr != in.end(); ++itr ) {
out[fc::substitute(itr->key, keys)] = recursive_substitute( itr->val, keys );
}
return out;
}
else if( in.is_array() ) {
for( size_t i = 0; i < in.size(); ++i ) {
out.push_back( recursive_substitute( in[i], keys ) );
}
}
return in;
}
fc::string error_report::to_string()const { fc::string error_report::to_string()const {
fc::stringstream ss; fc::stringstream ss;

View file

@ -147,9 +147,29 @@ namespace fc {
fc::value().set("srcfile",f).set("dstfile",t).set("inner", fc::except_str() ) ); fc::value().set("srcfile",f).set("dstfile",t).set("inner", fc::except_str() ) );
} }
} }
void create_hard_link( const path& f, const path& t ) { boost::filesystem::create_hard_link( f, t ); } void create_hard_link( const path& f, const path& t ) {
bool remove( const path& f ) { return boost::filesystem::remove( f ); } try {
fc::path canonical( const fc::path& p ) { return boost::filesystem::canonical(p); } boost::filesystem::create_hard_link( f, t );
} catch ( ... ) {
FC_THROW_REPORT( "Unable to create hard link from '${from}' to '${to}'",
fc::value().set( "from", f )
.set("to",t).set("exception", fc::except_str() ) );
}
}
bool remove( const path& f ) {
try {
boost::filesystem::remove( f );
} catch ( ... ) {
FC_THROW_REPORT( "Unable to remove '${path}'", fc::value().set( "path", f ).set("exception", fc::except_str() ) );
}
}
fc::path canonical( const fc::path& p ) {
try {
return boost::filesystem::canonical(p);
} catch ( ... ) {
FC_THROW_REPORT( "Unable to resolve path '${path}'", fc::value().set( "path", p ).set("exception", fc::except_str() ) );
}
}
fc::path absolute( const fc::path& p ) { return boost::filesystem::absolute(p); } fc::path absolute( const fc::path& p ) { return boost::filesystem::absolute(p); }
path unique_path() { return boost::filesystem::unique_path(); } path unique_path() { return boost::filesystem::unique_path(); }
path temp_directory_path() { return boost::filesystem::temp_directory_path(); } path temp_directory_path() { return boost::filesystem::temp_directory_path(); }

View file

@ -39,6 +39,7 @@ namespace fc { namespace json {
while( !in.eof() ) { while( !in.eof() ) {
// std::cerr<<"\n**line size: "<<line.size()<<"\n\n"; // std::cerr<<"\n**line size: "<<line.size()<<"\n\n";
// slog( "line size: '%d'", line.size() ); // slog( "line size: '%d'", line.size() );
// slog( "%s", line.c_str() );
try { try {
fc::value v= fc::json::from_string( line ); fc::value v= fc::json::from_string( line );
self.handle_message(v); self.handle_message(v);

Binary file not shown.

Binary file not shown.