diff options
author | Noel Grandin <noelgrandin@gmail.com> | 2023-09-30 20:47:46 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2023-10-03 10:12:41 +0200 |
commit | d9e322d60f65ff20631dab87baa5a2c7c71daaa2 (patch) | |
tree | d481e5a4728363b3866b5b5f6dbe64a2780b04a6 /include | |
parent | 64fc701388d1dcf8ae36ba2cc73eb5416a7c3374 (diff) |
replace ErrorInfo with simpler mechanism
Instead of returning ErrCode class everywhere, return a new
class ErrrCodeMsg, which combines an ErrCode with the other
parameters that are used to control the error reporting.
I do not change everything that uses ErrCode here, I started
from SfxBaseController/SfxMedium and worked outwards.
This change serves two purposes
(1) Replace the extremely whacky ErrorInfo mechanism we were
using to smuggle information into the error handler reporting
mechanism with a very straightforward approach of just combining it
into the error class.
(2) Allow us to capture the source location that produced the error,
which makes debugging the source of a problem soooo much easier.
Change-Id: I978b8f00c9851b41a216c7ebdef2ef94251d5519
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/157440
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'include')
-rw-r--r-- | include/basic/basmgr.hxx | 6 | ||||
-rw-r--r-- | include/basic/sbstar.hxx | 5 | ||||
-rw-r--r-- | include/comphelper/errcode.hxx | 126 | ||||
-rw-r--r-- | include/sfx2/app.hxx | 2 | ||||
-rw-r--r-- | include/sfx2/docfile.hxx | 12 | ||||
-rw-r--r-- | include/sfx2/objsh.hxx | 8 | ||||
-rw-r--r-- | include/sfx2/sfxbasemodel.hxx | 3 | ||||
-rw-r--r-- | include/svtools/ehdl.hxx | 4 | ||||
-rw-r--r-- | include/vcl/errinf.hxx | 106 |
9 files changed, 134 insertions, 138 deletions
diff --git a/include/basic/basmgr.hxx b/include/basic/basmgr.hxx index 072d92a6e417..1ef4f77cf16e 100644 --- a/include/basic/basmgr.hxx +++ b/include/basic/basmgr.hxx @@ -55,14 +55,14 @@ enum class BasicErrorReason class BasicError { private: - ErrCode nErrorId; + ErrCodeMsg nErrorId; BasicErrorReason nReason; public: BasicError( const BasicError& rErr ); - BasicError( ErrCode nId, BasicErrorReason nR ); + BasicError( ErrCodeMsg nId, BasicErrorReason nR ); - ErrCode const & GetErrorId() const { return nErrorId; } + ErrCodeMsg const & GetErrorId() const { return nErrorId; } }; class BasicLibInfo; diff --git a/include/basic/sbstar.hxx b/include/basic/sbstar.hxx index 42f551bc403f..599f14aa4b1b 100644 --- a/include/basic/sbstar.hxx +++ b/include/basic/sbstar.hxx @@ -25,6 +25,7 @@ #include <basic/sbmod.hxx> #include <rtl/ustring.hxx> #include <tools/link.hxx> +#include <comphelper/errcode.hxx> #include <basic/sbdef.hxx> #include <basic/basicdllapi.h> @@ -111,13 +112,13 @@ public: static sal_uInt16 GetLine(); static sal_uInt16 GetCol1(); static sal_uInt16 GetCol2(); - static void SetErrorData( ErrCode nCode, sal_uInt16 nLine, + static void SetErrorData( const ErrCodeMsg& nCode, sal_uInt16 nLine, sal_uInt16 nCol1, sal_uInt16 nCol2 ); // Specific to error handler static void MakeErrorText( ErrCode, std::u16string_view aMsg ); static const OUString& GetErrorText(); - static ErrCode const & GetErrorCode(); + static ErrCodeMsg const & GetErrorCode(); static sal_uInt16 GetVBErrorCode( ErrCode nError ); static ErrCode GetSfxFromVBError( sal_uInt16 nError ); bool IsBreak() const { return bBreak; } diff --git a/include/comphelper/errcode.hxx b/include/comphelper/errcode.hxx index 7b4fffa1bb3a..6e5390b9a09b 100644 --- a/include/comphelper/errcode.hxx +++ b/include/comphelper/errcode.hxx @@ -21,6 +21,16 @@ #include <rtl/ustring.hxx> #include <comphelper/comphelperdllapi.h> #include <ostream> +#include <o3tl/typed_flags_set.hxx> +#include <optional> + +#if (defined DBG_UTIL) && ((defined __GNUC__ && !defined __clang__) || (defined __clang__ && __clang_major__ >= 9)) && __has_include(<experimental/source_location>) +#define LIBO_ERRMSG_USE_SOURCE_LOCATION +#endif + +#ifdef LIBO_ERRMSG_USE_SOURCE_LOCATION +#include <experimental/source_location> +#endif /* @@ -28,7 +38,7 @@ || || || || | Warning || || | | || || || | - Dynamic || || | + Unused || || | | || || | Subsystemarea| || | | || | @@ -50,9 +60,6 @@ Warning || || | #define ERRCODE_CLASS_MASK (31UL << ERRCODE_CLASS_SHIFT) -#define ERRCODE_DYNAMIC_COUNT 31UL -#define ERRCODE_DYNAMIC_MASK (31UL << ERRCODE_DYNAMIC_SHIFT) - enum class ErrCodeArea; enum class ErrCodeClass; @@ -107,20 +114,8 @@ public: return m_value && !IsWarning(); } - bool IsDynamic() const { - return m_value & ERRCODE_DYNAMIC_MASK; - } - - sal_uInt32 GetDynamic() const { - return (m_value & ERRCODE_DYNAMIC_MASK) >> ERRCODE_DYNAMIC_SHIFT; - } - - ErrCode StripDynamic() const { - return ErrCode(m_value & ~ERRCODE_DYNAMIC_MASK); - } - - constexpr ErrCode StripWarningAndDynamic() const { - return ErrCode(m_value & ~(ERRCODE_DYNAMIC_MASK | ERRCODE_WARNING_MASK)); + constexpr ErrCode StripWarning() const { + return ErrCode(m_value & ~ERRCODE_WARNING_MASK); } constexpr ErrCodeArea GetArea() const { @@ -154,6 +149,101 @@ private: COMPHELPER_DLLPUBLIC std::ostream& operator<<(std::ostream& os, const ErrCode& err); +enum class DialogMask +{ + NONE = 0x0000, + ButtonsOk = 0x0001, + ButtonsCancel = 0x0002, + ButtonsRetry = 0x0004, + ButtonsNo = 0x0008, + ButtonsYes = 0x0010, + ButtonsYesNo = 0x0018, + + ButtonDefaultsOk = 0x0100, + ButtonDefaultsCancel = 0x0200, + ButtonDefaultsYes = 0x0300, + ButtonDefaultsNo = 0x0400, + + MessageError = 0x1000, + MessageWarning = 0x2000, + MessageInfo = 0x3000, + + MAX = USHRT_MAX, +}; +namespace o3tl +{ + template<> struct typed_flags<DialogMask> : is_typed_flags<DialogMask, 0xffff> {}; +} + +/** Wrap up an ErrCode and an explanation and the source location where the error was created, + helps with debugging when finding the source of a problem. +*/ +class SAL_WARN_UNUSED ErrCodeMsg +{ +public: + ErrCodeMsg() : mnCode(0) {} +#ifdef LIBO_ERRMSG_USE_SOURCE_LOCATION + ErrCodeMsg(ErrCode code, const OUString& arg, std::experimental::source_location loc = std::experimental::source_location::current()) + : mnCode(code), maArg1(arg), mnDialogMask(DialogMask::NONE), moLoc(loc) {} + ErrCodeMsg(ErrCode code, const OUString& arg1, const OUString& arg2, std::experimental::source_location loc = std::experimental::source_location::current()) + : mnCode(code), maArg1(arg1), maArg2(arg2), mnDialogMask(DialogMask::NONE), moLoc(loc) {} + ErrCodeMsg(ErrCode code, std::experimental::source_location loc = std::experimental::source_location::current()) + : mnCode(code), mnDialogMask(DialogMask::NONE), moLoc(loc) {} + ErrCodeMsg(ErrCode code, const OUString& arg, DialogMask mask, std::experimental::source_location loc = std::experimental::source_location::current()) + : mnCode(code), maArg1(arg), mnDialogMask(mask), moLoc(loc) {} + ErrCodeMsg(ErrCode code, const OUString& arg1, const OUString& arg2, DialogMask mask, std::experimental::source_location loc = std::experimental::source_location::current()) + : mnCode(code), maArg1(arg1), maArg2(arg2), mnDialogMask(mask), moLoc(loc) {} +#else + ErrCodeMsg(ErrCode code, const OUString& arg) + : mnCode(code), maArg1(arg), mnDialogMask(DialogMask::NONE) {} + ErrCodeMsg(ErrCode code, const OUString& arg1, const OUString& arg2) + : mnCode(code), maArg1(arg1), maArg2(arg2), mnDialogMask(DialogMask::NONE) {} + ErrCodeMsg(ErrCode code) + : mnCode(code), mnDialogMask(DialogMask::NONE) {} + ErrCodeMsg(ErrCode code, const OUString& arg, DialogMask mask) + : mnCode(code), maArg1(arg), mnDialogMask(mask) {} + ErrCodeMsg(ErrCode code, const OUString& arg1, const OUString& arg2, DialogMask mask) + : mnCode(code), maArg1(arg1), maArg2(arg2), mnDialogMask(mask) {} +#endif + + const ErrCode & GetCode() const { return mnCode; } + const OUString & GetArg1() const { return maArg1; } + const OUString & GetArg2() const { return maArg2; } + DialogMask GetDialogMask() const { return mnDialogMask; } + +#ifdef LIBO_ERRMSG_USE_SOURCE_LOCATION + const std::optional<std::experimental::source_location> & GetSourceLocation() const { return moLoc; } +#endif + + /** convert to ERRCODE_NONE if it's a warning, else return the error */ + ErrCodeMsg IgnoreWarning() const { return mnCode.IsWarning() ? ErrCodeMsg(ErrCode(0)) : *this; } + + bool IsWarning() const { return mnCode.IsWarning(); } + bool IsError() const { return mnCode.IsError(); } + explicit operator bool() const { return bool(mnCode); } + bool operator==(const ErrCodeMsg& rOther) const { return mnCode == rOther.mnCode; } + bool operator!=(const ErrCodeMsg& rOther) const { return mnCode != rOther.mnCode; } + + /// Return a string suitable for debug output, the same as the operator<< function + COMPHELPER_DLLPUBLIC OUString toString() const; + +private: + ErrCode mnCode; + OUString maArg1; + OUString maArg2; + DialogMask mnDialogMask; +#ifdef LIBO_ERRMSG_USE_SOURCE_LOCATION + std::optional<std::experimental::source_location> moLoc; +#endif +}; + +COMPHELPER_DLLPUBLIC std::ostream& operator<<(std::ostream& os, const ErrCodeMsg& err); + +inline bool operator==(const ErrCodeMsg& lhs, ErrCode rhs) { return lhs.GetCode() == rhs; } +inline bool operator!=(const ErrCodeMsg& lhs, ErrCode rhs) { return lhs.GetCode() != rhs; } +inline bool operator==(ErrCode lhs, const ErrCodeMsg& rhs) { return lhs == rhs.GetCode(); } +inline bool operator!=(ErrCode lhs, const ErrCodeMsg& rhs) { return lhs != rhs.GetCode(); } + enum class ErrCodeArea { Io = 0 , Sfx = 2 , diff --git a/include/sfx2/app.hxx b/include/sfx2/app.hxx index bd214ce2f2e5..96bce367fd75 100644 --- a/include/sfx2/app.hxx +++ b/include/sfx2/app.hxx @@ -129,7 +129,7 @@ public: /** * @param pArgs Takes ownership */ - ErrCode LoadTemplate( SfxObjectShellLock& xDoc, const OUString& rFileName, std::unique_ptr<SfxItemSet> pArgs ); + ErrCodeMsg LoadTemplate( SfxObjectShellLock& xDoc, const OUString& rFileName, std::unique_ptr<SfxItemSet> pArgs ); weld::Window* GetTopWindow() const; // members diff --git a/include/sfx2/docfile.hxx b/include/sfx2/docfile.hxx index 0b1c417247f5..377bab62830f 100644 --- a/include/sfx2/docfile.hxx +++ b/include/sfx2/docfile.hxx @@ -143,14 +143,14 @@ public: void Download( const Link<void*,void>& aLink = Link<void*,void>()); void SetDoneLink( const Link<void*,void>& rLink ); - ErrCode GetErrorCode() const; - ErrCode GetErrorIgnoreWarning() const + ErrCodeMsg GetErrorCode() const; + ErrCodeMsg GetErrorIgnoreWarning() const { return GetErrorCode().IgnoreWarning(); } - ErrCode GetWarningError() const; - ErrCode const & GetLastStorageCreationState() const; + ErrCodeMsg const & GetWarningError() const; + ErrCodeMsg const & GetLastStorageCreationState() const; - void SetError(ErrCode nError); - void SetWarningError(ErrCode nWarningError); + void SetError(ErrCodeMsg nError); + void SetWarningError(const ErrCodeMsg& nWarningError); void CloseInStream(); void CloseOutStream(); diff --git a/include/sfx2/objsh.hxx b/include/sfx2/objsh.hxx index a214c242a1c7..7e5cb97827a6 100644 --- a/include/sfx2/objsh.hxx +++ b/include/sfx2/objsh.hxx @@ -290,9 +290,9 @@ public: SAL_DLLPRIVATE void SetMacroMode_Impl(bool bModal=true); void ResetError(); - ErrCode GetErrorIgnoreWarning() const; - ErrCode GetErrorCode() const; - void SetError(ErrCode rErr); + ErrCodeMsg GetErrorIgnoreWarning() const; + ErrCodeMsg GetErrorCode() const; + void SetError(const ErrCodeMsg& rErr); /** * Initialize bare minimum just enough for unit test runs. @@ -719,7 +719,7 @@ public: SAL_DLLPRIVATE static bool UseInteractionToHandleError( const css::uno::Reference< css::task::XInteractionHandler >& xHandler, - ErrCode nError ); + const ErrCodeMsg& nError ); SAL_DLLPRIVATE const SfxObjectShell_Impl* Get_Impl() const { return pImpl.get(); } SAL_DLLPRIVATE void SetCreateMode_Impl( SfxObjectCreateMode nMode ); diff --git a/include/sfx2/sfxbasemodel.hxx b/include/sfx2/sfxbasemodel.hxx index 2602387fad59..de7661104451 100644 --- a/include/sfx2/sfxbasemodel.hxx +++ b/include/sfx2/sfxbasemodel.hxx @@ -67,6 +67,7 @@ #include <memory> class ErrCode; +class ErrCodeMsg; class SfxMedium; class SfxObjectShell ; class SfxViewFrame; @@ -738,7 +739,7 @@ private: SAL_DLLPRIVATE void loadCmisProperties(); - SAL_DLLPRIVATE SfxMedium* handleLoadError( ErrCode nError, SfxMedium* pMedium ); + SAL_DLLPRIVATE SfxMedium* handleLoadError( const ErrCodeMsg& nError, SfxMedium* pMedium ); // private variables and methods diff --git a/include/svtools/ehdl.hxx b/include/svtools/ehdl.hxx index 78bcf1723dfe..1f3b8d6cb17e 100644 --- a/include/svtools/ehdl.hxx +++ b/include/svtools/ehdl.hxx @@ -40,7 +40,7 @@ public: SfxErrorContext( sal_uInt16 nCtxIdP, OUString aArg1, weld::Window *pWin=nullptr, const ErrMsgCode* pIds = nullptr, const std::locale& rResLocaleP = SvtResLocale()); - bool GetString(ErrCode nErrId, OUString &rStr) override; + bool GetString(const ErrCodeMsg& nErrId, OUString &rStr) override; void SetExtendedMessage(ErrCode nErrId, const OUString& rStr); @@ -69,7 +69,7 @@ private: std::locale aResLocale; SVT_DLLPRIVATE static void GetClassString(ErrCodeClass lErrId, OUString &); - virtual bool CreateString(const ErrorInfo *, OUString &) const override; + virtual bool CreateString(const ErrCodeMsg&, OUString &) const override; }; /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/vcl/errinf.hxx b/include/vcl/errinf.hxx index 7f2976e23cee..0f4d8d781de7 100644 --- a/include/vcl/errinf.hxx +++ b/include/vcl/errinf.hxx @@ -24,7 +24,6 @@ #include <utility> #include <comphelper/errcode.hxx> #include <vcl/dllapi.h> -#include <o3tl/typed_flags_set.hxx> #include <vector> #include <memory> @@ -35,15 +34,12 @@ namespace weld { class Window; } class ErrorHandler; class ErrorContext; -class ErrorInfo; -class DynamicErrorInfo; -class ImplDynamicErrorInfo; enum class DialogMask; class VCL_DLLPUBLIC ErrorStringFactory { public: - static bool CreateString(const ErrorInfo*, OUString&); + static bool CreateString(const ErrCodeMsg&, OUString&); }; typedef void (* DisplayFnPtr)(); @@ -59,7 +55,6 @@ class VCL_DLLPUBLIC ErrorRegistry friend class ErrorHandler; friend class ErrorContext; friend class ErrorStringFactory; - friend class ImplDynamicErrorInfo; public: ErrorRegistry(); @@ -78,39 +73,9 @@ private: bool m_bLock; - sal_uInt16 nNextError; - std::vector<ErrorHandler*> errorHandlers; std::vector<ErrorContext*> contexts; - - DynamicErrorInfo* ppDynErrInfo[ERRCODE_DYNAMIC_COUNT]; -}; - -enum class DialogMask -{ - NONE = 0x0000, - ButtonsOk = 0x0001, - ButtonsCancel = 0x0002, - ButtonsRetry = 0x0004, - ButtonsNo = 0x0008, - ButtonsYes = 0x0010, - ButtonsYesNo = 0x0018, - - ButtonDefaultsOk = 0x0100, - ButtonDefaultsCancel = 0x0200, - ButtonDefaultsYes = 0x0300, - ButtonDefaultsNo = 0x0400, - - MessageError = 0x1000, - MessageWarning = 0x2000, - MessageInfo = 0x3000, - - MAX = USHRT_MAX, }; -namespace o3tl -{ - template<> struct typed_flags<DialogMask> : is_typed_flags<DialogMask, 0xffff> {}; -} class SAL_WARN_UNUSED VCL_DLLPUBLIC ErrorHandler { @@ -136,72 +101,11 @@ public: @return what sort of dialog to use, with what buttons */ - static DialogMask HandleError(ErrCode nId, weld::Window* pParent = nullptr, DialogMask nMask = DialogMask::MAX); - static bool GetErrorString(ErrCode nId, OUString& rStr); + static DialogMask HandleError(const ErrCodeMsg& nId, weld::Window* pParent = nullptr, DialogMask nMask = DialogMask::MAX); + static bool GetErrorString(const ErrCodeMsg& nId, OUString& rStr); protected: - virtual bool CreateString(const ErrorInfo*, OUString &) const = 0; - -}; - -class SAL_WARN_UNUSED VCL_DLLPUBLIC ErrorInfo -{ -public: - ErrorInfo(ErrCode nArgUserId) : - nUserId(nArgUserId) {} - virtual ~ErrorInfo(); - - ErrCode const & GetErrorCode() const { return nUserId; } - - static std::unique_ptr<ErrorInfo> GetErrorInfo(ErrCode); - -private: - ErrCode nUserId; -}; - -class SAL_WARN_UNUSED VCL_DLLPUBLIC DynamicErrorInfo : public ErrorInfo -{ - friend class ImplDynamicErrorInfo; - -public: - DynamicErrorInfo(ErrCode nUserId, DialogMask nMask); - virtual ~DynamicErrorInfo() override; - - operator ErrCode() const; - DialogMask GetDialogMask() const; - -private: - std::unique_ptr<ImplDynamicErrorInfo> pImpl; - -}; - -class SAL_WARN_UNUSED VCL_DLLPUBLIC StringErrorInfo final : public DynamicErrorInfo -{ -public: - StringErrorInfo(ErrCode nUserId, - OUString aStringP, - DialogMask nMask = DialogMask::NONE); - - const OUString& GetErrorString() const { return aString; } - -private: - OUString aString; - -}; - -class SAL_WARN_UNUSED VCL_DLLPUBLIC TwoStringErrorInfo final : public DynamicErrorInfo -{ -public: - TwoStringErrorInfo(ErrCode nUserID, OUString aTheArg1, - OUString aTheArg2, DialogMask nMask): - DynamicErrorInfo(nUserID, nMask), aArg1(std::move(aTheArg1)), aArg2(std::move(aTheArg2)) {} - - const OUString& GetArg1() const { return aArg1; } - const OUString& GetArg2() const { return aArg2; } - -private: - OUString aArg1; - OUString aArg2; + virtual bool CreateString(const ErrCodeMsg&, OUString &) const = 0; }; @@ -215,7 +119,7 @@ public: ErrorContext(weld::Window *pWin); virtual ~ErrorContext(); - virtual bool GetString(ErrCode nErrId, OUString& rCtxStr) = 0; + virtual bool GetString(const ErrCodeMsg& nErrId, OUString& rCtxStr) = 0; weld::Window* GetParent(); static ErrorContext* GetContext(); |