Skip to content

Commit

Permalink
put exception handling/conversion in a utility routine
Browse files Browse the repository at this point in the history
  • Loading branch information
JDanielSmith committed Dec 23, 2022
1 parent 819a99d commit ea1c03e
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 47 deletions.
5 changes: 2 additions & 3 deletions modules/c++/hdf5.lite/source/H5.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@

#include <vector>

#include "types/RowCol.h"


// see https://docs.hdfgroup.org/archive/support/HDF5/doc1.8/cpplus_RM/readdata_8cpp-example.html
#include <H5Cpp.h>

#include "types/RowCol.h"

// Utility routines for INTERNAL use!

namespace hdf5
Expand Down
46 changes: 2 additions & 44 deletions modules/c++/hdf5.lite/source/Read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,55 +22,13 @@

#include "hdf5/lite/Read.h"

#include <stdexcept>
#include <tuple> // std::ignore
#include <vector>
#include <functional>

#include "except/Exception.h"
#include "coda_oss/cstddef.h" // byte

#include "hdf5/lite/HDF5Exception.h"

#include "H5.h"

static void try_catch_H5Exception(std::function<void(void*)> f, void* context=nullptr)
{
try
{
/*
* Turn off the auto-printing when failure occurs so that we can
* handle the errors appropriately
*/
H5::Exception::dontPrint();

f(context);
}
// catch failure caused by the H5File operations
catch (const H5::FileIException& error)
{
const except::Context ctx(error.getDetailMsg(), __FILE__, __LINE__, error.getFuncName());
throw except::IOException(ctx);
}
// catch failure caused by the DataSet operations
catch (const H5::DataSetIException& error)
{
const except::Context ctx(error.getDetailMsg(), __FILE__, __LINE__, error.getFuncName());
throw hdf5::lite::DataSetException11(ctx);
}
// catch failure caused by the DataSpace operations
catch (const H5::DataSpaceIException& error)
{
const except::Context ctx(error.getDetailMsg(), __FILE__, __LINE__, error.getFuncName());
throw hdf5::lite::DataSpaceException11(ctx);
}
// catch failure caused by the DataType operations
catch (const H5::DataTypeIException& error)
{
const except::Context ctx(error.getDetailMsg(), __FILE__, __LINE__, error.getFuncName());
throw hdf5::lite::DataTypeException11(ctx);
}
}
#include "hdf5.lite.h"

template<typename T>
static types::RowCol<size_t> readDatasetT(const H5::DataSet& dataset, H5T_class_t type_class, const H5::DataType& mem_type,
Expand Down Expand Up @@ -115,6 +73,6 @@ types::RowCol<size_t> hdf5::lite::readFile(coda_oss::filesystem::path fileName,
{
types::RowCol<size_t> retval;
auto call_readFile_ = [&](void*) { retval = readFile_(fileName, datasetName, result); };
try_catch_H5Exception(call_readFile_);
details::try_catch_H5Exceptions(call_readFile_);
return retval;
}
61 changes: 61 additions & 0 deletions modules/c++/hdf5.lite/source/hdf5.lite.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* =========================================================================
* This file is part of hd5.lite-c++
* =========================================================================
*
* (C) Copyright 2022, Maxar Technologies, Inc.
*
* hd5.lite-c++ is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; If not,
* see <http://www.gnu.org/licenses/>.
*
*/

#include "hdf5.lite.h"

void hdf5::lite::details::try_catch_H5Exceptions(std::function<void(void*)> f, void* context)
{
try
{
/*
* Turn off the auto-printing when failure occurs so that we can
* handle the errors appropriately
*/
H5::Exception::dontPrint();

f(context);
}
// catch failure caused by the H5File operations
catch (const H5::FileIException& error)
{
const except::Context ctx(error.getDetailMsg(), __FILE__, __LINE__, error.getFuncName());
throw except::IOException(ctx);
}
// catch failure caused by the DataSet operations
catch (const H5::DataSetIException& error)
{
const except::Context ctx(error.getDetailMsg(), __FILE__, __LINE__, error.getFuncName());
throw hdf5::lite::DataSetException11(ctx);
}
// catch failure caused by the DataSpace operations
catch (const H5::DataSpaceIException& error)
{
const except::Context ctx(error.getDetailMsg(), __FILE__, __LINE__, error.getFuncName());
throw hdf5::lite::DataSpaceException11(ctx);
}
// catch failure caused by the DataType operations
catch (const H5::DataTypeIException& error)
{
const except::Context ctx(error.getDetailMsg(), __FILE__, __LINE__, error.getFuncName());
throw hdf5::lite::DataTypeException11(ctx);
}
}
43 changes: 43 additions & 0 deletions modules/c++/hdf5.lite/source/hdf5.lite.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* =========================================================================
* This file is part of hd5.lite-c++
* =========================================================================
*
* (C) Copyright 2022, Maxar Technologies, Inc.
*
* hd5.lite-c++ is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; If not,
* see <http://www.gnu.org/licenses/>.
*
*/

#include <functional>
#include <stdexcept>

// see https://docs.hdfgroup.org/archive/support/HDF5/doc1.8/cpplus_RM/readdata_8cpp-example.html
#include <H5Cpp.h>

#include "except/Exception.h"
#include "hdf5/lite/HDF5Exception.h"

// Utility routines for INTERNAL use!

namespace hdf5
{
namespace lite
{
namespace details
{
void try_catch_H5Exceptions(std::function<void(void*)> f, void* context = nullptr);
}
}
}

0 comments on commit ea1c03e

Please sign in to comment.