Responses contain jsonrpc field and increased readability of errors #13

This commit is contained in:
Michael Vandeberg 2017-04-10 11:12:24 -04:00
parent 5cfcb5c0d6
commit fa63cd799f
3 changed files with 12 additions and 7 deletions

View file

@ -23,7 +23,10 @@ namespace fc { namespace rpc {
response(){} response(){}
response( int64_t i, fc::variant r ):id(i),result(r){} response( int64_t i, fc::variant r ):id(i),result(r){}
response( int64_t i, error_object r ):id(i),error(r){} response( int64_t i, error_object r ):id(i),error(r){}
response( int64_t i, fc::variant r, string j ):id(i),jsonrpc(j),result(r){}
response( int64_t i, error_object r, string j ):id(i),jsonrpc(j),error(r){}
int64_t id = 0; int64_t id = 0;
optional<fc::string> jsonrpc;
optional<fc::variant> result; optional<fc::variant> result;
optional<error_object> error; optional<error_object> error;
}; };
@ -57,4 +60,4 @@ namespace fc { namespace rpc {
FC_REFLECT( fc::rpc::request, (id)(method)(params) ); FC_REFLECT( fc::rpc::request, (id)(method)(params) );
FC_REFLECT( fc::rpc::error_object, (code)(message)(data) ) FC_REFLECT( fc::rpc::error_object, (code)(message)(data) )
FC_REFLECT( fc::rpc::response, (id)(result)(error) ) FC_REFLECT( fc::rpc::response, (id)(jsonrpc)(result)(error) )

View file

@ -156,7 +156,7 @@ namespace fc
* and other information that is generally only useful for * and other information that is generally only useful for
* developers. * developers.
*/ */
string exception::to_detail_string( log_level ll )const string exception::to_detail_string( log_level ll )const
{ {
fc::stringstream ss; fc::stringstream ss;
ss << variant(my->_code).as_string() <<" " << my->_name << ": " <<my->_what<<"\n"; ss << variant(my->_code).as_string() <<" " << my->_name << ": " <<my->_what<<"\n";
@ -174,13 +174,14 @@ namespace fc
/** /**
* Generates a user-friendly error report. * Generates a user-friendly error report.
*/ */
string exception::to_string( log_level ll )const string exception::to_string( log_level ll )const
{ {
fc::stringstream ss; fc::stringstream ss;
ss << what() << " (" << variant(my->_code).as_string() <<")\n"; ss << what() << ":";
for( auto itr = my->_elog.begin(); itr != my->_elog.end(); ++itr ) for( auto itr = my->_elog.begin(); itr != my->_elog.end(); ++itr )
{ {
ss << fc::format_string( itr->get_format(), itr->get_data() ) <<"\n"; if( itr->get_format().size() )
ss << " " << fc::format_string( itr->get_format(), itr->get_data() );
// ss << " " << itr->get_context().to_string() <<"\n"; // ss << " " << itr->get_context().to_string() <<"\n";
} }
return ss.str(); return ss.str();

View file

@ -90,6 +90,7 @@ std::string websocket_api_connection::on_message(
{ {
auto var = fc::json::from_string(message); auto var = fc::json::from_string(message);
const auto& var_obj = var.get_object(); const auto& var_obj = var.get_object();
if( var_obj.contains( "method" ) ) if( var_obj.contains( "method" ) )
{ {
auto call = var.as<fc::rpc::request>(); auto call = var.as<fc::rpc::request>();
@ -115,7 +116,7 @@ std::string websocket_api_connection::on_message(
if( call.id ) if( call.id )
{ {
auto reply = fc::json::to_string( response( *call.id, result ) ); auto reply = fc::json::to_string( response( *call.id, result, "2.0" ) );
if( send_message ) if( send_message )
_connection.send_message( reply ); _connection.send_message( reply );
return reply; return reply;
@ -132,7 +133,7 @@ std::string websocket_api_connection::on_message(
} }
if( optexcept ) { if( optexcept ) {
auto reply = fc::json::to_string( response( *call.id, error_object{ 1, optexcept->to_detail_string(), fc::variant(*optexcept)} ) ); auto reply = fc::json::to_string( response( *call.id, error_object{ 1, optexcept->to_string(), fc::variant(*optexcept)}, "2.0" ) );
if( send_message ) if( send_message )
_connection.send_message( reply ); _connection.send_message( reply );