Skip to content

Commit

Permalink
Improve error message generated when invalid params are passed to hic…
Browse files Browse the repository at this point in the history
…tkpy.File()
  • Loading branch information
robomics committed Nov 9, 2024
1 parent b438fca commit e1a0426
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,34 @@ namespace hictkpy::file {
static void ctor(hictk::File *fp, const std::filesystem::path &path,
std::optional<std::int32_t> resolution, std::string_view matrix_type,
std::string_view matrix_unit) {
new (fp) hictk::File{path.string(), static_cast<std::uint32_t>(resolution.value_or(0)),
hictk::hic::ParseMatrixTypeStr(std::string{matrix_type}),
hictk::hic::ParseUnitStr(std::string{matrix_unit})};
const auto resolution_ = static_cast<std::uint32_t>(resolution.value_or(0));
try {
new (fp) hictk::File{path.string(), resolution_,
hictk::hic::ParseMatrixTypeStr(std::string{matrix_type}),
hictk::hic::ParseUnitStr(std::string{matrix_unit})};
// TODO all the exceptions should ideally be handled on the hictk side
// but this will have to do until the next release of hictk
} catch (const HighFive::Exception &e) {
std::string_view msg{e.what()};
if (msg.find("Unable to open the group \"/resolutions/0\"") != std::string_view::npos) {
throw std::runtime_error(
"resolution is required and cannot be None when opening .mcool files");
}
throw;
} catch (const std::runtime_error &e) {
std::string_view msg{e.what()};
if (msg.find("resolution cannot be 0 when opening .hic files") != std::string_view::npos) {
throw std::runtime_error("resolution is required and cannot be None when opening .hic files");
}
throw;
}

if (resolution.has_value() && fp->resolution() != resolution_) {
// TODO this should also be handled by hictk
throw std::runtime_error(
fmt::format(FMT_STRING("resolution mismatch for file \"{}\": expected {}, found {}"),
fp->uri(), resolution_, fp->resolution()));
}
}

static std::string repr(const hictk::File &f) {
Expand Down

0 comments on commit e1a0426

Please sign in to comment.