Mercurial > hg > piper-cpp
diff vamp-server/convert.cpp @ 158:0876b5e67afe
Improve error handling and extend tests for it
author | Chris Cannam <cannam@all-day-breakfast.com> |
---|---|
date | Fri, 20 Jan 2017 22:24:44 +0000 |
parents | e558e042d9c7 |
children | 59c89b0e9375 |
line wrap: on
line diff
--- a/vamp-server/convert.cpp Fri Jan 20 22:24:11 2017 +0000 +++ b/vamp-server/convert.cpp Fri Jan 20 22:24:44 2017 +0000 @@ -187,7 +187,7 @@ } RequestOrResponse -readRequestJson(string &err) +readRequestJson(string &err, bool &eof) { RequestOrResponse rr; rr.direction = RequestOrResponse::Request; @@ -195,7 +195,7 @@ string input; if (!getline(cin, input)) { // the EOF case, not actually an error - rr.type = RRType::NotValid; + eof = true; return rr; } @@ -272,7 +272,7 @@ } RequestOrResponse -readResponseJson(string &err) +readResponseJson(string &err, bool &eof) { RequestOrResponse rr; rr.direction = RequestOrResponse::Response; @@ -280,7 +280,7 @@ string input; if (!getline(cin, input)) { // the EOF case, not actually an error - rr.type = RRType::NotValid; + eof = true; return rr; } @@ -361,6 +361,7 @@ (rr.finishResponse, mapper, serialisation, id); break; case RRType::NotValid: + j = VampJson::fromError(rr.errorText, rr.type, id); break; } } @@ -472,8 +473,6 @@ VampnProto::readRpcResponse_Finish(rr.finishResponse, reader, mapper); break; case RRType::NotValid: - // error - rr.success = false; VampnProto::readRpcResponse_Error(errorCode, rr.errorText, reader); break; } @@ -513,6 +512,7 @@ VampnProto::buildRpcResponse_Finish(builder, rr.finishResponse, mapper); break; case RRType::NotValid: + VampnProto::buildRpcResponse_Error(builder, rr.errorText, rr.type); break; } } @@ -521,22 +521,23 @@ } RequestOrResponse -readInputJson(RequestOrResponse::Direction direction, string &err) +readInputJson(RequestOrResponse::Direction direction, string &err, bool &eof) { if (direction == RequestOrResponse::Request) { - return readRequestJson(err); + return readRequestJson(err, eof); } else { - return readResponseJson(err); + return readResponseJson(err, eof); } } RequestOrResponse -readInputCapnp(RequestOrResponse::Direction direction) +readInputCapnp(RequestOrResponse::Direction direction, bool &eof) { static kj::FdInputStream stream(0); // stdin static kj::BufferedInputStreamWrapper buffered(stream); if (buffered.tryGetReadBuffer() == nullptr) { + eof = true; return {}; } @@ -548,15 +549,17 @@ } RequestOrResponse -readInput(string format, RequestOrResponse::Direction direction) +readInput(string format, RequestOrResponse::Direction direction, bool &eof) { + eof = false; + if (format == "json") { string err; - auto result = readInputJson(direction, err); + auto result = readInputJson(direction, err, eof); if (err != "") throw runtime_error(err); else return result; } else if (format == "capnp") { - return readInputCapnp(direction); + return readInputCapnp(direction, eof); } else { throw runtime_error("unknown input format \"" + format + "\""); } @@ -649,13 +652,12 @@ try { - RequestOrResponse rr = readInput(informat, direction); - - // NotValid without an exception indicates EOF: - if (rr.type == RRType::NotValid) break; + bool eof = false; + RequestOrResponse rr = readInput(informat, direction, eof); + if (eof) break; writeOutput(outformat, rr); - + } catch (std::exception &e) { cerr << "Error: " << e.what() << endl;