Mercurial > hg > piper-cpp
comparison vamp-server/convert.cpp @ 176:60dc013bd69c
Fix handling of parsing json error responses from a server, and allow for re-writing them without changing the error message further.
author | Lucas Thompson <dev@lucas.im> |
---|---|
date | Tue, 31 Jan 2017 22:56:52 +0000 |
parents | 59c89b0e9375 |
children | bd543e74a9bf |
comparison
equal
deleted
inserted
replaced
175:7532233f8e49 | 176:60dc013bd69c |
---|---|
335 VampJson::BufferSerialisation::Array); | 335 VampJson::BufferSerialisation::Array); |
336 | 336 |
337 Json id = writeJsonId(rr.id); | 337 Json id = writeJsonId(rr.id); |
338 | 338 |
339 if (!rr.success) { | 339 if (!rr.success) { |
340 | 340 // errorText here likely contains a full message produced by simple-server |
341 j = VampJson::fromError(rr.errorText, rr.type, id); | 341 // setting writeVerbatimError to true avoids doubling error descriptions |
342 | 342 j = VampJson::fromError(rr.errorText, rr.type, id, true); |
343 } else { | 343 |
344 | 344 } else { |
345 switch (rr.type) { | 345 switch (rr.type) { |
346 | 346 |
347 case RRType::List: | 347 case RRType::List: |
348 j = VampJson::fromRpcResponse_List(rr.listResponse, id); | 348 j = VampJson::fromRpcResponse_List(rr.listResponse, id); |
349 break; | 349 break; |
556 eof = false; | 556 eof = false; |
557 | 557 |
558 if (format == "json") { | 558 if (format == "json") { |
559 string err; | 559 string err; |
560 auto result = readInputJson(direction, err, eof); | 560 auto result = readInputJson(direction, err, eof); |
561 if (err != "") throw runtime_error(err); | 561 const bool isRecognisedError = !result.success && result.errorText != ""; |
562 else return result; | 562 |
563 // if the RequestOrResponse (result) has been populated with success=false and an error message | |
564 // then the server returned a well formed error, it is safe to return it for conversion | |
565 // -- but if err is populated, something else has gone wrong | |
566 if (isRecognisedError || err == "") | |
567 return result; | |
568 else | |
569 throw runtime_error(err); | |
563 } else if (format == "capnp") { | 570 } else if (format == "capnp") { |
564 return readInputCapnp(direction, eof); | 571 return readInputCapnp(direction, eof); |
565 } else { | 572 } else { |
566 throw runtime_error("unknown input format \"" + format + "\""); | 573 throw runtime_error("unknown input format \"" + format + "\""); |
567 } | 574 } |
659 if (eof) break; | 666 if (eof) break; |
660 | 667 |
661 writeOutput(outformat, rr); | 668 writeOutput(outformat, rr); |
662 | 669 |
663 } catch (std::exception &e) { | 670 } catch (std::exception &e) { |
664 | |
665 cerr << "Error: " << e.what() << endl; | 671 cerr << "Error: " << e.what() << endl; |
666 exit(1); | 672 exit(1); |
667 } | 673 } |
668 } | 674 } |
669 | 675 |