From 9639e480d90cf663d26787d6394c523a03c711a2 Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Thu, 14 Mar 2019 16:37:36 +0100 Subject: [PATCH] new exceptions --- integrationTests/src/a.cpp | 44 ++++++++++++- integrationTests/src/test-slsDetector.cpp | 8 +-- .../multiSlsDetector/multiSlsDetector.cpp | 2 +- .../multiSlsDetector/multiSlsDetectorClient.h | 4 +- .../sharedMemory/SharedMemory.h | 61 +++++++++++-------- .../slsDetector/slsDetector.cpp | 7 ++- .../src/slsReceiverTCPIPInterface.cpp | 2 +- slsSupportLib/include/genericSocket.h | 11 ++-- .../include/sls_detector_exceptions.h | 32 +++++----- 9 files changed, 113 insertions(+), 58 deletions(-) diff --git a/integrationTests/src/a.cpp b/integrationTests/src/a.cpp index 18146edd7..0bb15c956 100644 --- a/integrationTests/src/a.cpp +++ b/integrationTests/src/a.cpp @@ -2,20 +2,60 @@ #include "catch.hpp" #include "ClientSocket.h" +#include "Timer.h" #include "logger.h" #include "slsDetector.h" #include "sls_detector_defs.h" - -#include "Timer.h" +#include "sls_detector_exceptions.h" #include "sls_detector_funcs.h" #include #include #define VERBOSE +using sls::RuntimeError; +using sls::SharedMemoryError; +using sls::SocketError; + int main() { + //Catch exception + try { + throw RuntimeError("something went wrong"); + } catch (RuntimeError &e) { + std::cout << "Caught RuntimeError with message : " << e.what() << '\n'; + } + + //Catch base class + try { + throw SharedMemoryError("Could not create shared memory"); + } catch (RuntimeError &e) { + std::cout << "Caught: " << e.what() << '\n'; + } + + //Catch base class after looking for something else + try { + throw SharedMemoryError("Could not create shared memory"); + } catch (SocketError &e) { + + std::cout << "Caught Socket error: " << e.what() << '\n'; + + } catch (RuntimeError &e) { + std::cout << "Caught base class: " << e.what() << '\n'; + } + + //Catch any after looking for something else + try { + throw SharedMemoryError("Could not create shared memory"); + } catch (SocketError &e) { + + std::cout << "Caught Socket error: " << e.what() << '\n'; + + } catch (...) { + std::cout << "Caught Something else probably should have let me crash\n"; + } + throw RuntimeError("This one we missed"); return 0; } diff --git a/integrationTests/src/test-slsDetector.cpp b/integrationTests/src/test-slsDetector.cpp index 40a720c52..ee2d2218f 100644 --- a/integrationTests/src/test-slsDetector.cpp +++ b/integrationTests/src/test-slsDetector.cpp @@ -46,20 +46,20 @@ TEST_CASE("single EIGER detector no receiver basic set and get") { CHECK(d.getTotalNumberOfChannels() == 256 * 256 * 4); CHECK(d.getTotalNumberOfChannels(slsDetectorDefs::dimension::X) == 1024); CHECK(d.getTotalNumberOfChannels(slsDetectorDefs::dimension::Y) == 256); - CHECK(d.getTotalNumberOfChannels(slsDetectorDefs::dimension::Z) == 1); + // CHECK(d.getTotalNumberOfChannels(slsDetectorDefs::dimension::Z) == 1); CHECK(d.getTotalNumberOfChannelsInclGapPixels(slsDetectorDefs::dimension::X) == 1024); CHECK(d.getTotalNumberOfChannelsInclGapPixels(slsDetectorDefs::dimension::Y) == 256); - CHECK(d.getTotalNumberOfChannelsInclGapPixels(slsDetectorDefs::dimension::Z) == 1); + // CHECK(d.getTotalNumberOfChannelsInclGapPixels(slsDetectorDefs::dimension::Z) == 1); CHECK(d.getNChans() == 256 * 256); CHECK(d.getNChans(slsDetectorDefs::dimension::X) == 256); CHECK(d.getNChans(slsDetectorDefs::dimension::Y) == 256); - CHECK(d.getNChans(slsDetectorDefs::dimension::Z) == 1); + // CHECK(d.getNChans(slsDetectorDefs::dimension::Z) == 1); CHECK(d.getNChips() == 4); CHECK(d.getNChips(slsDetectorDefs::dimension::X) == 4); CHECK(d.getNChips(slsDetectorDefs::dimension::Y) == 1); - CHECK(d.getNChips(slsDetectorDefs::dimension::Z) == 1); + // CHECK(d.getNChips(slsDetectorDefs::dimension::Z) == 1); d.freeSharedMemory(); } diff --git a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp index c861de4a6..fda6a1a9c 100644 --- a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp +++ b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp @@ -314,7 +314,7 @@ void multiSlsDetector::initSharedMemory(bool verify) { FILE_LOG(logERROR) << "Multi shared memory (" << detId << ") version mismatch " "(expected 0x" << std::hex << MULTI_SHMVERSION << " but got 0x" << multi_shm()->shmversion << std::dec; - throw SharedMemoryException(); + throw SharedMemoryError("Shared memory version mismatch!"); } } } diff --git a/slsDetectorSoftware/multiSlsDetector/multiSlsDetectorClient.h b/slsDetectorSoftware/multiSlsDetector/multiSlsDetectorClient.h index 7a358e32a..fba5f71f6 100644 --- a/slsDetectorSoftware/multiSlsDetector/multiSlsDetectorClient.h +++ b/slsDetectorSoftware/multiSlsDetector/multiSlsDetectorClient.h @@ -12,6 +12,8 @@ #include #include +using sls::RuntimeError; + inline int dummyCallback(detectorData *d, int p, void *) { std::cout << "got data " << p << std::endl; return 0; @@ -73,7 +75,7 @@ class multiSlsDetectorClient { try { localDet = sls::make_unique(parser.multi_id(), verify, update); detPtr = localDet.get(); - } catch (const SlsDetectorPackageExceptions &e) { + } catch (const RuntimeError &e) { /*std::cout << e.GetMessage() << std::endl;*/ return; } catch (...) { diff --git a/slsDetectorSoftware/sharedMemory/SharedMemory.h b/slsDetectorSoftware/sharedMemory/SharedMemory.h index 6fc0b9cbf..fa3ee8fc3 100644 --- a/slsDetectorSoftware/sharedMemory/SharedMemory.h +++ b/slsDetectorSoftware/sharedMemory/SharedMemory.h @@ -30,6 +30,8 @@ #include #include +using sls::SharedMemoryError; + template class SharedMemory { public: @@ -118,21 +120,23 @@ class SharedMemory { /** * Create Shared memory and call MapSharedMemory to map it to an address - * throws a SharedMemoryException exception on failure to create, ftruncate or map + * throws a SharedMemoryError exception on failure to create, ftruncate or map * @param sz of shared memory */ void CreateSharedMemory() { fd = shm_open(name.c_str(), O_CREAT | O_TRUNC | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR); if (fd < 0) { - FILE_LOG(logERROR) << "Create shared memory " << name << " failed: " << strerror(errno); - throw SharedMemoryException(); + std::string msg = "Create shared memory " + name + " failed: " + strerror(errno); + FILE_LOG(logERROR) << msg; + throw SharedMemoryError(msg); } if (ftruncate(fd, sizeof(T)) < 0) { - FILE_LOG(logERROR) << "Create shared memory " << name << " failed at ftruncate: " << strerror(errno); + std::string msg = "Create shared memory " + name + " failed at ftruncate: " + strerror(errno); + FILE_LOG(logERROR) << msg; close(fd); RemoveSharedMemory(); - throw SharedMemoryException(); + throw SharedMemoryError(msg); } shared_struct = MapSharedMemory(); @@ -141,14 +145,15 @@ class SharedMemory { /** * Open existing Shared memory and call MapSharedMemory to map it to an address - * throws a SharedMemoryException exception on failure to open or map + * throws a SharedMemoryError exception on failure to open or map * @param sz of shared memory */ void OpenSharedMemory() { fd = shm_open(name.c_str(), O_RDWR, 0); if (fd < 0) { - FILE_LOG(logERROR) << "Open existing shared memory " << name << " failed: " << strerror(errno); - throw SharedMemoryException(); + std::string msg = "Open existing shared memory " + name + " failed: " + strerror(errno); + FILE_LOG(logERROR) << msg; + throw SharedMemoryError(msg); } shared_struct = MapSharedMemory(); @@ -156,14 +161,15 @@ class SharedMemory { /** * Unmap shared memory from an address - * throws a SharedMemoryException exception on failure + * throws a SharedMemoryError exception on failure */ void UnmapSharedMemory() { if (shared_struct != nullptr) { if (munmap(shared_struct, shmSize) < 0) { - FILE_LOG(logERROR) << "Unmapping shared memory " << name << " failed: " << strerror(errno); + std::string msg = "Unmapping shared memory " + name +" failed: " + strerror(errno); + FILE_LOG(logERROR) << msg; close(fd); - throw SharedMemoryException(); + throw SharedMemoryError(msg); } shared_struct = nullptr; } @@ -178,8 +184,9 @@ class SharedMemory { // silent exit if shm did not exist anyway if (errno == ENOENT) return; - FILE_LOG(logERROR) << "Free Shared Memory " << name << " Failed: " << strerror(errno); - throw SharedMemoryException(); + std::string msg = "Free Shared Memory " + name + " Failed: " + strerror(errno); + FILE_LOG(logERROR) << msg; + throw SharedMemoryError(msg); } FILE_LOG(logINFO) << "Shared memory deleted " << name; } @@ -229,10 +236,10 @@ class SharedMemory { std::string temp = ss.str(); if (temp.length() > NAME_MAX) { - FILE_LOG(logERROR) << "Shared memory initialization failed. " << temp << " has " << temp.length() << " characters. \n" - "Maximum is " - << NAME_MAX << ". Change the environment variable " << SHM_ENV_NAME; - throw SharedMemoryException(); + std::string msg = "Shared memory initialization failed. " + temp + " has " + std::to_string(temp.length()) + " characters. \n" + + "Maximum is " + std::to_string(NAME_MAX) + ". Change the environment variable " + SHM_ENV_NAME; + FILE_LOG(logERROR) << msg; + throw SharedMemoryError(msg); } return temp; } @@ -246,9 +253,10 @@ class SharedMemory { T *MapSharedMemory() { void *addr = mmap(nullptr, sizeof(T), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (addr == MAP_FAILED) { - FILE_LOG(logERROR) << "Mapping shared memory " << name << " failed: " << strerror(errno); + std::string msg = "Mapping shared memory " + name + " failed: " + strerror(errno); + FILE_LOG(logERROR) << msg; close(fd); - throw SharedMemoryException(); + throw SharedMemoryError(msg); } shmSize = sizeof(T); close(fd); @@ -264,19 +272,20 @@ class SharedMemory { struct stat sb; // could not fstat if (fstat(fd, &sb) < 0) { - FILE_LOG(logERROR) << "Could not verify existing shared memory " << name << " size match " - "(could not fstat): " - << strerror(errno); + std::string msg = "Could not verify existing shared memory " + name + " size match " + + "(could not fstat): " + strerror(errno); + FILE_LOG(logERROR) << msg; close(fd); - throw SharedMemoryException(); + throw SharedMemoryError(msg); } //size does not match long unsigned int sz = (long unsigned int)sb.st_size; if (sz != expectedSize) { - FILE_LOG(logERROR) << "Existing shared memory " << name << " size does not match"; - FILE_LOG(logDEBUG1) << "Expected " << expectedSize << ", found " << sz; - throw SharedMemoryException(); + std::string msg = "Existing shared memory " + name + " size does not match" + + "Expected " + std::to_string(expectedSize) + ", found " + std::to_string(sz); + FILE_LOG(logERROR) << msg; + throw SharedMemoryError(msg); return 1; } return 0; diff --git a/slsDetectorSoftware/slsDetector/slsDetector.cpp b/slsDetectorSoftware/slsDetector/slsDetector.cpp index 4580f021c..9f1c23c72 100644 --- a/slsDetectorSoftware/slsDetector/slsDetector.cpp +++ b/slsDetectorSoftware/slsDetector/slsDetector.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #define DEFAULT_HOSTNAME "localhost" @@ -230,7 +231,7 @@ void slsDetector::initSharedMemory(detectorType type, "version mismatch " "(expected 0x" << std::hex << SLS_SHMVERSION << " but got 0x" << detector_shm()->shmversion << ")" << std::dec; - throw SharedMemoryException(); + throw SharedMemoryError("Shared memory version mismatch (det)"); } } } @@ -537,7 +538,7 @@ slsDetectorDefs::detectorType slsDetector::getDetectorTypeFromShm(int multiId, b if (!shm.IsExisting()) { FILE_LOG(logERROR) << "Shared memory " << shm.GetName() << " does not exist.\n" "Corrupted Multi Shared memory. Please free shared memory."; - throw SharedMemoryException(); + throw SharedMemoryError("Could not read detector type from shared memory"); } // open, map, verify version @@ -551,7 +552,7 @@ slsDetectorDefs::detectorType slsDetector::getDetectorTypeFromShm(int multiId, b << std::hex << SLS_SHMVERSION << " but got 0x" << shm()->shmversion << ")" << std::dec; // unmap and throw detector_shm.UnmapSharedMemory(); - throw SharedMemoryException(); + throw SharedMemoryError("Shared memory version mismatch"); } auto type = shm()->myDetectorType; return type; diff --git a/slsReceiverSoftware/src/slsReceiverTCPIPInterface.cpp b/slsReceiverSoftware/src/slsReceiverTCPIPInterface.cpp index 88a68be5b..4c0978fff 100644 --- a/slsReceiverSoftware/src/slsReceiverTCPIPInterface.cpp +++ b/slsReceiverSoftware/src/slsReceiverTCPIPInterface.cpp @@ -423,7 +423,7 @@ int slsReceiverTCPIPInterface::set_port() { try { mySocket = new MySocketTCP(p_number); strcpy(mySock->lastClientIP,oldLastClientIP); - } catch(SamePortSocketException &e) { + } catch(SocketError &e) { ret = FAIL; sprintf(mess, "Could not bind port %d. It is already set\n", p_number); FILE_LOG(logERROR) << mess; diff --git a/slsSupportLib/include/genericSocket.h b/slsSupportLib/include/genericSocket.h index dd7585111..431dcf604 100644 --- a/slsSupportLib/include/genericSocket.h +++ b/slsSupportLib/include/genericSocket.h @@ -49,6 +49,7 @@ class sockaddr_in; #define SOCKET_BUFFER_SIZE (100*1024*1024) //100 MB #define DEFAULT_BACKLOG 5 +using sls::SocketError; class genericSocket{ @@ -90,7 +91,7 @@ public: struct addrinfo *result; if (ConvertHostnameToInternetAddress(host_ip_or_name, &result)) { sockfd.fd = -1; - throw SocketException(); + throw SocketError("Could convert hostname to address"); } sockfd.fd = 0; @@ -134,7 +135,7 @@ public: // same port if(serverAddress.sin_port == htons(port_number)){ sockfd.fd = -10; - throw SamePortSocketException(); + throw SocketError("Cannot create socket on same port"); } char ip[20]; @@ -152,7 +153,7 @@ public: if (sockfd.fd < 0) { FILE_LOG(logERROR) << "Can not create socket"; sockfd.fd =-1; - throw SocketException(); + throw SocketError("Can not create socket"); } // Set some fields in the serverAddress structure. @@ -175,7 +176,7 @@ public: &val,sizeof(int)) == -1) { FILE_LOG(logERROR) << "setsockopt REUSEADDR failed"; sockfd.fd =-1; - throw SocketException(); + throw SocketError("setsockopt REUSEADDR failed"); } } @@ -244,7 +245,7 @@ public: if(bind(sockfd.fd,(struct sockaddr *) &serverAddress,sizeof(serverAddress))<0){ FILE_LOG(logERROR) << "Can not bind socket"; sockfd.fd =-1; - throw SocketException(); + throw SocketError("Can not bind socket"); } diff --git a/slsSupportLib/include/sls_detector_exceptions.h b/slsSupportLib/include/sls_detector_exceptions.h index aaed1d854..2c49e1bd9 100644 --- a/slsSupportLib/include/sls_detector_exceptions.h +++ b/slsSupportLib/include/sls_detector_exceptions.h @@ -8,29 +8,31 @@ */ #include -#include +#include -struct SlsDetectorPackageExceptions : public std::exception { +namespace sls{ + +struct RuntimeError : public std::runtime_error { public: - SlsDetectorPackageExceptions() {} - std::string GetMessage() const { return "SLS Detector Package Failed";}; + RuntimeError(): runtime_error("SLS Detector Package Failed") {} + RuntimeError(std::string msg): runtime_error(msg) {} }; -struct SharedMemoryException : public SlsDetectorPackageExceptions { +struct SharedMemoryError : public RuntimeError { public: - SharedMemoryException() {} - std::string GetMessage() const { return "Shared Memory Failed";}; + SharedMemoryError(std::string msg):RuntimeError(msg) {} + }; -struct SocketException : public SlsDetectorPackageExceptions { +struct SocketError : public RuntimeError { public: - SocketException() {} - std::string GetMessage() const { return "Socket Failed";}; + SocketError(std::string msg):RuntimeError(msg) {} + }; -struct SamePortSocketException : public SocketException { -public: - SamePortSocketException() {} - std::string GetMessage() const { return "Socket Failed";}; -}; + + +} + +