changeset 1733:e1f5ab395297

PrintErrors
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 21 Oct 2015 14:10:55 +0200
parents 5cdea1cd2071
children e2675b37eb01
files OrthancServer/main.cpp Resources/GenerateErrorCodes.py
diffstat 2 files changed, 60 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/main.cpp	Wed Oct 21 11:53:26 2015 +0200
+++ b/OrthancServer/main.cpp	Wed Oct 21 14:10:55 2015 +0200
@@ -387,7 +387,7 @@
 
 
 
-static void PrintHelp(char* path)
+static void PrintHelp(const char* path)
 {
   std::cout 
     << "Usage: " << path << " [OPTION]... [CONFIGURATION]" << std::endl
@@ -404,6 +404,7 @@
     << "  --logdir=[dir]\tdirectory where to store the log files" << std::endl
     << "\t\t\t(if not used, the logs are dumped to stderr)" << std::endl
     << "  --config=[file]\tcreate a sample configuration file and exit" << std::endl
+    << "  --errors\t\tprint the supported error codes and exit" << std::endl
     << "  --verbose\t\tbe verbose in logs" << std::endl
     << "  --trace\t\thighest verbosity in logs (for debug)" << std::endl
     << "  --upgrade\t\tallow Orthanc to upgrade the version of the" << std::endl
@@ -413,12 +414,16 @@
     << std::endl
     << "Exit status:" << std::endl
     << "   0 if success," << std::endl
+#if defined(_WIN32)
+    << "!= 0 if error (use the --errors option to get the list of possible errors)." << std::endl
+#else
     << "  -1 if error (have a look at the logs)." << std::endl
+#endif
     << std::endl;
 }
 
 
-static void PrintVersion(char* path)
+static void PrintVersion(const char* path)
 {
   std::cout
     << path << " " << ORTHANC_VERSION << std::endl
@@ -431,6 +436,32 @@
 }
 
 
+static void PrintErrorCode(ErrorCode code, const char* description)
+{
+  std::cout 
+    << std::right << std::setw(16) 
+    << static_cast<int>(code)
+    << "   " << description << std::endl;
+}
+
+
+static void PrintErrors(const char* path)
+{
+  std::cout
+    << path << " " << ORTHANC_VERSION << std::endl
+    << "Orthanc, lightweight, RESTful DICOM server for healthcare and medical research." << std::endl
+    << "List of error codes that could be returned by Orthanc:" << std::endl << std::endl;
+
+  // The content of the following brackets is automatically generated
+  // by the "GenerateErrorCodes.py" script
+  {
+    PrintErrorCode(ErrorCode_BadFileFormat, "tutu");
+  }
+
+  std::cout << std::endl;
+}
+
+
 
 static void LoadLuaScripts(ServerContext& context)
 {
@@ -853,6 +884,11 @@
         configurationFile = argv[i];
       }
     }
+    else if (argument == "--errors")
+    {
+      PrintErrors(argv[0]);
+      return 0;
+    }
     else if (argument == "--help")
     {
       PrintHelp(argv[0]);
@@ -937,8 +973,12 @@
   }
   catch (const OrthancException& e)
   {
-    LOG(ERROR) << "Uncaught exception, stopping now: [" << e.What() << "]";
+    LOG(ERROR) << "Uncaught exception, stopping now: [" << e.What() << "] (code " << e.GetErrorCode() << ")";
+#if defined(_WIN32)
+    status = static_cast<int>(e.GetErrorCode());
+#else
     status = -1;
+#endif
   }
   catch (const std::exception& e) 
   {
--- a/Resources/GenerateErrorCodes.py	Wed Oct 21 11:53:26 2015 +0200
+++ b/Resources/GenerateErrorCodes.py	Wed Oct 21 14:10:55 2015 +0200
@@ -141,3 +141,20 @@
 
 with open(path, 'w') as f:
     f.write(a)
+
+
+
+##
+## Generate the "PrintErrors" function in "main.cpp"
+##
+
+path = os.path.join(BASE, 'OrthancServer', 'main.cpp')
+with open(path, 'r') as f:
+    a = f.read()
+
+s = '\n'.join(map(lambda x: '    PrintError(%d, "%s");' % (x['Code'], x['Name']), ERRORS))
+
+print s
+
+#with open(path, 'w') as f:
+#    f.write(a)