comparison Plugins/Samples/Common/OrthancPluginCppWrapper.cpp @ 2234:a78d15509a1c

cleaner separation of PluginException
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 16 Dec 2016 14:35:35 +0100
parents 61e0564d03bb
children a3a65de1840f
comparison
equal deleted inserted replaced
2232:3dd44baebc36 2234:a78d15509a1c
36 #include <json/writer.h> 36 #include <json/writer.h>
37 37
38 38
39 namespace OrthancPlugins 39 namespace OrthancPlugins
40 { 40 {
41 const char* GetErrorDescription(OrthancPluginContext* context,
42 OrthancPluginErrorCode code)
43 {
44 const char* description = OrthancPluginGetErrorDescription(context, code);
45 if (description)
46 {
47 return description;
48 }
49 else
50 {
51 return "No description available";
52 }
53 }
54
55
56 #if HAS_ORTHANC_EXCEPTION == 0
57 void PluginException::Check(OrthancPluginErrorCode code)
58 {
59 if (code != OrthancPluginErrorCode_Success)
60 {
61 ORTHANC_PLUGINS_THROW_EXCEPTION(code);
62 }
63 }
64 #endif
65
66
67 void MemoryBuffer::Check(OrthancPluginErrorCode code) 41 void MemoryBuffer::Check(OrthancPluginErrorCode code)
68 { 42 {
69 if (code != OrthancPluginErrorCode_Success) 43 if (code != OrthancPluginErrorCode_Success)
70 { 44 {
71 // Prevent using garbage information 45 // Prevent using garbage information
72 buffer_.data = NULL; 46 buffer_.data = NULL;
73 buffer_.size = 0; 47 buffer_.size = 0;
74 ORTHANC_PLUGINS_THROW_EXCEPTION(code); 48 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(code);
75 } 49 }
76 } 50 }
77 51
78 52
79 MemoryBuffer::MemoryBuffer(OrthancPluginContext* context) : 53 MemoryBuffer::MemoryBuffer(OrthancPluginContext* context) :
123 void MemoryBuffer::ToJson(Json::Value& target) const 97 void MemoryBuffer::ToJson(Json::Value& target) const
124 { 98 {
125 if (buffer_.data == NULL || 99 if (buffer_.data == NULL ||
126 buffer_.size == 0) 100 buffer_.size == 0)
127 { 101 {
128 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_InternalError); 102 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
129 } 103 }
130 104
131 const char* tmp = reinterpret_cast<const char*>(buffer_.data); 105 const char* tmp = reinterpret_cast<const char*>(buffer_.data);
132 106
133 Json::Reader reader; 107 Json::Reader reader;
134 if (!reader.parse(tmp, tmp + buffer_.size, target)) 108 if (!reader.parse(tmp, tmp + buffer_.size, target))
135 { 109 {
136 OrthancPluginLogError(context_, "Cannot convert some memory buffer to JSON"); 110 OrthancPluginLogError(context_, "Cannot convert some memory buffer to JSON");
137 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat); 111 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
138 } 112 }
139 } 113 }
140 114
141 115
142 bool MemoryBuffer::RestApiGet(const std::string& uri, 116 bool MemoryBuffer::RestApiGet(const std::string& uri,
164 { 138 {
165 return false; 139 return false;
166 } 140 }
167 else 141 else
168 { 142 {
169 ORTHANC_PLUGINS_THROW_EXCEPTION(error); 143 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(error);
170 } 144 }
171 } 145 }
172 146
173 147
174 bool MemoryBuffer::RestApiPost(const std::string& uri, 148 bool MemoryBuffer::RestApiPost(const std::string& uri,
198 { 172 {
199 return false; 173 return false;
200 } 174 }
201 else 175 else
202 { 176 {
203 ORTHANC_PLUGINS_THROW_EXCEPTION(error); 177 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(error);
204 } 178 }
205 } 179 }
206 180
207 181
208 bool MemoryBuffer::RestApiPut(const std::string& uri, 182 bool MemoryBuffer::RestApiPut(const std::string& uri,
232 { 206 {
233 return false; 207 return false;
234 } 208 }
235 else 209 else
236 { 210 {
237 ORTHANC_PLUGINS_THROW_EXCEPTION(error); 211 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(error);
238 } 212 }
239 } 213 }
240 214
241 215
242 bool MemoryBuffer::RestApiPost(const std::string& uri, 216 bool MemoryBuffer::RestApiPost(const std::string& uri,
285 259
286 void OrthancString::Assign(char* str) 260 void OrthancString::Assign(char* str)
287 { 261 {
288 if (str == NULL) 262 if (str == NULL)
289 { 263 {
290 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_InternalError); 264 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
291 } 265 }
292 else 266 else
293 { 267 {
294 Clear(); 268 Clear();
295 str_ = str; 269 str_ = str;
323 void OrthancString::ToJson(Json::Value& target) const 297 void OrthancString::ToJson(Json::Value& target) const
324 { 298 {
325 if (str_ == NULL) 299 if (str_ == NULL)
326 { 300 {
327 OrthancPluginLogError(context_, "Cannot convert an empty memory buffer to JSON"); 301 OrthancPluginLogError(context_, "Cannot convert an empty memory buffer to JSON");
328 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_InternalError); 302 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
329 } 303 }
330 304
331 Json::Reader reader; 305 Json::Reader reader;
332 if (!reader.parse(str_, target)) 306 if (!reader.parse(str_, target))
333 { 307 {
334 OrthancPluginLogError(context_, "Cannot convert some memory buffer to JSON"); 308 OrthancPluginLogError(context_, "Cannot convert some memory buffer to JSON");
335 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat); 309 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
336 } 310 }
337 } 311 }
338 312
339 313
340 void MemoryBuffer::DicomToJson(Json::Value& target, 314 void MemoryBuffer::DicomToJson(Json::Value& target,
356 str.Assign(OrthancPluginGetConfiguration(context)); 330 str.Assign(OrthancPluginGetConfiguration(context));
357 331
358 if (str.GetContent() == NULL) 332 if (str.GetContent() == NULL)
359 { 333 {
360 OrthancPluginLogError(context, "Cannot access the Orthanc configuration"); 334 OrthancPluginLogError(context, "Cannot access the Orthanc configuration");
361 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_InternalError); 335 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
362 } 336 }
363 337
364 str.ToJson(configuration_); 338 str.ToJson(configuration_);
365 339
366 if (configuration_.type() != Json::objectValue) 340 if (configuration_.type() != Json::objectValue)
367 { 341 {
368 OrthancPluginLogError(context, "Unable to read the Orthanc configuration"); 342 OrthancPluginLogError(context, "Unable to read the Orthanc configuration");
369 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_InternalError); 343 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
370 } 344 }
371 } 345 }
372 346
373 347
374 OrthancPluginContext* OrthancConfiguration::GetContext() const 348 OrthancPluginContext* OrthancConfiguration::GetContext() const
375 { 349 {
376 if (context_ == NULL) 350 if (context_ == NULL)
377 { 351 {
378 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_Plugin); 352 ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin);
379 } 353 }
380 else 354 else
381 { 355 {
382 return context_; 356 return context_;
383 } 357 }
426 { 400 {
427 std::string s = "The configuration section \"" + target.path_ + "\" is not an associative array as expected"; 401 std::string s = "The configuration section \"" + target.path_ + "\" is not an associative array as expected";
428 OrthancPluginLogError(context_, s.c_str()); 402 OrthancPluginLogError(context_, s.c_str());
429 } 403 }
430 404
431 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat); 405 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
432 } 406 }
433 407
434 target.configuration_ = configuration_[key]; 408 target.configuration_ = configuration_[key];
435 } 409 }
436 } 410 }
452 { 426 {
453 std::string s = "The configuration option \"" + GetPath(key) + "\" is not a string as expected"; 427 std::string s = "The configuration option \"" + GetPath(key) + "\" is not a string as expected";
454 OrthancPluginLogError(context_, s.c_str()); 428 OrthancPluginLogError(context_, s.c_str());
455 } 429 }
456 430
457 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat); 431 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
458 } 432 }
459 433
460 target = configuration_[key].asString(); 434 target = configuration_[key].asString();
461 return true; 435 return true;
462 } 436 }
487 { 461 {
488 std::string s = "The configuration option \"" + GetPath(key) + "\" is not an integer as expected"; 462 std::string s = "The configuration option \"" + GetPath(key) + "\" is not an integer as expected";
489 OrthancPluginLogError(context_, s.c_str()); 463 OrthancPluginLogError(context_, s.c_str());
490 } 464 }
491 465
492 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat); 466 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
493 } 467 }
494 } 468 }
495 469
496 470
497 bool OrthancConfiguration::LookupUnsignedIntegerValue(unsigned int& target, 471 bool OrthancConfiguration::LookupUnsignedIntegerValue(unsigned int& target,
509 { 483 {
510 std::string s = "The configuration option \"" + GetPath(key) + "\" is not a positive integer as expected"; 484 std::string s = "The configuration option \"" + GetPath(key) + "\" is not a positive integer as expected";
511 OrthancPluginLogError(context_, s.c_str()); 485 OrthancPluginLogError(context_, s.c_str());
512 } 486 }
513 487
514 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat); 488 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
515 } 489 }
516 else 490 else
517 { 491 {
518 target = static_cast<unsigned int>(tmp); 492 target = static_cast<unsigned int>(tmp);
519 return true; 493 return true;
537 { 511 {
538 std::string s = "The configuration option \"" + GetPath(key) + "\" is not a Boolean as expected"; 512 std::string s = "The configuration option \"" + GetPath(key) + "\" is not a Boolean as expected";
539 OrthancPluginLogError(context_, s.c_str()); 513 OrthancPluginLogError(context_, s.c_str());
540 } 514 }
541 515
542 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat); 516 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
543 } 517 }
544 518
545 target = configuration_[key].asBool(); 519 target = configuration_[key].asBool();
546 return true; 520 return true;
547 } 521 }
576 { 550 {
577 std::string s = "The configuration option \"" + GetPath(key) + "\" is not an integer as expected"; 551 std::string s = "The configuration option \"" + GetPath(key) + "\" is not an integer as expected";
578 OrthancPluginLogError(context_, s.c_str()); 552 OrthancPluginLogError(context_, s.c_str());
579 } 553 }
580 554
581 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat); 555 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
582 } 556 }
583 } 557 }
584 558
585 559
586 std::string OrthancConfiguration::GetStringValue(const std::string& key, 560 std::string OrthancConfiguration::GetStringValue(const std::string& key,
671 void OrthancImage::CheckImageAvailable() 645 void OrthancImage::CheckImageAvailable()
672 { 646 {
673 if (image_ == NULL) 647 if (image_ == NULL)
674 { 648 {
675 OrthancPluginLogError(context_, "Trying to access a NULL image"); 649 OrthancPluginLogError(context_, "Trying to access a NULL image");
676 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange); 650 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
677 } 651 }
678 } 652 }
679 653
680 654
681 OrthancImage::OrthancImage(OrthancPluginContext* context) : 655 OrthancImage::OrthancImage(OrthancPluginContext* context) :
682 context_(context), 656 context_(context),
683 image_(NULL) 657 image_(NULL)
684 { 658 {
685 if (context == NULL) 659 if (context == NULL)
686 { 660 {
687 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange); 661 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
688 } 662 }
689 } 663 }
690 664
691 665
692 OrthancImage::OrthancImage(OrthancPluginContext* context, 666 OrthancImage::OrthancImage(OrthancPluginContext* context,
694 context_(context), 668 context_(context),
695 image_(image) 669 image_(image)
696 { 670 {
697 if (context == NULL) 671 if (context == NULL)
698 { 672 {
699 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange); 673 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
700 } 674 }
701 } 675 }
702 676
703 677
704 OrthancImage::OrthancImage(OrthancPluginContext* context, 678 OrthancImage::OrthancImage(OrthancPluginContext* context,
707 uint32_t height) : 681 uint32_t height) :
708 context_(context) 682 context_(context)
709 { 683 {
710 if (context == NULL) 684 if (context == NULL)
711 { 685 {
712 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange); 686 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
713 } 687 }
714 else 688 else
715 { 689 {
716 image_ = OrthancPluginCreateImage(context, format, width, height); 690 image_ = OrthancPluginCreateImage(context, format, width, height);
717 } 691 }
724 Clear(); 698 Clear();
725 image_ = OrthancPluginUncompressImage(context_, data, size, OrthancPluginImageFormat_Png); 699 image_ = OrthancPluginUncompressImage(context_, data, size, OrthancPluginImageFormat_Png);
726 if (image_ == NULL) 700 if (image_ == NULL)
727 { 701 {
728 OrthancPluginLogError(context_, "Cannot uncompress a PNG image"); 702 OrthancPluginLogError(context_, "Cannot uncompress a PNG image");
729 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange); 703 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
730 } 704 }
731 } 705 }
732 706
733 707
734 void OrthancImage::UncompressJpegImage(const void* data, 708 void OrthancImage::UncompressJpegImage(const void* data,
737 Clear(); 711 Clear();
738 image_ = OrthancPluginUncompressImage(context_, data, size, OrthancPluginImageFormat_Jpeg); 712 image_ = OrthancPluginUncompressImage(context_, data, size, OrthancPluginImageFormat_Jpeg);
739 if (image_ == NULL) 713 if (image_ == NULL)
740 { 714 {
741 OrthancPluginLogError(context_, "Cannot uncompress a JPEG image"); 715 OrthancPluginLogError(context_, "Cannot uncompress a JPEG image");
742 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange); 716 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
743 } 717 }
744 } 718 }
745 719
746 720
747 void OrthancImage::DecodeDicomImage(const void* data, 721 void OrthancImage::DecodeDicomImage(const void* data,
751 Clear(); 725 Clear();
752 image_ = OrthancPluginDecodeDicomImage(context_, data, size, frame); 726 image_ = OrthancPluginDecodeDicomImage(context_, data, size, frame);
753 if (image_ == NULL) 727 if (image_ == NULL)
754 { 728 {
755 OrthancPluginLogError(context_, "Cannot uncompress a DICOM image"); 729 OrthancPluginLogError(context_, "Cannot uncompress a DICOM image");
756 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange); 730 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
757 } 731 }
758 } 732 }
759 733
760 734
761 OrthancPluginPixelFormat OrthancImage::GetPixelFormat() 735 OrthancPluginPixelFormat OrthancImage::GetPixelFormat()
843 matcher_(NULL), 817 matcher_(NULL),
844 worklist_(worklist) 818 worklist_(worklist)
845 { 819 {
846 if (worklist_ == NULL) 820 if (worklist_ == NULL)
847 { 821 {
848 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange); 822 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
849 } 823 }
850 } 824 }
851 825
852 826
853 void FindMatcher::SetupDicom(OrthancPluginContext* context, 827 void FindMatcher::SetupDicom(OrthancPluginContext* context,
858 worklist_ = NULL; 832 worklist_ = NULL;
859 833
860 matcher_ = OrthancPluginCreateFindMatcher(context_, query, size); 834 matcher_ = OrthancPluginCreateFindMatcher(context_, query, size);
861 if (matcher_ == NULL) 835 if (matcher_ == NULL)
862 { 836 {
863 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_InternalError); 837 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
864 } 838 }
865 } 839 }
866 840
867 841
868 FindMatcher::~FindMatcher() 842 FindMatcher::~FindMatcher()
890 { 864 {
891 result = OrthancPluginWorklistIsMatch(context_, worklist_, dicom, size); 865 result = OrthancPluginWorklistIsMatch(context_, worklist_, dicom, size);
892 } 866 }
893 else 867 else
894 { 868 {
895 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_InternalError); 869 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
896 } 870 }
897 871
898 if (result == 0) 872 if (result == 0)
899 { 873 {
900 return false; 874 return false;
903 { 877 {
904 return true; 878 return true;
905 } 879 }
906 else 880 else
907 { 881 {
908 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_InternalError); 882 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
909 } 883 }
910 } 884 }
911 885
912 #endif /* HAS_ORTHANC_PLUGIN_FIND_MATCHER == 1 */ 886 #endif /* HAS_ORTHANC_PLUGIN_FIND_MATCHER == 1 */
913 887
1016 { 990 {
1017 return false; 991 return false;
1018 } 992 }
1019 else 993 else
1020 { 994 {
1021 ORTHANC_PLUGINS_THROW_EXCEPTION(error); 995 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(error);
1022 } 996 }
1023 } 997 }
1024 998
1025 999
1026 static void ReportIncompatibleVersion(OrthancPluginContext* context, 1000 static void ReportIncompatibleVersion(OrthancPluginContext* context,