Skip to content

Commit

Permalink
Improve error details to display request error details
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrianto Lie committed Feb 27, 2018
1 parent ff2c6f8 commit 47c0458
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 21 deletions.
11 changes: 6 additions & 5 deletions libraries/chain/name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
namespace eosio { namespace chain {

void name::set( const char* str ) {
try {
const auto len = strnlen(str,14);
EOS_ASSERT( len <= 13, name_type_exception, "Name is longer than 13 characters (${name}) ", ("name",string(str)) );
const auto len = strnlen(str, 14);
EOS_ASSERT(len <= 13, name_type_exception, "Name is longer than 13 characters (${name}) ", ("name", string(str)));
value = string_to_name(str);
EOS_ASSERT( to_string() == string(str), name_type_exception, "Name not properly normalized (name: ${name}, normalized: ${normalized}) ", ("name",string(str))("normalized",to_string()) );
}FC_CAPTURE_AND_RETHROW( (str) ) }
EOS_ASSERT(to_string() == string(str), name_type_exception,
"Name not properly normalized (name: ${name}, normalized: ${normalized}) ",
("name", string(str))("normalized", to_string()));
}

name::operator string()const {
static const char* charmap = ".12345abcdefghijklmnopqrstuvwxyz";
Expand Down
6 changes: 3 additions & 3 deletions plugins/http_plugin/http_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ namespace eosio {
} else {
wlog("404 - not found: ${ep}", ("ep",resource));
error_results results{websocketpp::http::status_code::not_found,
"Not Found", fc::exception(fc::log_message(fc::log_context(), "Unknown Endpoint"))};
"Not Found", fc::exception(FC_LOG_MESSAGE(error, "Unknown Endpoint"))};
con->set_body(fc::json::to_string(results));
con->set_status(websocketpp::http::status_code::not_found);
}
Expand All @@ -199,12 +199,12 @@ namespace eosio {
} catch( const std::exception& e ) {
elog( "http: ${e}", ("e",e.what()));
error_results results{websocketpp::http::status_code::internal_server_error,
"Internal Service Error", fc::exception(fc::log_message(fc::log_context(), e.what()))};
"Internal Service Error", fc::exception(FC_LOG_MESSAGE(error, e.what()))};
con->set_body(fc::json::to_string(results));
con->set_status(websocketpp::http::status_code::internal_server_error);
} catch( ... ) {
error_results results{websocketpp::http::status_code::internal_server_error,
"Internal Service Error", fc::exception(fc::log_message(fc::log_context(), "Unknown Exception"))};
"Internal Service Error", fc::exception(FC_LOG_MESSAGE(error, "Unknown Exception"))};
con->set_body(fc::json::to_string(results));
con->set_status(websocketpp::http::status_code::internal_server_error);
}
Expand Down
12 changes: 8 additions & 4 deletions programs/eosioc/help_text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "help_text.hpp"
#include "localize.hpp"
#include <regex>
#include <fc/io/json.hpp>

using namespace eosio::client::localize;

Expand Down Expand Up @@ -196,7 +197,7 @@ const std::map<int64_t, std::string> error_advice = {


namespace eosio { namespace client { namespace help {
bool print_recognized_error_code(const fc::exception& e, const bool show_error_trace) {
bool print_recognized_error_code(const fc::exception& e, const bool verbose_errors) {
// eos recognized error code is from 3000000 to 3999999
// refer to libraries/chain/include/eosio/chain/exceptions.hpp
if (e.code() >= 3000000 && e.code() <= 3999999) {
Expand All @@ -212,9 +213,12 @@ bool print_recognized_error_code(const fc::exception& e, const bool show_error_t
if (!log.get_format().empty()) {
// Localize the message as needed
explanation += "\n " + localized_with_variant(log.get_format().data(), log.get_data());
} else if (log.get_data().size() > 0 && verbose_errors) {
// Show data-only log only if verbose_errors option is enabled
explanation += "\n " + fc::json::to_string(log.get_data());
}
// Check if there's stack trace to be added
if (!log.get_context().get_method().empty() && show_error_trace) {
if (!log.get_context().get_method().empty() && verbose_errors) {
stack_trace += "\n " +
log.get_context().get_file() + ":" +
fc::to_string(log.get_context().get_line_number()) + " " +
Expand All @@ -235,9 +239,9 @@ bool print_recognized_error_code(const fc::exception& e, const bool show_error_t
return false;
}

bool print_help_text(const fc::exception& e, const bool show_error_trace) {
bool print_help_text(const fc::exception& e, const bool verbose_errors) {
// Check if the exception has recognized error code
if (print_recognized_error_code(e, show_error_trace)) return true;
if (print_recognized_error_code(e, verbose_errors)) return true;
bool result = false;
// Large input strings to std::regex can cause SIGSEGV, this is a known bug in libstdc++.
// See https://stackoverflow.com/questions/36304204/%D0%A1-regex-segfault-on-long-sequences
Expand Down
2 changes: 1 addition & 1 deletion programs/eosioc/help_text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
#include <fc/exception/exception.hpp>

namespace eosio { namespace client { namespace help {
bool print_help_text(const fc::exception& e, const bool show_error_trace);
bool print_help_text(const fc::exception& e, const bool verbose_errors);
}}}
15 changes: 10 additions & 5 deletions programs/eosioc/httpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,15 @@ fc::variant call( const std::string& server, uint16_t port,
// refer to libraries/chain/include/eosio/chain/exceptions.hpp
if (error.code >= 3000000 && error.code <= 3999999) {
// Construct fc exception from error
const auto &stack_trace = error.stack_trace;
fc::exception new_exception(error.code, error.name, error.message);
new_exception.append_log(fc::log_message(fc::log_context(), error.details));

for (auto & trace : error.stack_trace) {
new_exception.append_log(fc::log_message(trace, std::string()));
if (stack_trace.empty()) {
new_exception.append_log(FC_LOG_MESSAGE(error, error.details));
} else {
for (auto itr = stack_trace.begin(); itr != stack_trace.end(); itr++) {
const auto &error_message = itr == stack_trace.begin() ? error.details : std::string();
new_exception.append_log(fc::log_message(*itr, error_message));
}
}
throw new_exception;
}
Expand All @@ -132,5 +136,6 @@ fc::variant call( const std::string& server, uint16_t port,
}

FC_ASSERT( !"unable to connect" );
} FC_CAPTURE_AND_RETHROW( (server)(port)(path)(postdata) )
} FC_RETHROW_EXCEPTIONS( error, "Request Path: ${server}:${port}${path}\n Request Post Data: ${postdata}}" ,
("server", server)("port", port)("path", path)("postdata", postdata) )
}
4 changes: 1 addition & 3 deletions programs/eosioc/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,6 @@ int main( int argc, char** argv ) {

bool verbose_errors = false;
app.add_flag( "-v,--verbose", verbose_errors, localized("output verbose actions on error"));
bool show_error_trace = false;
app.add_flag( "-t,--error-trace", show_error_trace, localized("output error stack trace"));

auto version = app.add_subcommand("version", localized("Retrieve version information"), false);
version->require_subcommand();
Expand Down Expand Up @@ -1008,7 +1006,7 @@ int main( int argc, char** argv ) {
}
} else {
// attempt to extract the error code if one is present
if (verbose_errors || !print_help_text(e, show_error_trace)) {
if (!print_help_text(e, verbose_errors) && verbose_errors) {
elog("Failed with error: ${e}", ("e", verbose_errors ? e.to_detail_string() : e.to_string()));
}
}
Expand Down

0 comments on commit 47c0458

Please sign in to comment.