introduce AwkException v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 25 May 2019 17:00:51 +0200
branchv_0
changeset 28 4fdbe30d8c58
parent 27 86d8bbc99e7b
child 29 b3d1a671315b
introduce AwkException
nbproject/configurations.xml
src/AwkException.h
src/AwkHandler.h
src/relpipe-tr-awk.cpp
--- a/nbproject/configurations.xml	Sat May 25 11:36:31 2019 +0200
+++ b/nbproject/configurations.xml	Sat May 25 17:00:51 2019 +0200
@@ -42,6 +42,7 @@
   <logicalFolder name="root" displayName="root" projectFiles="true" kind="ROOT">
     <df root="." name="0">
       <df name="src">
+        <in>AwkException.h</in>
         <in>relpipe-tr-awk.cpp</in>
       </df>
     </df>
@@ -92,6 +93,8 @@
           <preBuildFirst>true</preBuildFirst>
         </preBuild>
       </makefileType>
+      <item path="src/AwkException.h" ex="false" tool="3" flavor2="0">
+      </item>
       <item path="src/relpipe-tr-awk.cpp" ex="false" tool="1" flavor2="0">
         <ccTool flags="0">
         </ccTool>
@@ -132,6 +135,8 @@
           <preBuildFirst>true</preBuildFirst>
         </preBuild>
       </makefileType>
+      <item path="src/AwkException.h" ex="false" tool="3" flavor2="0">
+      </item>
       <item path="src/relpipe-tr-awk.cpp" ex="false" tool="1" flavor2="0">
         <ccTool flags="0">
         </ccTool>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/AwkException.h	Sat May 25 17:00:51 2019 +0200
@@ -0,0 +1,44 @@
+/**
+ * Relational pipes
+ * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#pragma once
+
+#include <string>
+
+using namespace std;
+
+namespace relpipe {
+namespace tr {
+namespace awk {
+
+class AwkException {
+private:
+	wstring message;
+public:
+
+	AwkException(wstring message) : message(message) {
+	}
+
+	wstring getMessge() {
+		return message;
+	}
+
+};
+
+}
+}
+}
\ No newline at end of file
--- a/src/AwkHandler.h	Sat May 25 11:36:31 2019 +0200
+++ b/src/AwkHandler.h	Sat May 25 17:00:51 2019 +0200
@@ -41,6 +41,7 @@
 #include <relpipe/cli/RelpipeCLIException.h>
 
 #include "Configuration.h"
+#include "AwkException.h"
 
 namespace relpipe {
 namespace tr {
@@ -78,17 +79,17 @@
 		int result = pipe(fds);
 		readerFD = fds[0];
 		writerFD = fds[1];
-		if (result < 0) throw cli::RelpipeCLIException(L"Unable to create a pipe.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions?
+		if (result < 0) throw AwkException(L"Unable to create a pipe.");
 	}
 
 	void redirectFD(int oldfd, int newfd) {
 		int result = dup2(oldfd, newfd);
-		if (result < 0) throw cli::RelpipeCLIException(L"Unable redirect FD.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions?
+		if (result < 0) throw AwkException(L"Unable redirect FD.");
 	}
 
 	void closeOrThrow(int fd) {
 		int error = close(fd);
-		if (error) throw cli::RelpipeCLIException(L"Unable to close FD: " + to_wstring(fd) + L" from PID: " + to_wstring(getpid()), cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions?
+		if (error) throw AwkException(L"Unable to close FD: " + to_wstring(fd) + L" from PID: " + to_wstring(getpid()));
 	}
 
 	void execp(const std::vector<std::string>& args) {
@@ -99,7 +100,7 @@
 		execvp(a[0], (char*const*) a);
 
 		delete[] a;
-		throw cli::RelpipeCLIException(L"Unable to do execvp().", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions?
+		throw AwkException(L"Unable to do execvp().");
 	}
 
 	/* TODO: move to lib-cli when stable and used in other modules */
@@ -161,7 +162,7 @@
 
 	string_t a2v(const string_t& attributeName) {
 		if (currenVariablesMapping.find(attributeName) != currenVariablesMapping.end()) return currenVariablesMapping[attributeName];
-		else throw cli::RelpipeCLIException(L"Unable to find value in currenVariablesMapping", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions?
+		else throw AwkException(L"Unable to find value in currenVariablesMapping");
 	}
 
 	template <typename K, typename V> bool containsValue(std::map<K, V> map, V value) {
@@ -218,7 +219,7 @@
 					if (ch == 't') currentValue << L'\t';
 					else if (ch == 'n') currentValue << L'\n';
 					else if (ch == '\\') currentValue << L'\\';
-					else throw cli::RelpipeCLIException(L"Unknown escape sequence. Only \\t, \\n and \\\\ are supported.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions?
+					else throw AwkException(L"Unknown escape sequence. Only \\t, \\n and \\\\ are supported.");
 				} else {
 					currentValue << ch;
 				}
@@ -288,7 +289,7 @@
 			__pid_t awkPid = fork();
 
 			if (awkPid < 0) {
-				throw cli::RelpipeCLIException(L"Unable to fork AWK process.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions?
+				throw AwkException(L"Unable to fork AWK process.");
 			} else if (awkPid == 0) {
 				// AWK child process
 				closeOrThrow(awkInputWriterFD);
@@ -379,7 +380,7 @@
 				__pid_t writerPid = fork();
 
 				if (writerPid < 0) {
-					throw cli::RelpipeCLIException(L"Unable to fork Writer process.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions?
+					throw AwkException(L"Unable to fork Writer process.");
 				} else if (writerPid == 0) {
 					// Writer child process
 					closeOrThrow(awkInputWriterFD);
--- a/src/relpipe-tr-awk.cpp	Sat May 25 11:36:31 2019 +0200
+++ b/src/relpipe-tr-awk.cpp	Sat May 25 17:00:51 2019 +0200
@@ -63,6 +63,10 @@
 		fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessge().c_str());
 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
 		resultCode = e.getExitCode();
+	} catch (AwkException& e) {
+		fwprintf(stderr, L"Caught AWK exception: %ls\n", e.getMessge().c_str());
+		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
+		resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
 	} catch (RelpipeReaderException& e) {
 		fwprintf(stderr, L"Caught Reader exception: %ls\n", e.getMessge().c_str());
 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());