From 84f795e453aa54c0d3c6cadd3056f29fd6051486 Mon Sep 17 00:00:00 2001 From: Jon Trulson Date: Fri, 7 Aug 2015 13:27:19 -0600 Subject: [PATCH] upm: add facility for exception handling in upm We add a new src/upm_exception.i interface file for SWIG to catch common exceptions and propagate them through SWIG. src/upm.i is modified to include this interface file, so all UPM drivers have it. In theory, this should be language agnostic - if the target language supports exceptions, then it should just work. Signed-off-by: Jon Trulson Signed-off-by: Mihai Tudor Panu --- src/upm.i | 1 + src/upm_exception.i | 65 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 src/upm_exception.i diff --git a/src/upm.i b/src/upm.i index 28cc4268..3f0ff031 100644 --- a/src/upm.i +++ b/src/upm.i @@ -1,5 +1,6 @@ %include "std_string.i" %include "stdint.i" +%include "upm_exception.i" %typemap(out) mraa_result_t = int; diff --git a/src/upm_exception.i b/src/upm_exception.i new file mode 100644 index 00000000..bc9ebdb9 --- /dev/null +++ b/src/upm_exception.i @@ -0,0 +1,65 @@ +/* Standardized exception handling for UPM + * + * catch blocks should be listed in order from most specific to least + * specific. + */ + +%include "exception.i" + +%exception { + try { + $action + } catch (std::invalid_argument& e) { + std::string s1("UPM Invalid Argument: "), s2(e.what()); + s1 = s1 + s2; + SWIG_exception(SWIG_ValueError, s1.c_str()); + + } catch (std::domain_error& e) { + std::string s1("UPM Domain Error: "), s2(e.what()); + s1 = s1 + s2; + SWIG_exception(SWIG_ValueError, s1.c_str() ); + + } catch (std::overflow_error& e) { + std::string s1("UPM Overflow Error: "), s2(e.what()); + s1 = s1 + s2; + SWIG_exception(SWIG_OverflowError, s1.c_str() ); + + } catch (std::out_of_range& e) { + std::string s1("UPM Out of Range: "), s2(e.what()); + s1 = s1 + s2; + SWIG_exception(SWIG_IndexError, s1.c_str() ); + + } catch (std::length_error& e) { + std::string s1("UPM Length Error: "), s2(e.what()); + s1 = s1 + s2; + SWIG_exception(SWIG_IndexError, s1.c_str() ); + + } catch (std::logic_error& e) { + std::string s1("UPM Logic Error: "), s2(e.what()); + s1 = s1 + s2; + SWIG_exception(SWIG_RuntimeError, s1.c_str() ); + + } catch (std::bad_alloc& e) { + /* for an allocation exception, don't try to create a string... */ + SWIG_exception(SWIG_MemoryError, e.what() ); + + } catch (std::runtime_error& e) { + /* catch other std::runtime_error exceptions here */ + std::string s1("UPM Runtime Error: "), s2(e.what()); + s1 = s1 + s2; + SWIG_exception(SWIG_RuntimeError, s1.c_str()); + + } catch (std::exception& e) { + /* catch other std::exceptions here */ + std::string s1("UPM Error: "), s2(e.what()); + s1 = s1 + s2; + SWIG_exception(SWIG_SystemError, s1.c_str() ); + + } catch (...) { + /* catch everything else */ + SWIG_exception(SWIG_UnknownError, "UPM Unknown exception" ); + + } + +} +