From a288d6199ed51d2f8733dcf59845d58ecd7f1c0d Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Tue, 5 Jun 2018 13:26:37 +0200 Subject: [PATCH] Add KI_PARAM_ERROR, similar to std::invalid_argument but using wxString instead of std::string to throw errors. std::invalid_argument does not work fine with translated strings as argument for message. (the translated message is incorrectly or not displayed if it contains non ascii8 chars, at least on Windows). KI_PARAM_ERROR can be throw-ed with a translatable/translated string (a wxString) --- eeschema/sim/sim_plot_frame.cpp | 3 ++- eeschema/sim/spice_value.cpp | 5 +++-- eeschema/widgets/tuner_slider.cpp | 7 ++++--- include/ki_exception.h | 29 +++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/eeschema/sim/sim_plot_frame.cpp b/eeschema/sim/sim_plot_frame.cpp index d333574689..b84ee280e4 100644 --- a/eeschema/sim/sim_plot_frame.cpp +++ b/eeschema/sim/sim_plot_frame.cpp @@ -382,9 +382,10 @@ void SIM_PLOT_FRAME::AddTuner( SCH_COMPONENT* aComponent ) m_tuners.push_back( tuner ); m_tunePanel->Layout(); } - catch( ... ) + catch( const KI_PARAM_ERROR& e ) { // Sorry, no bonus + DisplayError( nullptr, e.What() ); } } diff --git a/eeschema/sim/spice_value.cpp b/eeschema/sim/spice_value.cpp index 3296b7b0aa..42b0d8ec54 100644 --- a/eeschema/sim/spice_value.cpp +++ b/eeschema/sim/spice_value.cpp @@ -31,18 +31,19 @@ #include #include #include +#include SPICE_VALUE::SPICE_VALUE( const wxString& aString ) { char buf[8] = { 0, }; if( aString.IsEmpty() ) - throw std::invalid_argument( _( "Spice value cannot be empty" ) ); + throw KI_PARAM_ERROR( _( "Spice value cannot be empty" ) ); LOCALE_IO dummy; // All numeric values should be in "C" locale(decimal separator = .) if( sscanf( (const char*) aString.c_str(), "%lf%7s", &m_base, buf ) == 0 ) - throw std::invalid_argument( _( "Invalid Spice value string" ) ); + throw KI_PARAM_ERROR( _( "Invalid Spice value string" ) ); if( *buf == 0 ) { diff --git a/eeschema/widgets/tuner_slider.cpp b/eeschema/widgets/tuner_slider.cpp index 9be5c9e499..24ba8c90a0 100644 --- a/eeschema/widgets/tuner_slider.cpp +++ b/eeschema/widgets/tuner_slider.cpp @@ -36,6 +36,7 @@ TUNER_SLIDER::TUNER_SLIDER( SIM_PLOT_FRAME* aFrame, wxWindow* aParent, SCH_COMPO const wxString compName = aComponent->GetField( REFERENCE )->GetText(); m_name->SetLabel( compName ); m_value = SPICE_VALUE( aComponent->GetField( VALUE )->GetText() ); + m_changed = false; m_spiceName = aFrame->GetExporter()->GetSpiceDevice( compName ).Lower(); @@ -157,7 +158,7 @@ void TUNER_SLIDER::onMaxTextEnter( wxCommandEvent& event ) SPICE_VALUE newMax( m_maxText->GetValue() ); SetMax( newMax ); } - catch( std::exception& ) + catch( const KI_PARAM_ERROR& ) { // Restore the previous value m_maxText->SetValue( m_max.ToOrigString() ); @@ -173,7 +174,7 @@ void TUNER_SLIDER::onValueTextEnter( wxCommandEvent& event ) SetValue( newCur ); m_changed = true; } - catch( std::exception& ) + catch( const KI_PARAM_ERROR& ) { // Restore the previous value m_valueText->SetValue( m_value.ToOrigString() ); @@ -188,7 +189,7 @@ void TUNER_SLIDER::onMinTextEnter( wxCommandEvent& event ) SPICE_VALUE newMin( m_minText->GetValue() ); SetMin( newMin ); } - catch( std::exception& ) + catch( const KI_PARAM_ERROR& ) { // Restore the previous value m_minText->SetValue( m_min.ToOrigString() ); diff --git a/include/ki_exception.h b/include/ki_exception.h index 099aeb406f..f9dce32cf5 100644 --- a/include/ki_exception.h +++ b/include/ki_exception.h @@ -37,6 +37,35 @@ /// macro which captures the "call site" values of __FILE_, __FUNCTION__ & __LINE__ #define THROW_IO_ERROR( msg ) throw IO_ERROR( msg, __FILE__, __FUNCTION__, __LINE__ ) +/** + * class KI_PARAM_ERROR + * is a class used to hold a translatable error message and may be used when throwing exceptions + * containing a translated error message. + */ +class KI_PARAM_ERROR // similar to std::invalid_argument for instance +{ +public: + /** + * Constructor + */ + KI_PARAM_ERROR( const wxString& aMessage ) + { + m_message = aMessage; + } + + KI_PARAM_ERROR() {} + + const wxString What() const + { + return m_message; + } + + virtual ~KI_PARAM_ERROR() throw () {} + +private: + wxString m_message; +}; + /** * Struct IO_ERROR