# HG changeset patch # User Alain Mazy # Date 1666162654 -7200 # Node ID edefb278cb774eb52e74e33cf789868cb0f8d8ef # Parent d842e4446e63dfa499eed76120a9bbb476c534e5 cleanup diff -r d842e4446e63 -r edefb278cb77 OrthancServer/Plugins/Engine/OrthancPlugins.cpp --- a/OrthancServer/Plugins/Engine/OrthancPlugins.cpp Thu Oct 13 17:11:43 2022 +0200 +++ b/OrthancServer/Plugins/Engine/OrthancPlugins.cpp Wed Oct 19 08:57:34 2022 +0200 @@ -3113,6 +3113,27 @@ CopyToMemoryBuffer(*p.target, dicom); } + static void ThrowOnHttpError(HttpStatus httpStatus) + { + int intHttpStatus = static_cast(httpStatus); + if (intHttpStatus >= 200 && intHttpStatus <= 300) + { + return; // not an error + } + else if (intHttpStatus == 401 || intHttpStatus == 403) + { + throw OrthancException(ErrorCode_Unauthorized); + } + else if (intHttpStatus == 404) + { + throw OrthancException(ErrorCode_UnknownResource); + } + else + { + throw OrthancException(ErrorCode_BadRequest); + } + } + void OrthancPlugins::RestApiGet(const void* parameters, bool afterPlugins) @@ -3133,14 +3154,9 @@ std::map httpHeaders; std::string result; - if (IHttpHandler::SimpleGet(result, NULL, *handler, RequestOrigin_Plugins, p.uri, httpHeaders) == HttpStatus_200_Ok) - { - CopyToMemoryBuffer(*p.target, result); - } - else - { - throw OrthancException(ErrorCode_UnknownResource); - } + + ThrowOnHttpError(IHttpHandler::SimpleGet(result, NULL, *handler, RequestOrigin_Plugins, p.uri, httpHeaders)); + CopyToMemoryBuffer(*p.target, result); } @@ -3169,14 +3185,9 @@ } std::string result; - if (IHttpHandler::SimpleGet(result, NULL, *handler, RequestOrigin_Plugins, p.uri, headers) == HttpStatus_200_Ok) - { - CopyToMemoryBuffer(*p.target, result); - } - else - { - throw OrthancException(ErrorCode_UnknownResource); - } + + ThrowOnHttpError(IHttpHandler::SimpleGet(result, NULL, *handler, RequestOrigin_Plugins, p.uri, headers)); + CopyToMemoryBuffer(*p.target, result); } @@ -3200,18 +3211,13 @@ std::map httpHeaders; std::string result; - if (isPost ? + + ThrowOnHttpError((isPost ? IHttpHandler::SimplePost(result, NULL, *handler, RequestOrigin_Plugins, p.uri, - p.body, p.bodySize, httpHeaders) == HttpStatus_200_Ok : + p.body, p.bodySize, httpHeaders) : IHttpHandler::SimplePut(result, NULL, *handler, RequestOrigin_Plugins, p.uri, - p.body, p.bodySize, httpHeaders) == HttpStatus_200_Ok) - { - CopyToMemoryBuffer(*p.target, result); - } - else - { - throw OrthancException(ErrorCode_UnknownResource); - } + p.body, p.bodySize, httpHeaders))); + CopyToMemoryBuffer(*p.target, result); } @@ -3231,10 +3237,7 @@ std::map httpHeaders; - if (IHttpHandler::SimpleDelete(NULL, *handler, RequestOrigin_Plugins, uri, httpHeaders) != HttpStatus_200_Ok) - { - throw OrthancException(ErrorCode_UnknownResource); - } + ThrowOnHttpError(IHttpHandler::SimpleDelete(NULL, *handler, RequestOrigin_Plugins, uri, httpHeaders)); }