Add source location to ComError

And use in CSysShExec::execute to improve location reporting

Change-Id: I624df0418b99a79207f5aeefa38d2bfe5ea7ffe1
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/169880
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Tested-by: Jenkins
This commit is contained in:
Mike Kaganski 2024-07-02 16:34:52 +05:00
parent 3b824baa86
commit 6144341c8e
2 changed files with 16 additions and 6 deletions

View file

@ -19,6 +19,9 @@
#pragma once
#include <sal/config.h>
#include <source_location>
#include <string>
#include <string_view>
#include <stdexcept>
@ -35,22 +38,27 @@ namespace sal::systools
class ComError : public std::runtime_error
{
public:
ComError(const std::string& message, HRESULT hr) :
std::runtime_error(message),
hr_(hr)
ComError(std::string_view message, HRESULT hr,
const std::source_location& loc = std::source_location::current())
: std::runtime_error(std::string(message))
, hr_(hr)
, loc_(loc)
{}
HRESULT GetHresult() const { return hr_; }
const std::source_location& GetLocation() const { return loc_; }
private:
HRESULT hr_;
std::source_location loc_;
};
/* Convert failed HRESULT to thrown ComError */
inline void ThrowIfFailed(HRESULT hr, std::string_view msg)
inline void ThrowIfFailed(HRESULT hr, std::string_view msg,
std::source_location loc = std::source_location::current())
{
if (FAILED(hr))
throw ComError(std::string(msg), hr);
throw ComError(msg, hr, loc);
}
/* A guard class to call CoInitializeEx/CoUninitialize in proper pairs

View file

@ -297,7 +297,9 @@ void SAL_CALL CSysShExec::execute( const OUString& aCommand, const OUString& aPa
{
throw css::lang::IllegalArgumentException(
("XSystemShellExecute.execute, " + o3tl::runtimeToOUString(e.what())
+ " with " + OUString::number(e.GetHresult())),
+ " at " + o3tl::runtimeToOUString(e.GetLocation().file_name()) + ":"
+ OUString::number(e.GetLocation().line()) + " error "
+ OUString::number(e.GetHresult())),
{}, 0);
}
// Fail at some arbitrary nesting depth, to avoid an infinite loop: