diff --git a/hts.c b/hts.c index cc24135f2..14dff342d 100644 --- a/hts.c +++ b/hts.c @@ -922,6 +922,7 @@ htsFile *hts_open_format(const char *fn, const char *mode, const htsFormat *fmt) fn = rmme; } + errno = 0; //reset to ensure appropriate error display hfile = hopen(fn, smode); if (hfile == NULL) goto error; @@ -938,10 +939,19 @@ htsFile *hts_open_format(const char *fn, const char *mode, const htsFormat *fmt) fmt->format == fastq_format)) fp->format.format = fmt->format; - if (fmt && fmt->specific) - if (hts_opt_apply(fp, fmt->specific) != 0) + if (fmt && fmt->specific) { + int save_errno = errno; + if (hts_opt_apply(fp, fmt->specific) != 0) { + if (((hts_opt*)fmt->specific)->opt == CRAM_OPT_REFERENCE) { + const char *ref = ((hts_opt*)fmt->specific)->val.s; + hts_log_error("Couldn't load reference \"%s\" : %s", + ref ? ref : "(NULL)", strerror(errno)); + // set error to error prior to reference file operation + errno = save_errno; + } goto error; - + } + } if ( rmme ) free(rmme); return fp; @@ -1595,6 +1605,10 @@ htsFile *hts_hopen(hFILE *hfile, const char *fn, const char *mode) int hts_close(htsFile *fp) { int ret = 0, save; + if (!fp) { + errno = EINVAL; + return -1; + } switch (fp->format.format) { case binary_format: