From 8f93c36466f2d69791139febea2ec7aed82d489e Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@duempel.org>
Date: Tue, 15 Dec 2015 23:17:34 +0100
Subject: [PATCH] command/AllCommands: catch and report std::exception

---
 src/command/AllCommands.cxx  | 14 ++++++++++----
 src/command/CommandError.cxx | 16 ++++++++++++++++
 src/command/CommandError.hxx |  7 +++++++
 3 files changed, 33 insertions(+), 4 deletions(-)

diff --git a/src/command/AllCommands.cxx b/src/command/AllCommands.cxx
index 87ed7a1dc..9dd1b0a94 100644
--- a/src/command/AllCommands.cxx
+++ b/src/command/AllCommands.cxx
@@ -19,6 +19,7 @@
 
 #include "config.h"
 #include "AllCommands.hxx"
+#include "CommandError.hxx"
 #include "Request.hxx"
 #include "QueueCommands.hxx"
 #include "TagCommands.hxx"
@@ -411,9 +412,14 @@ command_process(Client &client, unsigned num, char *line)
 		command_checked_lookup(r, client.GetPermission(),
 				       cmd_name, args);
 
-	CommandResult ret = cmd
-		? cmd->handler(client, args, r)
-		: CommandResult::ERROR;
+	try {
+		CommandResult ret = cmd
+			? cmd->handler(client, args, r)
+			: CommandResult::ERROR;
 
-	return ret;
+		return ret;
+	} catch (const std::exception &e) {
+		PrintError(r, e);
+		return CommandResult::ERROR;
+	}
 }
diff --git a/src/command/CommandError.cxx b/src/command/CommandError.cxx
index 7ed23d864..812cb11b2 100644
--- a/src/command/CommandError.cxx
+++ b/src/command/CommandError.cxx
@@ -25,6 +25,8 @@
 #include "util/Error.hxx"
 #include "Log.hxx"
 
+#include <system_error>
+
 #include <assert.h>
 #include <string.h>
 #include <errno.h>
@@ -155,3 +157,17 @@ print_error(Response &r, const Error &error)
 	r.Error(ToAck(error), error.GetMessage());
 	return CommandResult::ERROR;
 }
+
+void
+PrintError(Response &r, const std::exception &e)
+{
+	LogError(e);
+
+	try {
+		throw e;
+	} catch (const std::system_error &) {
+		r.Error(ACK_ERROR_SYSTEM, e.what());
+	} catch (...) {
+		r.Error(ACK_ERROR_UNKNOWN, e.what());
+	}
+}
diff --git a/src/command/CommandError.hxx b/src/command/CommandError.hxx
index e33386078..5fc547339 100644
--- a/src/command/CommandError.hxx
+++ b/src/command/CommandError.hxx
@@ -23,6 +23,10 @@
 #include "CommandResult.hxx"
 #include "PlaylistError.hxx"
 
+namespace std {
+	class exception;
+}
+
 class Response;
 class Error;
 
@@ -35,4 +39,7 @@ print_playlist_result(Response &r, PlaylistResult result);
 CommandResult
 print_error(Response &r, const Error &error);
 
+void
+PrintError(Response &r, const std::exception &e);
+
 #endif