Skip to content

Commit

Permalink
Move part of errors.h to a new errors.cpp file
Browse files Browse the repository at this point in the history
No real changes, just refactoring.
  • Loading branch information
vslavik committed Apr 21, 2024
1 parent 9beb207 commit f593952
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 37 deletions.
1 change: 1 addition & 0 deletions Poedit.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
<ClCompile Include="src\edframe.cpp" />
<ClCompile Include="src\editing_area.cpp" />
<ClCompile Include="src\edlistctrl.cpp" />
<ClCompile Include="src\errors.cpp" />
<ClCompile Include="src\export_html.cpp" />
<ClCompile Include="src\extractors\extractor.cpp" />
<ClCompile Include="src\extractors\extractor_gettext.cpp" />
Expand Down
3 changes: 3 additions & 0 deletions Poedit.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@
<ClCompile Include="src\app_updates.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\errors.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\attentionbar.h">
Expand Down
6 changes: 6 additions & 0 deletions Poedit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@
B26E2C8F25A245BD008D6DF1 /* CloseButtonTemplate@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = B26E2C8B25A245BC008D6DF1 /* CloseButtonTemplate@2x.png */; };
B26E2C9025A245BD008D6DF1 /* CloseButtonTemplate.png in Resources */ = {isa = PBXBuildFile; fileRef = B26E2C8C25A245BC008D6DF1 /* CloseButtonTemplate.png */; };
B26E2C9125A245BD008D6DF1 /* CloseButtonHoverTemplate.png in Resources */ = {isa = PBXBuildFile; fileRef = B26E2C8D25A245BD008D6DF1 /* CloseButtonHoverTemplate.png */; };
B273818C2BD5027E005F24DA /* errors.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B273818B2BD5027E005F24DA /* errors.cpp */; };
B273818D2BD5027E005F24DA /* errors.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B273818B2BD5027E005F24DA /* errors.cpp */; };
B27959DE1E85850A00DBA47D /* qa_checks.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B27959DC1E85850A00DBA47D /* qa_checks.cpp */; };
B280E84D1A92776D009F4A98 /* http_client_macos.mm in Sources */ = {isa = PBXBuildFile; fileRef = B280E84B1A92776D009F4A98 /* http_client_macos.mm */; };
B28602441DDB279400FCA617 /* colorscheme.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B28602421DDB279400FCA617 /* colorscheme.cpp */; };
Expand Down Expand Up @@ -388,6 +390,7 @@
B26E2C8B25A245BC008D6DF1 /* CloseButtonTemplate@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "CloseButtonTemplate@2x.png"; path = "macos/CloseButtonTemplate@2x.png"; sourceTree = "<group>"; };
B26E2C8C25A245BC008D6DF1 /* CloseButtonTemplate.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = CloseButtonTemplate.png; path = macos/CloseButtonTemplate.png; sourceTree = "<group>"; };
B26E2C8D25A245BD008D6DF1 /* CloseButtonHoverTemplate.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = CloseButtonHoverTemplate.png; path = macos/CloseButtonHoverTemplate.png; sourceTree = "<group>"; };
B273818B2BD5027E005F24DA /* errors.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = errors.cpp; sourceTree = "<group>"; };
B27959DC1E85850A00DBA47D /* qa_checks.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = qa_checks.cpp; sourceTree = "<group>"; };
B27959DD1E85850A00DBA47D /* qa_checks.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = qa_checks.h; sourceTree = "<group>"; };
B27D1FC519EFFA2800AB1913 /* da */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = da; path = da.lproj/InfoPlist.strings; sourceTree = "<group>"; };
Expand Down Expand Up @@ -759,6 +762,7 @@
B21B7B481DD4DB9F002A4C62 /* editing_area.h */,
B28F1CBC16F629D30018AF7E /* edlistctrl.cpp */,
B28F1CBD16F629D30018AF7E /* edlistctrl.h */,
B273818B2BD5027E005F24DA /* errors.cpp */,
B29AE89B17105306008D1F8A /* errors.h */,
B28F1CE216F629D30018AF7E /* export_html.cpp */,
B238F674261237C4002D6845 /* filemonitor.cpp */,
Expand Down Expand Up @@ -1503,6 +1507,7 @@
B28F1CF916F629D30018AF7E /* progressinfo.cpp in Sources */,
B28F1CFA16F629D30018AF7E /* propertiesdlg.cpp in Sources */,
B26D0655182697200069C378 /* welcomescreen.cpp in Sources */,
B273818C2BD5027E005F24DA /* errors.cpp in Sources */,
B2E11F121A2C66FB00E4E42C /* text_control.cpp in Sources */,
B28F1CFB16F629D30018AF7E /* cat_update.cpp in Sources */,
B2CE2FEF1A94EBF50020A620 /* crowdin_client.cpp in Sources */,
Expand Down Expand Up @@ -1533,6 +1538,7 @@
B260089529AF88A000349A0E /* catalog_json.cpp in Sources */,
B201EBE41DCF8C0100FFB541 /* configuration.cpp in Sources */,
B2DAD7121AD198DE00DCB398 /* language.cpp in Sources */,
B273818D2BD5027E005F24DA /* errors.cpp in Sources */,
B201EBE31DCF8BFD00FFB541 /* catalog.cpp in Sources */,
B2DAD70F1AD1984200DCB398 /* utility.cpp in Sources */,
B260AA682BB2BDAE0003E378 /* unicode_helpers.cpp in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ poedit_SOURCES = \
edframe.cpp edframe.h \
editing_area.cpp editing_area.h \
edlistctrl.cpp edlistctrl.h \
errors.h \
errors.cpp errors.h \
export_html.cpp \
extractors/extractor.cpp extractors/extractor.h \
extractors/extractor_gettext.cpp \
Expand Down
4 changes: 2 additions & 2 deletions src/concurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ void detail::dispatch_async_cxx(boost::executors::work&& f, detail::queue q)
namespace
{

std::unique_ptr<detail::background_queue_executor> gs_background_executor;
std::unique_ptr<detail::main_thread_executor> gs_main_thread_executor;
std::unique_ptr<dispatch::detail::background_queue_executor> gs_background_executor;
std::unique_ptr<dispatch::detail::main_thread_executor> gs_main_thread_executor;
static std::once_flag gs_background_executor_flag, gs_main_thread_executor_flag;

}
Expand Down
56 changes: 56 additions & 0 deletions src/errors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* This file is part of Poedit (https://poedit.net)
*
* Copyright (C) 2013-2023 Vaclav Slavik
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/

#include "errors.h"

wxString errors::detail::DescribeExceptionImpl(Rethrower& rethrower)
{
try
{
rethrower.rethrow();
return "no error"; // silence stupid VC++
}
catch (const Exception& e)
{
return e.What();
}
catch (const std::exception& e)
{
const char *msg = e.what();
// try interpreting as UTF-8 first as the most likely one (from external sources)
wxString s = wxString::FromUTF8(msg);
if (s.empty())
{
s = wxString(msg);
if (s.empty()) // not in current locale either, fall back to Latin1
s = wxString(msg, wxConvISO8859_1);
}
return s;
}
catch (...)
{
return "unknown error";
}
}
67 changes: 33 additions & 34 deletions src/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
*
*/

#ifndef _ERRORS_H_
#define _ERRORS_H_
#ifndef Poedit_errors_h
#define Poedit_errors_h

#include <wx/string.h>
#include <exception>
#include <stdexcept>
#include <functional>

#include <boost/exception_ptr.hpp>

Expand All @@ -55,51 +56,49 @@ class Exception : public std::runtime_error
};


/// Helper to convert an exception into a human-readable string
template<typename Rethrow>
inline wxString DoDescribeException(Rethrow&& rethrow_exception)
namespace errors::detail
{

struct Rethrower
{
try
{
rethrow_exception();
return "no error"; // silence stupid VC++
}
catch (const Exception& e)
{
return e.What();
}
catch (const std::exception& e)
{
const char *msg = e.what();
// try interpreting as UTF-8 first as the most likely one (from external sources)
wxString s = wxString::FromUTF8(msg);
if (s.empty())
{
s = wxString(msg);
if (s.empty()) // not in current locale either, fall back to Latin1
s = wxString(msg, wxConvISO8859_1);
}
return s;
}
catch (...)
{
return "unknown error";
}
virtual void rethrow() = 0;
virtual ~Rethrower() {}
};

template<typename T>
struct RethrowerImpl : public Rethrower
{
RethrowerImpl(T&& func) : rethrow_exception(std::move(func)) {}
void rethrow() override { rethrow_exception(); }

T rethrow_exception;
};

wxString DescribeExceptionImpl(Rethrower& rethrower);

template<typename T>
inline wxString DescribeException(T&& rethrow_exception)
{
RethrowerImpl<T> rethrower(std::move(rethrow_exception));
return DescribeExceptionImpl(rethrower);
}

} // namespace errors::detail

/// Helper to convert an exception into a human-readable string
inline wxString DescribeException(std::exception_ptr e)
{
return DoDescribeException([e]{ std::rethrow_exception(e); });
return errors::detail::DescribeException([e]{ std::rethrow_exception(e); });
}

inline wxString DescribeException(boost::exception_ptr e)
{
return DoDescribeException([e]{ boost::rethrow_exception(e); });
return errors::detail::DescribeException([e]{ boost::rethrow_exception(e); });
}

inline wxString DescribeCurrentException()
{
return DescribeException(std::current_exception());
}

#endif // _ERRORS_H_
#endif // Poedit_errors_h

0 comments on commit f593952

Please sign in to comment.