Skip to content

Commit 3f201fa

Browse files
Improve NeXusException information (#39254)
* Add more template instantiations * Add more template instantiations * Remove unnecessary and confusing function * Change closeGroup to closeData * Quiet compiler warning * Call openPath with absolute path * Change from char array to string * Add function name as a variable in NexusException
1 parent 17f6740 commit 3f201fa

File tree

11 files changed

+37
-32
lines changed

11 files changed

+37
-32
lines changed

Framework/DataHandling/inc/MantidDataHandling/SaveNexusProcessedHelper.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ class MANTID_DATAHANDLING_DLL NexusFileIO {
6262
void closeGroup();
6363
/// write the workspace data
6464
int writeNexusProcessedData2D(const API::MatrixWorkspace_const_sptr &localworkspace, const bool &uniformSpectra,
65-
const bool &raggedSpectra, const std::vector<int> &indices, const char *group_name,
66-
bool write2Ddata) const;
65+
const bool &raggedSpectra, const std::vector<int> &indices,
66+
const std::string &group_name, bool write2Ddata) const;
6767

6868
/// write table workspace
6969
int writeNexusTableWorkspace(const API::ITableWorkspace_const_sptr &itableworkspace, const char *group_name) const;

Framework/DataHandling/src/LoadEmptyInstrument.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ API::MatrixWorkspace_sptr LoadEmptyInstrument::runLoadIDFFromNexus(const std::st
236236
bool foundIDF{false};
237237
try {
238238
::NeXus::File nxsfile(filename);
239-
nxsfile.openPath(instrumentParentEntryName + instrumentEntryName);
239+
nxsfile.openPath("/" + instrumentParentEntryName + instrumentEntryName);
240240
foundIDF = true;
241241
} catch (::NeXus::Exception &) {
242242
}
@@ -245,7 +245,7 @@ API::MatrixWorkspace_sptr LoadEmptyInstrument::runLoadIDFFromNexus(const std::st
245245
instrumentParentEntryName = instrumentParentEntryName_2;
246246
try {
247247
::NeXus::File nxsfile(filename);
248-
nxsfile.openPath(instrumentParentEntryName + instrumentEntryName);
248+
nxsfile.openPath("/" + instrumentParentEntryName + instrumentEntryName);
249249
foundIDF = true;
250250
} catch (::NeXus::Exception &) {
251251
}

Framework/DataHandling/src/SaveNexusProcessed.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ void SaveNexusProcessed::doExec(const Workspace_sptr &inputWorkspace,
305305
workspaceTypeGroupName = "workspace";
306306

307307
nexusFile->writeNexusProcessedData2D(matrixWorkspace, uniformSpectra, raggedSpectra, indices,
308-
workspaceTypeGroupName.c_str(), true);
308+
workspaceTypeGroupName, true);
309309
}
310310

311311
if (saveLegacyInstrument()) {

Framework/DataHandling/src/SaveNexusProcessedHelper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ void _writeChunkedData(std::shared_ptr<::NeXus::File> dest, // Must have open gr
277277
*/
278278
int NexusFileIO::writeNexusProcessedData2D(const API::MatrixWorkspace_const_sptr &localworkspace,
279279
const bool &uniformSpectra, const bool &raggedSpectra,
280-
const std::vector<int> &indices, const char *group_name,
280+
const std::vector<int> &indices, const std::string &group_name,
281281
bool write2Ddata) const {
282282
// NXstatus status;
283283

Framework/Nexus/inc/MantidNexus/H5Util.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ MANTID_NEXUS_DLL H5::FileAccPropList defaultFileAcc();
4040
/// Create a 1D data-space to hold data of length.
4141
MANTID_NEXUS_DLL H5::DataSpace getDataSpace(const size_t length);
4242

43-
/// Create a 1D data-space that will hold the supplied vector.
44-
template <typename NumT> H5::DataSpace getDataSpace(const std::vector<NumT> &data);
45-
4643
/// Convert a primitive type to the appropriate H5::DataType.
4744
template <typename NumT> H5::DataType getType();
4845

Framework/Nexus/inc/MantidNexus/NeXusException.hpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "MantidNexus/DllConfig.h"
44
#include "MantidNexus/NeXusFile_fwd.h"
5+
#include <iosfwd>
56
#include <stdexcept>
67
#include <string>
78

@@ -21,11 +22,17 @@ namespace NeXus {
2122
class MANTID_NEXUS_DLL Exception : public std::runtime_error {
2223
public:
2324
/// Create a new NeXus::Exception
24-
Exception(const std::string &msg = "GENERIC ERROR", const std::string &filename = "");
25+
Exception(const std::string &msg = "GENERIC ERROR", const std::string &functionname = "",
26+
const std::string &filename = "");
27+
/// \return the message with functionname
28+
const std::string functionname() const throw();
2529
/// \return the message with filename
2630
const std::string filename() const throw();
2731

2832
private:
29-
std::string m_filename; ///< File this exceptoin is associated with
33+
std::string m_functionname; ///< Function this exception is associted with
34+
std::string m_filename; ///< File this exception is associated with
3035
};
36+
37+
MANTID_NEXUS_DLL std::ostream &operator<<(std::ostream &os, const Exception &err);
3138
}; // namespace NeXus

Framework/Nexus/src/H5Util.cpp

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,6 @@ DataSpace getDataSpace(const size_t length) {
102102
return DataSpace(1, dims);
103103
}
104104

105-
template <typename NumT> DataSpace getDataSpace(const std::vector<NumT> &data) {
106-
return H5Util::getDataSpace(data.size());
107-
}
108-
109105
namespace {
110106

111107
template <typename NumT> H5::DataSet writeScalarDataSet(Group &group, const std::string &name, const NumT &value) {
@@ -193,7 +189,7 @@ void writeNumAttribute(const H5::H5Object &object, const std::string &name, cons
193189
"The writeNumAttribute function only accepts integral of "
194190
"floating point values.");
195191
auto attrType = getType<NumT>();
196-
DataSpace attrSpace = getDataSpace(value);
192+
DataSpace attrSpace = getDataSpace(value.size());
197193

198194
auto attribute = object.createAttribute(name, attrType, attrSpace);
199195
attribute.write(attrType, value.data());
@@ -212,7 +208,7 @@ void writeScalarDataSetWithStrAttributes(H5::Group &group, const std::string &na
212208

213209
template <typename NumT> void writeArray1D(Group &group, const std::string &name, const std::vector<NumT> &values) {
214210
DataType dataType(getType<NumT>());
215-
DataSpace dataSpace = getDataSpace(values);
211+
DataSpace dataSpace = getDataSpace(values.size());
216212

217213
DSetCreatPropList propList = setCompressionAttributes(values.size());
218214

@@ -613,16 +609,6 @@ template MANTID_NEXUS_DLL void
613609
writeScalarDataSetWithStrAttributes(H5::Group &group, const std::string &name, const uint64_t &value,
614610
const std::map<std::string, std::string> &attributes);
615611

616-
// -------------------------------------------------------------------
617-
// instantiations for getDataSpace
618-
// -------------------------------------------------------------------
619-
template MANTID_NEXUS_DLL DataSpace getDataSpace(const std::vector<float> &data);
620-
template MANTID_NEXUS_DLL DataSpace getDataSpace(const std::vector<double> &data);
621-
template MANTID_NEXUS_DLL DataSpace getDataSpace(const std::vector<int32_t> &data);
622-
template MANTID_NEXUS_DLL DataSpace getDataSpace(const std::vector<uint32_t> &data);
623-
template MANTID_NEXUS_DLL DataSpace getDataSpace(const std::vector<int64_t> &data);
624-
template MANTID_NEXUS_DLL DataSpace getDataSpace(const std::vector<uint64_t> &data);
625-
626612
// -------------------------------------------------------------------
627613
// instantiations for readArray1DCoerce
628614
// -------------------------------------------------------------------
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "MantidNexus/NeXusException.hpp"
22
#include "MantidNexus/NeXusFile_fwd.h"
3+
#include <ostream>
34

45
/**
56
* \file NeXusException.cpp
@@ -8,9 +9,22 @@
89

910
namespace NeXus {
1011

11-
Exception::Exception(const std::string &msg, const std::string &filename)
12-
: std::runtime_error(msg), m_filename(filename) {}
12+
Exception::Exception(const std::string &msg, const std::string &functionname, const std::string &filename)
13+
: std::runtime_error(msg), m_functionname(functionname), m_filename(filename) {}
14+
15+
const std::string Exception::functionname() const throw() { return this->m_functionname; }
1316

1417
const std::string Exception::filename() const throw() { return this->m_filename; }
1518

19+
std::ostream &operator<<(std::ostream &os, const Exception &err) {
20+
if (!err.functionname().empty()) {
21+
os << "in function " << err.functionname() << " ";
22+
}
23+
os << err.what();
24+
if (!err.filename().empty()) {
25+
os << " in file " << err.filename();
26+
}
27+
return os;
28+
}
29+
1630
} // namespace NeXus

Framework/Nexus/src/NeXusFile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ using std::vector;
1616
#define NAPI_CALL(status, msg) \
1717
NXstatus tmp = (status); \
1818
if (tmp != NXstatus::NX_OK) { \
19-
throw NeXus::Exception(msg, m_filename); \
19+
throw NeXus::Exception(msg, __func__, m_filename); \
2020
}
2121

2222
/**

Framework/Nexus/test/NeXusFileLeakTest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class NeXusFileLeakTest : public CxxTest::TestSuite {
9696
fileid.openGroup(oss2, "NXdata");
9797
for (int iData = 0; iData < nData; iData++) {
9898
std::string oss3(strmakef("i2_data_%d", iData));
99-
DimVector dims({(int64_t)i2_array.size()});
99+
DimVector dims{static_cast<dimsize_t>(i2_array.size())};
100100
fileid.makeData(oss3, NXnumtype::INT16, dims);
101101
fileid.openData(oss3);
102102
fileid.putData(i2_array.data());

0 commit comments

Comments
 (0)