6e0fa099b257 — Chris Cannam 4 months ago
Return full error texts
1 files changed, 25 insertions(+), 6 deletions(-)

M ffi/impl-bqaudiostream.cpp
M ffi/impl-bqaudiostream.cpp +25 -6
@@ 51,6 51,25 @@ extern "C" {
 	delete sr;
     }
 
+    static char *format_error(const char *what, const std::string &more) {
+        if (what && what[0]) {
+            if (more == "") {
+                return strdup(what);
+            } else {
+                // 4 == space, open paren, close paren, NUL
+                char *err = (char *)malloc(strlen(what) + more.size() + 4);
+                sprintf(err, "%s (%s)", what, more.c_str());
+                return err;
+            }
+        } else {
+            if (more == "") {
+                return strdup("(unknown error)");
+            } else {
+                return strdup(more.c_str());
+            }
+        }
+    }
+    
     static char **convert_string_vector(vector<string> v) {
 	size_t n = v.size();
 	char **rv = (char **)malloc((n+1) * sizeof(char *));

          
@@ 96,7 115,7 @@ extern "C" {
 	try {
 	    rsr->rstream = AudioReadStreamFactory::createReadStream(fnstr);
 	} catch (const std::exception &e) {
-	    rsr->error_text = strdup(e.what());
+	    rsr->error_text = format_error(e.what(), {});
 	}
 
 	return rsr;

          
@@ 199,7 218,7 @@ extern "C" {
 	stream_rec *rsr = reinterpret_cast<stream_rec *>(sptr);
         if (n <= 0) {
             if (n < 0) {
-                rsr->error_text = strdup("Negative frame count passed to bqar_read_interleaved_frames");
+                rsr->error_text = format_error("Negative frame count passed to bqar_read_interleaved_frames", {});
             }
             return 0;
         }

          
@@ 214,7 233,7 @@ extern "C" {
         try {
             read = int64_t(rsr->rstream->getInterleavedFrames(n, rsr->buffer));
         } catch (const std::exception &e) {
-            rsr->error_text = strdup(e.what());
+            rsr->error_text = format_error(e.what(), rsr->rstream->getError());
         }
 	breakfastquay::v_convert(out, rsr->buffer, read * ch);
 	return read;

          
@@ 272,7 291,7 @@ extern "C" {
 							   channelCount,
 							   sampleRate);
 	} catch (const std::exception &e) {
-	    wsr->error_text = strdup(e.what());
+	    wsr->error_text = format_error(e.what(), {});
 	}
 
 	return reinterpret_cast<Pointer>(wsr);

          
@@ 293,7 312,7 @@ extern "C" {
 	stream_rec *wsr = reinterpret_cast<stream_rec *>(sptr);
         if (n <= 0) {
             if (n < 0) {
-                wsr->error_text = strdup("Negative frame count passed to bqaw_write_interleaved_frames");
+                wsr->error_text = format_error("Negative frame count passed to bqaw_write_interleaved_frames", {});
             }
             return;
         }

          
@@ 308,7 327,7 @@ extern "C" {
         try {
             wsr->wstream->putInterleavedFrames(n, wsr->buffer);
         } catch (const std::exception &e) {
-            wsr->error_text = strdup(e.what());
+            wsr->error_text = format_error(e.what(), wsr->wstream->getError());
         }
     }