Create a central exception handler we can also use to generate sentry events

(cherry picked from commit c5d5450f55)
This commit is contained in:
Marek Roszko 2023-04-19 19:51:33 -04:00 committed by Mark Roszko
parent 4a274709e7
commit 0c6eb6dec3
3 changed files with 44 additions and 33 deletions

View File

@ -55,6 +55,7 @@
#include <kicad_curl/kicad_curl.h>
#include <kiplatform/policy.h>
#include <lockfile.h>
#include <macros.h>
#include <menus_helpers.h>
#include <paths.h>
#include <pgm_base.h>
@ -880,4 +881,36 @@ bool PGM_BASE::IsGUI()
bool run_gui = wxTheApp->GetClassName() != KICAD_CLI_APP_NAME;
return run_gui;
#endif
}
void PGM_BASE::HandleException( std::exception_ptr aPtr )
{
try
{
if( aPtr )
std::rethrow_exception( aPtr );
}
catch( const IO_ERROR& ioe )
{
wxLogError( ioe.What() );
}
catch( const std::exception& e )
{
#ifdef KICAD_USE_SENTRY
sentry_value_t exc = sentry_value_new_exception( "exception", e.what() );
sentry_value_set_stacktrace( exc, NULL, 0 );
sentry_value_t sentryEvent = sentry_value_new_event();
sentry_event_add_exception( sentryEvent, exc );
sentry_capture_event( sentryEvent );
#endif
wxLogError( wxT( "Unhandled exception class: %s what: %s" ),
FROM_UTF8( typeid( e ).name() ), FROM_UTF8( e.what() ) );
}
catch( ... )
{
wxLogError( wxT( "Unhandled exception of unknown type" ) );
}
}

View File

@ -52,6 +52,9 @@
#include <kiplatform/app.h>
#include <kiplatform/environment.h>
#ifdef KICAD_USE_SENTRY
#include <sentry.h>
#endif
// Only a single KIWAY is supported in this single_top top level component,
// which is dedicated to loading only a single DSO.
@ -159,18 +162,9 @@ struct APP_SINGLE_TOP : public wxApp
{
return program.OnPgmInit();
}
catch( const std::exception& e )
catch( ... )
{
wxLogError( wxT( "Unhandled exception class: %s what: %s" ),
FROM_UTF8( typeid( e ).name() ), FROM_UTF8( e.what() ) );
}
catch( const IO_ERROR& ioe )
{
wxLogError( ioe.What() );
}
catch(...)
{
wxLogError( wxT( "Unhandled exception of unknown type" ) );
Pgm().HandleException( std::current_exception() );
}
program.OnPgmExit();
@ -192,18 +186,9 @@ struct APP_SINGLE_TOP : public wxApp
{
ret = wxApp::OnRun();
}
catch( const std::exception& e )
{
wxLogError( wxT( "Unhandled exception class: %s what: %s" ),
FROM_UTF8( typeid( e ).name() ), FROM_UTF8( e.what() ) );
}
catch( const IO_ERROR& ioe )
{
wxLogError( ioe.What() );
}
catch(...)
{
wxLogError( wxT( "Unhandled exception of unknown type" ) );
Pgm().HandleException( std::current_exception() );
}
return ret;
@ -254,19 +239,9 @@ struct APP_SINGLE_TOP : public wxApp
{
throw;
}
catch( const std::exception& e )
catch( ... )
{
wxLogError( "Unhandled exception class: %s what: %s",
FROM_UTF8( typeid(e).name() ),
FROM_UTF8( e.what() ) );
}
catch( const IO_ERROR& ioe )
{
wxLogError( ioe.What() );
}
catch(...)
{
wxLogError( "Unhandled exception of unknown type" );
Pgm().HandleException( std::current_exception() );
}
return false; // continue on. Return false to abort program

View File

@ -31,6 +31,7 @@
#ifndef PGM_BASE_H_
#define PGM_BASE_H_
#include <exception>
#include <map>
#include <vector>
#include <memory>
@ -303,6 +304,8 @@ public:
const wxString& GetSentryId();
#endif
void HandleException( std::exception_ptr ptr );
/**
* Determine if the application is running with a GUI
*