comparison OrthancServer/OrthancRestApi/OrthancRestAnonymizeModify.cpp @ 1582:bd1889029cbb

encoding of exceptions
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 25 Aug 2015 17:39:38 +0200
parents 904096e7367e
children 644c32c07306
comparison
equal deleted inserted replaced
1581:357c4bb15701 1582:bd1889029cbb
313 **/ 313 **/
314 314
315 std::string modifiedInstance; 315 std::string modifiedInstance;
316 if (context.Store(modifiedInstance, toStore) != StoreStatus_Success) 316 if (context.Store(modifiedInstance, toStore) != StoreStatus_Success)
317 { 317 {
318 throw OrthancException("Error while storing a modified instance " + *it); 318 LOG(ERROR) << "Error while storing a modified instance " << *it;
319 throw OrthancException(ErrorCode_CannotStoreInstance);
319 } 320 }
320 321
321 // Sanity checks in debug mode 322 // Sanity checks in debug mode
322 assert(modifiedInstance == modifiedHasher.HashInstance()); 323 assert(modifiedInstance == modifiedHasher.HashInstance());
323 324
442 ServerContext& context = OrthancRestApi::GetContext(call); 443 ServerContext& context = OrthancRestApi::GetContext(call);
443 StoreStatus status = context.Store(id, toStore); 444 StoreStatus status = context.Store(id, toStore);
444 445
445 if (status == StoreStatus_Failure) 446 if (status == StoreStatus_Failure)
446 { 447 {
447 throw OrthancException("Error while storing a manually-created instance"); 448 throw OrthancException(ErrorCode_CannotStoreInstance);
448 } 449 }
449 } 450 }
450 451
451 452
452 static void CreateDicomV1(ParsedDicomFile& dicom, 453 static void CreateDicomV1(ParsedDicomFile& dicom,
463 for (size_t i = 0; i < members.size(); i++) 464 for (size_t i = 0; i < members.size(); i++)
464 { 465 {
465 const std::string& name = members[i]; 466 const std::string& name = members[i];
466 if (request[name].type() != Json::stringValue) 467 if (request[name].type() != Json::stringValue)
467 { 468 {
468 throw OrthancException("Only string values are supported when creating DICOM instances"); 469 throw OrthancException(ErrorCode_CreateDicomNotString);
469 } 470 }
470 471
471 std::string value = request[name].asString(); 472 std::string value = request[name].asString();
472 473
473 DicomTag tag = FromDcmtkBridge::ParseTag(name); 474 DicomTag tag = FromDcmtkBridge::ParseTag(name);
486 static void InjectTags(ParsedDicomFile& dicom, 487 static void InjectTags(ParsedDicomFile& dicom,
487 const Json::Value& tags) 488 const Json::Value& tags)
488 { 489 {
489 if (tags.type() != Json::objectValue) 490 if (tags.type() != Json::objectValue)
490 { 491 {
491 throw OrthancException("Bad syntax to specify the tags"); 492 throw OrthancException(ErrorCode_BadRequest);
492 } 493 }
493 494
494 // Inject the user-specified tags 495 // Inject the user-specified tags
495 Json::Value::Members members = tags.getMemberNames(); 496 Json::Value::Members members = tags.getMemberNames();
496 for (size_t i = 0; i < members.size(); i++) 497 for (size_t i = 0; i < members.size(); i++)
497 { 498 {
498 const std::string& name = members[i]; 499 const std::string& name = members[i];
499 if (tags[name].type() != Json::stringValue) 500 if (tags[name].type() != Json::stringValue)
500 { 501 {
501 throw OrthancException("Only string values are supported when creating DICOM instances"); 502 throw OrthancException(ErrorCode_CreateDicomNotString);
502 } 503 }
503 504
504 std::string value = tags[name].asString(); 505 std::string value = tags[name].asString();
505 506
506 DicomTag tag = FromDcmtkBridge::ParseTag(name); 507 DicomTag tag = FromDcmtkBridge::ParseTag(name);
507 if (tag != DICOM_TAG_SPECIFIC_CHARACTER_SET) 508 if (tag != DICOM_TAG_SPECIFIC_CHARACTER_SET)
508 { 509 {
509 if (tag != DICOM_TAG_PATIENT_ID && 510 if (tag != DICOM_TAG_PATIENT_ID &&
510 dicom.HasTag(tag)) 511 dicom.HasTag(tag))
511 { 512 {
512 throw OrthancException("Trying to override a value inherited from a parent module"); 513 throw OrthancException(ErrorCode_CreateDicomOverrideTag);
513 } 514 }
514 515
515 if (tag == DICOM_TAG_PIXEL_DATA) 516 if (tag == DICOM_TAG_PIXEL_DATA)
516 { 517 {
517 throw OrthancException("Use \"Content\" to inject an image into a new DICOM instance"); 518 throw OrthancException(ErrorCode_CreateDicomUseContent);
518 } 519 }
519 else 520 else
520 { 521 {
521 dicom.Replace(tag, Toolbox::ConvertFromUtf8(value, dicom.GetEncoding())); 522 dicom.Replace(tag, Toolbox::ConvertFromUtf8(value, dicom.GetEncoding()));
522 } 523 }
551 } 552 }
552 else if (content[i].type() == Json::objectValue) 553 else if (content[i].type() == Json::objectValue)
553 { 554 {
554 if (!content[i].isMember("Content")) 555 if (!content[i].isMember("Content"))
555 { 556 {
556 throw OrthancException("No payload is present for one instance in the series"); 557 throw OrthancException(ErrorCode_CreateDicomNoPayload);
557 } 558 }
558 559
559 payload = &content[i]["Content"]; 560 payload = &content[i]["Content"];
560 561
561 if (content[i].isMember("Tags")) 562 if (content[i].isMember("Tags"))
565 } 566 }
566 567
567 if (payload == NULL || 568 if (payload == NULL ||
568 payload->type() != Json::stringValue) 569 payload->type() != Json::stringValue)
569 { 570 {
570 throw OrthancException("The payload of the DICOM instance must be specified according to Data URI scheme"); 571 throw OrthancException(ErrorCode_CreateDicomUseDataUriScheme);
571 } 572 }
572 573
573 dicom->EmbedContent(payload->asString()); 574 dicom->EmbedContent(payload->asString());
574 dicom->Replace(DICOM_TAG_INSTANCE_NUMBER, boost::lexical_cast<std::string>(i + 1)); 575 dicom->Replace(DICOM_TAG_INSTANCE_NUMBER, boost::lexical_cast<std::string>(i + 1));
575 dicom->Replace(DICOM_TAG_IMAGE_INDEX, boost::lexical_cast<std::string>(i + 1)); 576 dicom->Replace(DICOM_TAG_IMAGE_INDEX, boost::lexical_cast<std::string>(i + 1));
619 if (request["Tags"].isMember("SpecificCharacterSet")) 620 if (request["Tags"].isMember("SpecificCharacterSet"))
620 { 621 {
621 const char* tmp = request["Tags"]["SpecificCharacterSet"].asCString(); 622 const char* tmp = request["Tags"]["SpecificCharacterSet"].asCString();
622 if (!GetDicomEncoding(encoding, tmp)) 623 if (!GetDicomEncoding(encoding, tmp))
623 { 624 {
624 throw OrthancException("Unknown specific character set: " + std::string(tmp)); 625 LOG(ERROR) << "Unknown specific character set: " << std::string(tmp);
626 throw OrthancException(ErrorCode_ParameterOutOfRange);
625 } 627 }
626 } 628 }
627 else 629 else
628 { 630 {
629 std::string tmp = Configuration::GetGlobalStringParameter("DefaultEncoding", "Latin1"); 631 std::string tmp = Configuration::GetGlobalStringParameter("DefaultEncoding", "Latin1");
639 { 641 {
640 // Locate the parent tags 642 // Locate the parent tags
641 std::string parent = request["Parent"].asString(); 643 std::string parent = request["Parent"].asString();
642 if (!context.GetIndex().LookupResourceType(parentType, parent)) 644 if (!context.GetIndex().LookupResourceType(parentType, parent))
643 { 645 {
644 throw OrthancException("Trying to attach a new DICOM instance to an inexistent resource: " + parent); 646 throw OrthancException(ErrorCode_CreateDicomBadParent);
645 } 647 }
646 648
647 if (parentType == ResourceType_Instance) 649 if (parentType == ResourceType_Instance)
648 { 650 {
649 throw OrthancException("Trying to attach a new DICOM instance to an instance (must be a series, study or patient): " + parent); 651 throw OrthancException(ErrorCode_CreateDicomParentIsInstance);
650 } 652 }
651 653
652 // Select one existing child instance of the parent resource, to 654 // Select one existing child instance of the parent resource, to
653 // retrieve all its tags 655 // retrieve all its tags
654 Json::Value siblingTags; 656 Json::Value siblingTags;
677 Encoding encoding; 679 Encoding encoding;
678 if (!siblingTags[SPECIFIC_CHARACTER_SET].isMember("Value") || 680 if (!siblingTags[SPECIFIC_CHARACTER_SET].isMember("Value") ||
679 siblingTags[SPECIFIC_CHARACTER_SET]["Value"].type() != Json::stringValue || 681 siblingTags[SPECIFIC_CHARACTER_SET]["Value"].type() != Json::stringValue ||
680 !GetDicomEncoding(encoding, siblingTags[SPECIFIC_CHARACTER_SET]["Value"].asCString())) 682 !GetDicomEncoding(encoding, siblingTags[SPECIFIC_CHARACTER_SET]["Value"].asCString()))
681 { 683 {
682 throw OrthancException("Unable to get the encoding of the parent resource"); 684 throw OrthancException(ErrorCode_CreateDicomParentEncoding);
683 } 685 }
684 686
685 dicom.SetEncoding(encoding); 687 dicom.SetEncoding(encoding);
686 } 688 }
687 } 689 }
781 return; 783 return;
782 } 784 }
783 } 785 }
784 else 786 else
785 { 787 {
786 throw OrthancException("The payload of the DICOM instance must be specified according to Data URI scheme"); 788 throw OrthancException(ErrorCode_CreateDicomUseDataUriScheme);
787 return; 789 return;
788 } 790 }
789 } 791 }
790 792
791 std::string id; 793 std::string id;