From 64777bd0ef385b8aee54ee9bbf5edb3bd9161ba0 Mon Sep 17 00:00:00 2001 From: Jon Trulson Date: Fri, 11 Sep 2015 12:40:59 -0600 Subject: [PATCH] wt5001: throw exception(s) on fatal errors Signed-off-by: Jon Trulson Signed-off-by: Mihai Tudor Panu --- src/wt5001/wt5001.cxx | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/src/wt5001/wt5001.cxx b/src/wt5001/wt5001.cxx index bbcf7597..d60e4cd6 100644 --- a/src/wt5001/wt5001.cxx +++ b/src/wt5001/wt5001.cxx @@ -23,6 +23,9 @@ */ #include +#include +#include +#include #include "wt5001.h" @@ -37,7 +40,8 @@ WT5001::WT5001(int uart) if ( !(m_uart = mraa_uart_init(uart)) ) { - cerr << __FUNCTION__ << ": mraa_uart_init() failed" << endl; + throw std::invalid_argument(std::string(__FUNCTION__) + + ": mraa_uart_init() failed"); return; } @@ -46,15 +50,18 @@ WT5001::WT5001(int uart) if (!devPath) { - cerr << __FUNCTION__ << ": mraa_uart_get_dev_path() failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": mraa_uart_get_dev_path() failed"); return; } // now open the tty if ( (m_ttyFd = open(devPath, O_RDWR)) == -1) { - cerr << __FUNCTION__ << ": open of " << devPath << " failed: " - << strerror(errno) << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": open of " + + string(devPath) + " failed: " + + string(strerror(errno))); return; } } @@ -102,7 +109,12 @@ int WT5001::readData(char *buffer, int len) int rv = read(m_ttyFd, buffer, len); if (rv < 0) - cerr << __FUNCTION__ << ": read failed: " << strerror(errno) << endl; + { + throw std::runtime_error(std::string(__FUNCTION__) + + ": read() failed: " + + string(strerror(errno))); + return rv; + } return rv; } @@ -119,7 +131,9 @@ int WT5001::writeData(char *buffer, int len) if (rv < 0) { - cerr << __FUNCTION__ << ": write failed: " << strerror(errno) << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": write() failed: " + + string(strerror(errno))); return rv; } @@ -149,7 +163,9 @@ bool WT5001::setupTty(speed_t baud) // make it so if (tcsetattr(m_ttyFd, TCSAFLUSH, &termio) < 0) { - cerr << __FUNCTION__ << ": tcsetattr failed: " << strerror(errno) << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": tcsetattr() failed: " + + string(strerror(errno))); return false; } @@ -267,8 +283,13 @@ bool WT5001::setVolume(uint8_t vol) { if (vol > WT5001_MAX_VOLUME) { - cerr << __FUNCTION__ << ": volume must be between 0 and " - << WT5001_MAX_VOLUME << endl; + // C++11 std::to_string() would be nice, but... + std::ostringstream str; + str << WT5001_MAX_VOLUME; + + throw std::out_of_range(std::string(__FUNCTION__) + + ": angle must be between 0 and " + + str.str()); return false; }