add error_report::to_string and to_detail_string

This commit is contained in:
Daniel Larimer 2012-12-13 13:08:25 -05:00
parent eeee86be6b
commit cf10bac3db
5 changed files with 78 additions and 6 deletions

View file

@ -19,6 +19,9 @@ namespace fc {
error_frame& operator=(const error_frame& );
error_frame& operator=(error_frame&& );
fc::string to_string()const;
fc::string to_detail_string()const;
fc::string desc;
fc::string file;
int64_t line;
@ -43,6 +46,8 @@ namespace fc {
error_report& push_frame( const fc::string& file, uint64_t line, const fc::string& method, const fc::string& desc, fc::value meta = fc::value() );
error_report& append( const error_report& e );
fc::string to_string()const;
fc::string to_detail_string()const;
error_context stack; ///< Human readable stack of what we were atempting to do.
};

View file

@ -82,7 +82,7 @@ namespace fc {
friend string operator + ( const string&, const string& );
friend string operator + ( const string&, char c );
fc::string substr( int32_t start, int32_t len = 0x7fffffff );
fc::string substr( int32_t start, int32_t len = 0x7fffffff )const;
private:
fc::fwd<std::string,32> my;

View file

@ -1,5 +1,8 @@
#include <fc/error_report.hpp>
#include <fc/filesystem.hpp>
#include <fc/sstream.hpp>
#include <fc/value.hpp>
#include <fc/json.hpp>
namespace fc {
error_frame::error_frame( const fc::string& f, uint64_t l, const fc::string& m, const fc::string& d, fc::value met )
@ -70,4 +73,69 @@ fc::error_report& error_report::append( const error_report& e )
return *this;
}
fc::string error_frame::to_detail_string()const {
fc::stringstream ss;
ss << to_string() << "\n\t";
ss << file << ":" << line << "\t"<<method;
if( meta ) ss << "\t" <<fc::json::to_string(*meta);
return ss.str();
}
fc::string error_frame::to_string()const {
fc::stringstream ss;
int64_t prev = 0;
auto next = desc.find( '$' );
while( prev != int64_t(fc::string::npos) && prev < int64_t(desc.size()) ) {
// print everything from the last pos until the first '$'
ss << desc.substr( prev, next );
// if we got to the end, return it.
if( next == string::npos ) { return ss.str(); }
// if we are not at the end, then update the start
prev = next + 1;
if( desc[prev] == '{' ) {
// if the next char is a open, then find close
next = desc.find( '}', prev );
// if we found close...
if( next != fc::string::npos ) {
// the key is between prev and next
fc::string key = desc.substr( prev+1, (next-prev-1) );
if( meta ) {
auto itr = meta->find( key.c_str() );
if( itr != meta->end() ) {
ss << fc::json::to_string( itr->val );
} else {
ss << "???";
}
}
prev = next + 1;
// find the next $
next = desc.find( '$', prev );
} else {
// we didn't find it.. continue to while...
}
} else {
ss << desc[prev];
++prev;
next = desc.find( '$', prev );
}
}
return ss.str();
}
fc::string error_report::to_string()const {
fc::stringstream ss;
for( int i = 0; i < stack.size(); ++i ) {
ss << stack[i].to_string() << "\n";
}
return ss.str();
}
fc::string error_report::to_detail_string()const {
fc::stringstream ss;
for( int i = 0; i < stack.size(); ++i ) {
ss << stack[i].to_detail_string() << "\n";
}
return ss.str();
}
} // namespace fc

View file

@ -919,12 +919,11 @@ fc::string pretty_print( fc::vector<char>&& v, uint8_t indent ) {
value from_file( const fc::path& local_path ) {
if( !exists(local_path) ) {
slog("...");
FC_THROW_REPORT( "Source file '${filename}' does not exist", value().set("filename",local_path.string()) );
FC_THROW_REPORT( "Source file ${filename} does not exist", value().set("filename",local_path.string()) );
}
if( is_directory( local_path ) ) {
wlog("...");
FC_THROW_MSG( "Source path '%s' is a directory, expected a file.", local_path.string() );
FC_THROW_REPORT( "Source path ${path} is a directory; a file was expected",
value().set("path",local_path.string()) );
}
// memory map the file

View file

@ -36,7 +36,7 @@ namespace fc {
void string::clear() { my->clear(); }
void string::resize( uint64_t s ) { my->resize(s); }
fc::string string::substr( int32_t start, int32_t len ) { return my->substr(start,len); }
fc::string string::substr( int32_t start, int32_t len )const { return my->substr(start,len); }
const char* string::c_str()const { return my->c_str(); }
bool string::operator == ( const char* s )const { return *my == s; }