HDFS-9741: libhdfs++: GetLastError not returning meaningful messages after some 
failures.  Contributed by Bob Hansen


Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/038e01f5
Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/038e01f5
Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/038e01f5

Branch: refs/heads/HDFS-8707
Commit: 038e01f5e9e0e59e9234383d439e711dafa76817
Parents: 4d09d2d
Author: Bob Hansen <b...@hp.com>
Authored: Wed Feb 3 16:41:15 2016 -0500
Committer: Bob Hansen <b...@hp.com>
Committed: Wed Feb 3 16:41:15 2016 -0500

----------------------------------------------------------------------
 .../native/libhdfspp/lib/bindings/c/hdfs.cc     | 33 +++++++++++++++-----
 1 file changed, 25 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hadoop/blob/038e01f5/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/lib/bindings/c/hdfs.cc
----------------------------------------------------------------------
diff --git 
a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/lib/bindings/c/hdfs.cc
 
b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/lib/bindings/c/hdfs.cc
index 339c7fe..23e1f0a 100644
--- 
a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/lib/bindings/c/hdfs.cc
+++ 
b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/lib/bindings/c/hdfs.cc
@@ -61,7 +61,7 @@ thread_local std::string errstr;
 
 /* Fetch last error that happened in this thread */
 void hdfsGetLastError(char *buf, int len) {
-  if(nullptr == buf || len < 1 || errstr.empty()) {
+  if(nullptr == buf || len < 1) {
     return;
   }
 
@@ -93,7 +93,7 @@ struct hdfsBuilder {
 };
 
 /* Error handling with optional debug to stderr */
-static void ReportError(int errnum, std::string msg) {
+static void ReportError(int errnum, const std::string & msg) {
   errno = errnum;
   errstr = msg;
 #ifdef LIBHDFSPP_C_API_ENABLE_DEBUG
@@ -106,24 +106,32 @@ static void ReportError(int errnum, std::string msg) {
 
 /* Convert Status wrapped error into appropriate errno and return code */
 static int Error(const Status &stat) {
+  const char * default_message;
+  int errnum;
+
   int code = stat.code();
   switch (code) {
     case Status::Code::kOk:
       return 0;
     case Status::Code::kInvalidArgument:
-      ReportError(EINVAL, "Invalid argument");
+      errnum = EINVAL;
+      default_message = "Invalid argument";
       break;
     case Status::Code::kResourceUnavailable:
-      ReportError(EAGAIN, "Resource temporarily unavailable");
+      errnum = EAGAIN;
+      default_message = "Resource temporarily unavailable";
       break;
     case Status::Code::kUnimplemented:
-      ReportError(ENOSYS, "Function not implemented");
+      errnum = ENOSYS;
+      default_message = "Function not implemented";
       break;
     case Status::Code::kException:
-      ReportError(EINTR, "Exception raised");
+      errnum = EINTR;
+      default_message = "Exception raised";
       break;
     case Status::Code::kOperationCanceled:
-      ReportError(EINTR, "Operation canceled");
+      errnum = EINTR;
+      default_message = "Operation canceled";
       break;
     case Status::Code::kPermissionDenied:
       if (!stat.ToString().empty())
@@ -132,8 +140,13 @@ static int Error(const Status &stat) {
         ReportError(EACCES, "Permission denied");
       break;
     default:
-      ReportError(ENOSYS, "Error: unrecognised code");
+      errnum = ENOSYS;
+      default_message = "Error: unrecognised code";
   }
+  if (stat.ToString().empty())
+    ReportError(errnum, default_message);
+  else
+    ReportError(errnum, stat.ToString());
   return -1;
 }
 
@@ -176,6 +189,7 @@ hdfsFS hdfsConnectAsUser(const char* nn, tPort port, const 
char *user) {
 
   FileSystem *fs = FileSystem::New(io_service, user_name, Options());
   if (!fs) {
+    ReportError(ENODEV, "Could not create FileSystem object");
     return nullptr;
   }
 
@@ -217,6 +231,7 @@ hdfsFile hdfsOpenFile(hdfsFS fs, const char *path, int 
flags, int bufferSize,
   FileHandle *f = nullptr;
   Status stat = fs->get_impl()->Open(path, &f);
   if (!stat.ok()) {
+    Error(stat);
     return nullptr;
   }
   return new hdfsFile_internal(f);
@@ -365,6 +380,7 @@ int hdfsBuilderConfSetStr(struct hdfsBuilder *bld, const 
char *key,
   }
   else
   {
+    ReportError(EINVAL, "Could not change Builder value");
     return 1;
   }
 }
@@ -454,5 +470,6 @@ int hdfsBuilderConfGetInt(struct hdfsBuilder *bld, const 
char *key, int32_t *val
     *val = *value;
   }
   // If not found, don't change val
+  ReportError(EINVAL, "Could not get Builder value");
   return 0;
 }

Reply via email to