# HG changeset patch # User Lucas Thompson # Date 1485903412 0 # Node ID 60dc013bd69ca7f54ba2f196093424fbe5721208 # Parent 7532233f8e49a96e86368e03d30f99f5a7ab9e6f Fix handling of parsing json error responses from a server, and allow for re-writing them without changing the error message further. diff -r 7532233f8e49 -r 60dc013bd69c vamp-json/VampJson.h --- a/vamp-json/VampJson.h Tue Jan 31 22:54:08 2017 +0000 +++ b/vamp-json/VampJson.h Tue Jan 31 22:56:52 2017 +0000 @@ -1298,7 +1298,8 @@ static json11::Json fromError(std::string errorText, RRType responseType, - const json11::Json &id) { + const json11::Json &id, + bool writeVerbatimError = false) { json11::Json::object jo; markRPC(jo); @@ -1315,7 +1316,7 @@ json11::Json::object eo; eo["code"] = 0; - if (responseType == RRType::NotValid) { + if (responseType == RRType::NotValid || writeVerbatimError) { eo["message"] = errorText; } else { eo["message"] = diff -r 7532233f8e49 -r 60dc013bd69c vamp-server/convert.cpp --- a/vamp-server/convert.cpp Tue Jan 31 22:54:08 2017 +0000 +++ b/vamp-server/convert.cpp Tue Jan 31 22:56:52 2017 +0000 @@ -337,11 +337,11 @@ Json id = writeJsonId(rr.id); if (!rr.success) { - - j = VampJson::fromError(rr.errorText, rr.type, id); + // errorText here likely contains a full message produced by simple-server + // setting writeVerbatimError to true avoids doubling error descriptions + j = VampJson::fromError(rr.errorText, rr.type, id, true); } else { - switch (rr.type) { case RRType::List: @@ -558,8 +558,15 @@ if (format == "json") { string err; auto result = readInputJson(direction, err, eof); - if (err != "") throw runtime_error(err); - else return result; + const bool isRecognisedError = !result.success && result.errorText != ""; + + // if the RequestOrResponse (result) has been populated with success=false and an error message + // then the server returned a well formed error, it is safe to return it for conversion + // -- but if err is populated, something else has gone wrong + if (isRecognisedError || err == "") + return result; + else + throw runtime_error(err); } else if (format == "capnp") { return readInputCapnp(direction, eof); } else { @@ -661,7 +668,6 @@ writeOutput(outformat, rr); } catch (std::exception &e) { - cerr << "Error: " << e.what() << endl; exit(1); }