comparison 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
comparison
equal deleted inserted replaced
157:5699fca64251 158:0876b5e67afe
185 break; 185 break;
186 } 186 }
187 } 187 }
188 188
189 RequestOrResponse 189 RequestOrResponse
190 readRequestJson(string &err) 190 readRequestJson(string &err, bool &eof)
191 { 191 {
192 RequestOrResponse rr; 192 RequestOrResponse rr;
193 rr.direction = RequestOrResponse::Request; 193 rr.direction = RequestOrResponse::Request;
194 194
195 string input; 195 string input;
196 if (!getline(cin, input)) { 196 if (!getline(cin, input)) {
197 // the EOF case, not actually an error 197 // the EOF case, not actually an error
198 rr.type = RRType::NotValid; 198 eof = true;
199 return rr; 199 return rr;
200 } 200 }
201 201
202 Json j = convertRequestJson(input, err); 202 Json j = convertRequestJson(input, err);
203 if (err != "") return {}; 203 if (err != "") return {};
270 270
271 cout << j.dump() << endl; 271 cout << j.dump() << endl;
272 } 272 }
273 273
274 RequestOrResponse 274 RequestOrResponse
275 readResponseJson(string &err) 275 readResponseJson(string &err, bool &eof)
276 { 276 {
277 RequestOrResponse rr; 277 RequestOrResponse rr;
278 rr.direction = RequestOrResponse::Response; 278 rr.direction = RequestOrResponse::Response;
279 279
280 string input; 280 string input;
281 if (!getline(cin, input)) { 281 if (!getline(cin, input)) {
282 // the EOF case, not actually an error 282 // the EOF case, not actually an error
283 rr.type = RRType::NotValid; 283 eof = true;
284 return rr; 284 return rr;
285 } 285 }
286 286
287 Json j = convertResponseJson(input, err); 287 Json j = convertResponseJson(input, err);
288 if (err != "") return {}; 288 if (err != "") return {};
359 case RRType::Finish: 359 case RRType::Finish:
360 j = VampJson::fromRpcResponse_Finish 360 j = VampJson::fromRpcResponse_Finish
361 (rr.finishResponse, mapper, serialisation, id); 361 (rr.finishResponse, mapper, serialisation, id);
362 break; 362 break;
363 case RRType::NotValid: 363 case RRType::NotValid:
364 j = VampJson::fromError(rr.errorText, rr.type, id);
364 break; 365 break;
365 } 366 }
366 } 367 }
367 368
368 cout << j.dump() << endl; 369 cout << j.dump() << endl;
470 break; 471 break;
471 case RRType::Finish: 472 case RRType::Finish:
472 VampnProto::readRpcResponse_Finish(rr.finishResponse, reader, mapper); 473 VampnProto::readRpcResponse_Finish(rr.finishResponse, reader, mapper);
473 break; 474 break;
474 case RRType::NotValid: 475 case RRType::NotValid:
475 // error
476 rr.success = false;
477 VampnProto::readRpcResponse_Error(errorCode, rr.errorText, reader); 476 VampnProto::readRpcResponse_Error(errorCode, rr.errorText, reader);
478 break; 477 break;
479 } 478 }
480 479
481 return rr; 480 return rr;
511 break; 510 break;
512 case RRType::Finish: 511 case RRType::Finish:
513 VampnProto::buildRpcResponse_Finish(builder, rr.finishResponse, mapper); 512 VampnProto::buildRpcResponse_Finish(builder, rr.finishResponse, mapper);
514 break; 513 break;
515 case RRType::NotValid: 514 case RRType::NotValid:
515 VampnProto::buildRpcResponse_Error(builder, rr.errorText, rr.type);
516 break; 516 break;
517 } 517 }
518 } 518 }
519 519
520 writeMessageToFd(1, message); 520 writeMessageToFd(1, message);
521 } 521 }
522 522
523 RequestOrResponse 523 RequestOrResponse
524 readInputJson(RequestOrResponse::Direction direction, string &err) 524 readInputJson(RequestOrResponse::Direction direction, string &err, bool &eof)
525 { 525 {
526 if (direction == RequestOrResponse::Request) { 526 if (direction == RequestOrResponse::Request) {
527 return readRequestJson(err); 527 return readRequestJson(err, eof);
528 } else { 528 } else {
529 return readResponseJson(err); 529 return readResponseJson(err, eof);
530 } 530 }
531 } 531 }
532 532
533 RequestOrResponse 533 RequestOrResponse
534 readInputCapnp(RequestOrResponse::Direction direction) 534 readInputCapnp(RequestOrResponse::Direction direction, bool &eof)
535 { 535 {
536 static kj::FdInputStream stream(0); // stdin 536 static kj::FdInputStream stream(0); // stdin
537 static kj::BufferedInputStreamWrapper buffered(stream); 537 static kj::BufferedInputStreamWrapper buffered(stream);
538 538
539 if (buffered.tryGetReadBuffer() == nullptr) { 539 if (buffered.tryGetReadBuffer() == nullptr) {
540 eof = true;
540 return {}; 541 return {};
541 } 542 }
542 543
543 if (direction == RequestOrResponse::Request) { 544 if (direction == RequestOrResponse::Request) {
544 return readRequestCapnp(buffered); 545 return readRequestCapnp(buffered);
546 return readResponseCapnp(buffered); 547 return readResponseCapnp(buffered);
547 } 548 }
548 } 549 }
549 550
550 RequestOrResponse 551 RequestOrResponse
551 readInput(string format, RequestOrResponse::Direction direction) 552 readInput(string format, RequestOrResponse::Direction direction, bool &eof)
552 { 553 {
554 eof = false;
555
553 if (format == "json") { 556 if (format == "json") {
554 string err; 557 string err;
555 auto result = readInputJson(direction, err); 558 auto result = readInputJson(direction, err, eof);
556 if (err != "") throw runtime_error(err); 559 if (err != "") throw runtime_error(err);
557 else return result; 560 else return result;
558 } else if (format == "capnp") { 561 } else if (format == "capnp") {
559 return readInputCapnp(direction); 562 return readInputCapnp(direction, eof);
560 } else { 563 } else {
561 throw runtime_error("unknown input format \"" + format + "\""); 564 throw runtime_error("unknown input format \"" + format + "\"");
562 } 565 }
563 } 566 }
564 567
647 650
648 while (true) { 651 while (true) {
649 652
650 try { 653 try {
651 654
652 RequestOrResponse rr = readInput(informat, direction); 655 bool eof = false;
653 656 RequestOrResponse rr = readInput(informat, direction, eof);
654 // NotValid without an exception indicates EOF: 657 if (eof) break;
655 if (rr.type == RRType::NotValid) break;
656 658
657 writeOutput(outformat, rr); 659 writeOutput(outformat, rr);
658 660
659 } catch (std::exception &e) { 661 } catch (std::exception &e) {
660 662
661 cerr << "Error: " << e.what() << endl; 663 cerr << "Error: " << e.what() << endl;
662 exit(1); 664 exit(1);
663 } 665 }