[Spice-devel] [PATCH spice-streaming-agent v2 3/9] Implement an exception hierarchy for ReadError
Lukáš Hrázký
lhrazky at redhat.com
Wed May 16 16:26:01 UTC 2018
Introduces an exception hierarchy up to a ReadError class, which is
thrown from read_all().
Signed-off-by: Lukáš Hrázký <lhrazky at redhat.com>
---
src/Makefile.am | 2 ++
src/error.cpp | 22 ++++++++++++++++++++++
src/error.hpp | 37 +++++++++++++++++++++++++++++++++++++
src/spice-streaming-agent.cpp | 2 +-
src/stream-port.cpp | 4 ++--
5 files changed, 64 insertions(+), 3 deletions(-)
create mode 100644 src/error.cpp
create mode 100644 src/error.hpp
diff --git a/src/Makefile.am b/src/Makefile.am
index 604c1e5..18ed22c 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -52,6 +52,8 @@ spice_streaming_agent_SOURCES = \
spice-streaming-agent.cpp \
concrete-agent.cpp \
concrete-agent.hpp \
+ error.cpp \
+ error.hpp \
mjpeg-fallback.cpp \
mjpeg-fallback.hpp \
jpeg.cpp \
diff --git a/src/error.cpp b/src/error.cpp
new file mode 100644
index 0000000..7d38033
--- /dev/null
+++ b/src/error.cpp
@@ -0,0 +1,22 @@
+/* The errors module.
+ *
+ * \copyright
+ * Copyright 2018 Red Hat Inc. All rights reserved.
+ */
+
+#include "error.hpp"
+
+#include <string.h>
+#include <syslog.h>
+
+
+namespace spice {
+namespace streaming_agent {
+
+Error::Error(const std::string &message) : std::runtime_error(message) {}
+
+IOError::IOError(const std::string &msg, int sys_errno) :
+ Error(msg + ": " + std::to_string(sys_errno) + " - " + strerror(sys_errno))
+{}
+
+}} // namespace spice::streaming_agent
diff --git a/src/error.hpp b/src/error.hpp
new file mode 100644
index 0000000..54ae527
--- /dev/null
+++ b/src/error.hpp
@@ -0,0 +1,37 @@
+/* The errors module.
+ *
+ * \copyright
+ * Copyright 2018 Red Hat Inc. All rights reserved.
+ */
+
+#ifndef SPICE_STREAMING_AGENT_ERROR_HPP
+#define SPICE_STREAMING_AGENT_ERROR_HPP
+
+#include <stdexcept>
+#include <string>
+
+
+namespace spice {
+namespace streaming_agent {
+
+class Error : public std::runtime_error
+{
+public:
+ Error(const std::string &message);
+};
+
+class IOError : public Error
+{
+public:
+ IOError(const std::string &msg, int sys_errno);
+};
+
+class ReadError : public IOError
+{
+public:
+ using IOError::IOError;
+};
+
+}} // namespace spice::streaming_agent
+
+#endif // SPICE_STREAMING_AGENT_ERROR_HPP
diff --git a/src/spice-streaming-agent.cpp b/src/spice-streaming-agent.cpp
index 7b166d3..2c0340d 100644
--- a/src/spice-streaming-agent.cpp
+++ b/src/spice-streaming-agent.cpp
@@ -535,7 +535,7 @@ int main(int argc, char* argv[])
try {
do_capture(streamport, f_log);
}
- catch (std::runtime_error &err) {
+ catch (std::exception &err) {
syslog(LOG_ERR, "%s\n", err.what());
ret = EXIT_FAILURE;
}
diff --git a/src/stream-port.cpp b/src/stream-port.cpp
index 3699d92..f256698 100644
--- a/src/stream-port.cpp
+++ b/src/stream-port.cpp
@@ -5,6 +5,7 @@
*/
#include "stream-port.hpp"
+#include "error.hpp"
#include <errno.h>
#include <string.h>
@@ -25,8 +26,7 @@ void read_all(int fd, void *msg, size_t len)
if (errno == EINTR) {
continue;
}
- throw std::runtime_error("Reading message from device failed: " +
- std::string(strerror(errno)));
+ throw ReadError("Reading message from device failed", errno);
}
len -= n;
--
2.16.2
More information about the Spice-devel
mailing list