023255de913dda73eeceee882e1b5f824c94f34f
jcasper
  Tue Apr 18 10:52:42 2023 -0700
Need to catch errors on opening a hic file with Straw, because the
magic string check can also generate an exception.  refs #31071

diff --git src/hg/lib/straw/cStraw.cpp src/hg/lib/straw/cStraw.cpp
index b284723..31a663c 100644
--- src/hg/lib/straw/cStraw.cpp
+++ src/hg/lib/straw/cStraw.cpp
@@ -20,32 +20,44 @@
 {
   hicFile->loadHeader();
   genome = hicFile->genome;
   chromNames = hicFile->chrNames;
   chromSizes = hicFile->chrSizes;
   bpResolutions = hicFile->bpResolutions;
   fragResolutions = hicFile->fragResolutions;
   map<std::string,std::string>::iterator loop;
   for (loop=hicFile->attributes.begin(); loop!=hicFile->attributes.end(); loop++) {
     attributes.insert(attributes.end(), loop->first);
     attributes.insert(attributes.end(), loop->second);
   }
 }
 
 
-extern "C" Straw *cStrawOpen(char *fname) {
-    return new Straw(fname);
+extern "C" char *cStrawOpen(char *fname, Straw **p)
+/* Create a Straw object based on the hic file at the provided path and set *p to point to it.
+ * On error, set *p = NULL and return a non-null string describing the error. */
+{
+    try {
+        *p = new Straw(fname);
+    } catch (strawException& err) {
+      char *errMsg = (char*) calloc((size_t) strlen(err.what())+1, sizeof(char));
+      strcpy(errMsg, err.what());
+      delete *p;
+      *p = NULL;
+      return errMsg;
+    }
+    return NULL;
 }
 
 extern "C" void cStrawClose(Straw **hicFile) {
     delete *hicFile;
     *hicFile = NULL;
 }
 
 extern "C" char *cStraw (Straw *hicFile, char *norm, int binsize, char *chr1loc, char *chr2loc, char *unit, int **xActual, int **yActual, double **counts, int *numRecords)
 /* Wrapper function to retrieve a data chunk from a .hic file, for use by C libraries.
  * norm is one of NONE/VC/VC_SQRT/KR.
  * binsize is one of the supported bin sizes as determined by cStrawHeader.
  * chr1loc and chr2loc are the two positions to retrieve interaction data for, specified as chr:start-end.
  * unit is one of BP/FRAG.
  * Values are returned in newly allocated arrays in xActual, yActual, and counts, with the number of
  * returned records in numRecords.