diff --git a/src/main.cpp b/src/main.cpp index 980c7d5b..e2d40b8e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -34,13 +34,24 @@ IMPLEMENT_APP(MSE) // ----------------------------------------------------------------------------- : Initialization bool MSE::OnInit() { - wxInitAllImageHandlers(); - initFileFormats(); - settings.read(); - //Window* wnd = new SymbolWindow(nullptr); - Window* wnd = new SetWindow(nullptr, new_shared1(Game::byName(_("magic")))); - wnd->Show(); - return true; + try { + wxInitAllImageHandlers(); + initFileFormats(); + settings.read(); + //Window* wnd = new SymbolWindow(nullptr); + Window* wnd = new SetWindow(nullptr, new_shared1(Game::byName(_("magic")))); + wnd->Show(); + return true; + + } catch (Error e) { + handle_error(e, false); + } catch (std::exception e) { + // we don't throw std::exception ourselfs, so this is probably something serious + handle_error(InternalError(String(e.what(), IF_UNICODE(wxConvLocal, wxSTRING_MAXLEN) )), false); + } catch (...) { + handle_error(InternalError(_("An unexpected exception occurred, this is a bug!\nPlease save your work (use 'save as' to so you don't overwrite things),\n and restart Magic Set Editor.\n\nYou can leave a bug report on http://magicseteditor.sourceforge.net/")), false); + } + return false; } // ----------------------------------------------------------------------------- : Exit @@ -53,5 +64,15 @@ int MSE::OnExit() { // ----------------------------------------------------------------------------- : Exception handling bool MSE::OnExceptionInMainLoop() { + try { + throw; // rethrow the exception, so we can examine it + } catch (Error e) { + handle_error(e, false); + } catch (std::exception e) { + // we don't throw std::exception ourselfs, so this is probably something serious + handle_error(InternalError(String(e.what(), IF_UNICODE(wxConvLocal, wxSTRING_MAXLEN) )), false); + } catch (...) { + handle_error(InternalError(_("An unexpected exception occurred, this is a bug!\nPlease save your work (use 'save as' to so you don't overwrite things),\n and restart Magic Set Editor.\n\nYou can leave a bug report on http://magicseteditor.sourceforge.net/")), false); + } return true; } diff --git a/src/util/error.cpp b/src/util/error.cpp index 6d34239d..3a369940 100644 --- a/src/util/error.cpp +++ b/src/util/error.cpp @@ -19,3 +19,31 @@ Error::~Error() {} String Error::what() const { return message; } + +// ----------------------------------------------------------------------------- : Error handling + +// Errors for which a message box was already shown +vector previous_errors; +String pending_error; +DECLARE_TYPEOF_COLLECTION(String); + +void handle_error(const String& e, bool allow_duplicate = true, bool now = true) { + // Check duplicates + if (!allow_duplicate) { + FOR_EACH(pe, previous_errors) { + if (e == pe) return; + } + previous_errors.push_back(e); + } + // Only show errors in the main thread + if (!now || !wxThread::IsMain()) { + pending_error = e; + return; + } + // show message + wxMessageBox(e, _("Error"), wxOK | wxICON_ERROR); +} + +void handle_error(const Error& e, bool allow_duplicate, bool now) { + handle_error(e.what(), allow_duplicate, now); +} diff --git a/src/util/error.hpp b/src/util/error.hpp index bd7d6159..11376a6e 100644 --- a/src/util/error.hpp +++ b/src/util/error.hpp @@ -80,5 +80,17 @@ class ScriptError : public Error { inline ScriptError(const String& str) : Error(str) {} }; +// ----------------------------------------------------------------------------- : Error handling + +/// Handle an error by showing a message box +/** If !allow_duplicate and the error is the same as the previous error, does nothing. + * If !now the error is handled by a later call to handle_pending_errors() + */ +void handle_error(const Error& e, bool allow_duplicate = true, bool now = true); + +/// Handle errors that were not handled immediatly in handleError +/** Should be called repeatedly (e.g. in an onIdle event handler) */ +void handle_pending_errors(); + // ----------------------------------------------------------------------------- : EOF #endif diff --git a/src/util/io/reader.cpp b/src/util/io/reader.cpp index 6f764ad9..65bc4859 100644 --- a/src/util/io/reader.cpp +++ b/src/util/io/reader.cpp @@ -91,7 +91,7 @@ template <> void Reader::handle(String& s) { // read all lines that are indented enough readLine(); while (indent >= expected_indent) { - if (!first) value += '\n'; + if (!first) multi_line_str += _('\n'); first = false; multi_line_str += line.substr(expected_indent); // strip expected indent readLine();