# HG changeset patch # User Sebastien Jodogne # Date 1557477346 -7200 # Node ID d1f4521b9bbb7b0fe0317bab42a1b1ea0c8c8fd0 # Parent e626f38c5512100a00fc7d3e3c50b18f689f2734 OracleCommandExceptionMessage diff -r e626f38c5512 -r d1f4521b9bbb Framework/StoneEnumerations.h --- a/Framework/StoneEnumerations.h Thu May 09 18:45:52 2019 +0200 +++ b/Framework/StoneEnumerations.h Fri May 10 10:35:46 2019 +0200 @@ -174,6 +174,7 @@ MessageType_ImageReady, + MessageType_OrthancException, MessageType_CustomMessage // Custom messages ids ust be greater than this (this one must remain in last position) diff -r e626f38c5512 -r d1f4521b9bbb Samples/Sdl/Loader.cpp --- a/Samples/Sdl/Loader.cpp Thu May 09 18:45:52 2019 +0200 +++ b/Samples/Sdl/Loader.cpp Fri May 10 10:35:46 2019 +0200 @@ -138,6 +138,40 @@ + class OracleCommandExceptionMessage : + public OrthancStone::BaseMessage + { + private: + const IOracleCommand& command_; + Orthanc::OrthancException exception_; + + public: + OracleCommandExceptionMessage(const IOracleCommand& command, + const Orthanc::OrthancException& exception) : + command_(command), + exception_(exception) + { + } + + OracleCommandExceptionMessage(const IOracleCommand& command, + const Orthanc::ErrorCode& error) : + command_(command), + exception_(error) + { + } + + const IOracleCommand& GetCommand() const + { + return command_; + } + + const Orthanc::OrthancException& GetException() const + { + return exception_; + } + }; + + typedef std::map HttpHeaders; class OrthancRestApiCommand : public OracleCommandWithPayload @@ -181,27 +215,6 @@ }; - class FailureMessage : public OrthancStone::OriginMessage - { - private: - Orthanc::HttpStatus status_; - - public: - FailureMessage(const OrthancRestApiCommand& command, - Orthanc::HttpStatus status) : - OriginMessage(command), - status_(status) - { - } - - Orthanc::HttpStatus GetHttpStatus() const - { - return status_; - } - }; - - private: Orthanc::HttpMethod method_; std::string uri_; @@ -210,7 +223,7 @@ unsigned int timeout_; std::auto_ptr< OrthancStone::MessageHandler > successCallback_; - std::auto_ptr< OrthancStone::MessageHandler > failureCallback_; + std::auto_ptr< OrthancStone::MessageHandler > failureCallback_; public: OrthancRestApiCommand() : @@ -335,34 +348,13 @@ }; - class FailureMessage : public OrthancStone::OriginMessage - { - private: - Orthanc::HttpStatus status_; - - public: - FailureMessage(const DecodeOrthancImageCommand& command, - Orthanc::HttpStatus status) : - OriginMessage(command), - status_(status) - { - } - - Orthanc::HttpStatus GetHttpStatus() const - { - return status_; - } - }; - - private: std::string uri_; HttpHeaders headers_; unsigned int timeout_; std::auto_ptr< OrthancStone::MessageHandler > successCallback_; - std::auto_ptr< OrthancStone::MessageHandler > failureCallback_; + std::auto_ptr< OrthancStone::MessageHandler > failureCallback_; public: DecodeOrthancImageCommand() : @@ -437,28 +429,6 @@ } }; - - class FailureMessage : public OrthancStone::OriginMessage - { - private: - Orthanc::HttpStatus status_; - - public: - FailureMessage(const DecodeOrthancWebViewerJpegCommand& command, - Orthanc::HttpStatus status) : - OriginMessage(command), - status_(status) - { - } - - Orthanc::HttpStatus GetHttpStatus() const - { - return status_; - } - }; - - private: std::string instanceId_; unsigned int frame_; @@ -467,7 +437,7 @@ unsigned int timeout_; std::auto_ptr< OrthancStone::MessageHandler > successCallback_; - std::auto_ptr< OrthancStone::MessageHandler > failureCallback_; + std::auto_ptr< OrthancStone::MessageHandler > failureCallback_; public: DecodeOrthancWebViewerJpegCommand() : @@ -668,28 +638,12 @@ std::string answer; HttpHeaders answerHeaders; + client.ApplyAndThrowException(answer, answerHeaders); - bool success; - try - { - success = client.Apply(answer, answerHeaders); - DecodeAnswer(answer, answerHeaders); - } - catch (Orthanc::OrthancException& e) - { - success = false; - } + DecodeAnswer(answer, answerHeaders); - if (success) - { - OrthancRestApiCommand::SuccessMessage message(command, answerHeaders, answer); - emitter_.EmitMessage(receiver, message); - } - else - { - OrthancRestApiCommand::FailureMessage message(command, client.GetLastStatus()); - emitter_.EmitMessage(receiver, message); - } + OrthancRestApiCommand::SuccessMessage message(command, answerHeaders, answer); + emitter_.EmitMessage(receiver, message); } @@ -703,76 +657,58 @@ std::string answer; HttpHeaders answerHeaders; + client.ApplyAndThrowException(answer, answerHeaders); - bool success; - try + DecodeAnswer(answer, answerHeaders); + + Orthanc::MimeType contentType = Orthanc::MimeType_Binary; + + for (HttpHeaders::const_iterator it = answerHeaders.begin(); + it != answerHeaders.end(); ++it) { - success = client.Apply(answer, answerHeaders); - } - catch (Orthanc::OrthancException& e) - { - success = false; + std::string s; + Orthanc::Toolbox::ToLowerCase(s, it->first); + + if (s == "content-type") + { + contentType = Orthanc::StringToMimeType(it->second); + break; + } } - if (success) - { - DecodeAnswer(answer, answerHeaders); + std::auto_ptr image; - Orthanc::MimeType contentType = Orthanc::MimeType_Binary; - - for (HttpHeaders::const_iterator it = answerHeaders.begin(); - it != answerHeaders.end(); ++it) + switch (contentType) + { + case Orthanc::MimeType_Png: { - std::string s; - Orthanc::Toolbox::ToLowerCase(s, it->first); - - if (s == "content-type") - { - contentType = Orthanc::StringToMimeType(it->second); - break; - } + image.reset(new Orthanc::PngReader); + dynamic_cast(*image).ReadFromMemory(answer); + break; } - std::auto_ptr image; - - switch (contentType) + case Orthanc::MimeType_Pam: { - case Orthanc::MimeType_Png: - { - image.reset(new Orthanc::PngReader); - dynamic_cast(*image).ReadFromMemory(answer); - break; - } - - case Orthanc::MimeType_Pam: - { - image.reset(new Orthanc::PamReader); - dynamic_cast(*image).ReadFromMemory(answer); - break; - } - - case Orthanc::MimeType_Jpeg: - { - image.reset(new Orthanc::JpegReader); - dynamic_cast(*image).ReadFromMemory(answer); - break; - } - - default: - // TODO - Emit error message? - throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol, - "Unsupported HTTP Content-Type for an image: " + - std::string(Orthanc::EnumerationToString(contentType))); + image.reset(new Orthanc::PamReader); + dynamic_cast(*image).ReadFromMemory(answer); + break; } - DecodeOrthancImageCommand::SuccessMessage message(command, image.release(), contentType); - emitter_.EmitMessage(receiver, message); + case Orthanc::MimeType_Jpeg: + { + image.reset(new Orthanc::JpegReader); + dynamic_cast(*image).ReadFromMemory(answer); + break; + } + + default: + throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol, + "Unsupported HTTP Content-Type for an image: " + + std::string(Orthanc::EnumerationToString(contentType))); } - else - { - DecodeOrthancImageCommand::FailureMessage message(command, client.GetLastStatus()); - emitter_.EmitMessage(receiver, message); - } + + DecodeOrthancImageCommand::SuccessMessage message(command, image.release(), contentType); + emitter_.EmitMessage(receiver, message); } @@ -786,36 +722,19 @@ std::string answer; HttpHeaders answerHeaders; + client.ApplyAndThrowException(answer, answerHeaders); - bool success; - try + DecodeAnswer(answer, answerHeaders); + + Json::Value value; + Json::Reader reader; + if (reader.parse(answer, value)) { - success = client.Apply(answer, answerHeaders); - } - catch (Orthanc::OrthancException& e) - { - success = false; + std::cout << value.toStyledString() << std::endl; } - if (success) - { - DecodeAnswer(answer, answerHeaders); - - Json::Value value; - Json::Reader reader; - if (reader.parse(answer, value)) - { - std::cout << value.toStyledString() << std::endl; - } - - //DecodeOrthancWebViewerJpegCommand::SuccessMessage message(command, image.release(), contentType); - //emitter_.EmitMessage(receiver, message); - } - else - { - DecodeOrthancWebViewerJpegCommand::FailureMessage message(command, client.GetLastStatus()); - emitter_.EmitMessage(receiver, message); - } + //DecodeOrthancWebViewerJpegCommand::SuccessMessage message(command, image.release(), contentType); + //emitter_.EmitMessage(receiver, message); } @@ -854,10 +773,13 @@ catch (Orthanc::OrthancException& e) { LOG(ERROR) << "Exception within the oracle: " << e.What(); + emitter_.EmitMessage(item.GetReceiver(), OracleCommandExceptionMessage(item.GetCommand(), e)); } catch (...) { LOG(ERROR) << "Native exception within the oracle"; + emitter_.EmitMessage(item.GetReceiver(), OracleCommandExceptionMessage + (item.GetCommand(), Orthanc::ErrorCode_InternalError)); } } } @@ -1008,8 +930,15 @@ virtual void EmitMessage(const OrthancStone::IObserver& observer, const OrthancStone::IMessage& message) { - boost::unique_lock lock(mutex_); - oracleObservable_.EmitMessage(observer, message); + try + { + boost::unique_lock lock(mutex_); + oracleObservable_.EmitMessage(observer, message); + } + catch (Orthanc::OrthancException& e) + { + LOG(ERROR) << "Exception while emitting a message: " << e.What(); + } } @@ -1520,9 +1449,20 @@ printf("IMAGE %dx%d\n", message.GetImage().GetWidth(), message.GetImage().GetHeight()); } - void Handle(const Refactoring::OrthancRestApiCommand::FailureMessage& message) + void Handle(const Refactoring::OracleCommandExceptionMessage& message) { - printf("ERROR %d\n", message.GetHttpStatus()); + printf("EXCEPTION: [%s] on command type %d\n", message.GetException().What(), message.GetCommand().GetType()); + + switch (message.GetCommand().GetType()) + { + case Refactoring::IOracleCommand::Type_DecodeOrthancWebViewerJpeg: + printf("URI: [%s]\n", dynamic_cast + (message.GetCommand()).GetUri().c_str()); + break; + + default: + break; + } } public: @@ -1536,6 +1476,10 @@ oracle.RegisterObserverCallback (new OrthancStone::Callable (*this, &Toto::Handle)); + + oracle.RegisterObserverCallback + (new OrthancStone::Callable + (*this, &Toto::Handle)); } };