comparison Plugins/Samples/Common/OrthancPluginCppWrapper.h @ 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 15637de71fee
comparison
equal deleted inserted replaced
2232:3dd44baebc36 2234:a78d15509a1c
30 **/ 30 **/
31 31
32 32
33 #pragma once 33 #pragma once
34 34
35 #include "OrthancPluginException.h"
36
35 #include <orthanc/OrthancCPlugin.h> 37 #include <orthanc/OrthancCPlugin.h>
36 #include <boost/noncopyable.hpp> 38 #include <boost/noncopyable.hpp>
37 #include <boost/lexical_cast.hpp> 39 #include <boost/lexical_cast.hpp>
38 #include <json/value.h> 40 #include <json/value.h>
39
40 #if !defined(HAS_ORTHANC_EXCEPTION)
41 # error The macro HAS_ORTHANC_EXCEPTION must be defined
42 #endif
43
44
45 #if HAS_ORTHANC_EXCEPTION == 1
46 # include "../../../Core/OrthancException.h"
47 # define ORTHANC_PLUGINS_THROW_EXCEPTION(code) throw ::Orthanc::OrthancException(static_cast<Orthanc::ErrorCode>(code))
48 #else
49 # define ORTHANC_PLUGINS_THROW_EXCEPTION(code) throw ::OrthancPlugins::PluginException(code)
50 #endif
51 41
52 42
53 #if (ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER >= 2 || \ 43 #if (ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER >= 2 || \
54 (ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER == 1 && \ 44 (ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER == 1 && \
55 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER >= 2)) 45 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER >= 2))
59 # define HAS_ORTHANC_PLUGIN_FIND_MATCHER 0 49 # define HAS_ORTHANC_PLUGIN_FIND_MATCHER 0
60 #endif 50 #endif
61 51
62 52
63 53
64
65 namespace OrthancPlugins 54 namespace OrthancPlugins
66 { 55 {
67 typedef void (*RestCallback) (OrthancPluginRestOutput* output, 56 typedef void (*RestCallback) (OrthancPluginRestOutput* output,
68 const char* url, 57 const char* url,
69 const OrthancPluginHttpRequest* request); 58 const OrthancPluginHttpRequest* request);
70
71 const char* GetErrorDescription(OrthancPluginContext* context,
72 OrthancPluginErrorCode code);
73
74 #if HAS_ORTHANC_EXCEPTION == 0
75 class PluginException
76 {
77 private:
78 OrthancPluginErrorCode code_;
79
80 public:
81 PluginException(OrthancPluginErrorCode code) : code_(code)
82 {
83 }
84
85 OrthancPluginErrorCode GetErrorCode() const
86 {
87 return code_;
88 }
89
90 const char* What(OrthancPluginContext* context) const
91 {
92 return ::OrthancPlugins::GetErrorDescription(context, code_);
93 }
94
95 static void Check(OrthancPluginErrorCode code);
96 };
97 #endif
98
99 59
100 class MemoryBuffer : public boost::noncopyable 60 class MemoryBuffer : public boost::noncopyable
101 { 61 {
102 private: 62 private:
103 OrthancPluginContext* context_; 63 OrthancPluginContext* context_;
485 try 445 try
486 { 446 {
487 Callback(output, url, request); 447 Callback(output, url, request);
488 return OrthancPluginErrorCode_Success; 448 return OrthancPluginErrorCode_Success;
489 } 449 }
490 #if HAS_ORTHANC_EXCEPTION == 1 450 catch (ORTHANC_PLUGINS_GET_EXCEPTION_CLASS& e)
491 catch (Orthanc::OrthancException& e)
492 { 451 {
493 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); 452 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode());
494 } 453 }
495 #else
496 catch (OrthancPlugins::PluginException& e)
497 {
498 return e.GetErrorCode();
499 }
500 #endif
501 catch (boost::bad_lexical_cast&) 454 catch (boost::bad_lexical_cast&)
502 { 455 {
503 return OrthancPluginErrorCode_BadFileFormat; 456 return OrthancPluginErrorCode_BadFileFormat;
504 } 457 }
505 catch (...) 458 catch (...)