Skip to content

Commit

Permalink
try...catch throughout the reading logic
Browse files Browse the repository at this point in the history
Also use error::ReadError in the frontend
  • Loading branch information
franzpoeschel committed Apr 8, 2022
1 parent 0213774 commit 20e1cf4
Show file tree
Hide file tree
Showing 15 changed files with 368 additions and 86 deletions.
1 change: 1 addition & 0 deletions include/openPMD/Error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ namespace error
NotFound,
CannotRead,
UnexpectedContent,
Inaccessible,
Other
};

Expand Down
2 changes: 2 additions & 0 deletions src/Error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ namespace error
return "CannotRead";
case Re::UnexpectedContent:
return "UnexpectedContent";
case Re::Inaccessible:
return "Inaccessible";
case Re::Other:
return "Other";
}
Expand Down
36 changes: 34 additions & 2 deletions src/Iteration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,17 @@ void Iteration::readMeshes(std::string const &meshesPath)
mrc.get().m_isConstant = true;
}
m.read();
try
{
m.read();
}
catch (error::ReadError const &err)
{
std::cerr << "Cannot read mesh with name '" << mesh_name
<< "' and will skip it due to read error:\n"
<< err.what() << std::endl;
map.forget(mesh_name);
}
}

/* obtain all scalar meshes */
Expand All @@ -593,7 +604,17 @@ void Iteration::readMeshes(std::string const &meshesPath)
mrc.written() = false;
mrc.resetDataset(Dataset(*dOpen.dtype, *dOpen.extent));
mrc.written() = true;
m.read();
try
{
m.read();
}
catch (error::ReadError const &err)
{
std::cerr << "Cannot read mesh with name '" << mesh_name
<< "' and will skip it due to read error:\n"
<< err.what() << std::endl;
map.forget(mesh_name);
}
}
}

Expand All @@ -619,7 +640,18 @@ void Iteration::readParticles(std::string const &particlesPath)
pOpen.path = species_name;
IOHandler()->enqueue(IOTask(&p, pOpen));
IOHandler()->flush(internal::defaultFlushParams);
p.read();
try
{
p.read();
}
catch (error::ReadError const &err)
{
std::cerr << "Cannot read particle species with name '"
<< species_name
<< "' and will skip it due to read error:\n"
<< err.what() << std::endl;
map.forget(species_name);
}
}
}

Expand Down
37 changes: 30 additions & 7 deletions src/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
* and the GNU Lesser General Public License along with openPMD-api.
* If not, see <http://www.gnu.org/licenses/>.
*/

#include "openPMD/Mesh.hpp"
#include "openPMD/Error.hpp"
#include "openPMD/Series.hpp"
#include "openPMD/auxiliary/DerefDynamicCast.hpp"
#include "openPMD/auxiliary/StringManip.hpp"
Expand Down Expand Up @@ -294,7 +296,10 @@ void Mesh::read()
setGeometry(tmpGeometry);
}
else
throw std::runtime_error(
throw error::ReadError(
error::AffectedObject::Attribute,
error::Reason::UnexpectedContent,
{},
"Unexpected Attribute datatype for 'geometry'");

aRead.name = "dataOrder";
Expand All @@ -310,11 +315,17 @@ void Mesh::read()
if (tmpDataOrder.size() == 1)
setDataOrder(static_cast<DataOrder>(tmpDataOrder[0]));
else
throw std::runtime_error(
throw error::ReadError(
error::AffectedObject::Attribute,
error::Reason::UnexpectedContent,
{},
"Unexpected Attribute value for 'dataOrder': " + tmpDataOrder);
}
else
throw std::runtime_error(
throw error::ReadError(
error::AffectedObject::Attribute,
error::Reason::UnexpectedContent,
{},
"Unexpected Attribute datatype for 'dataOrder'");

aRead.name = "axisLabels";
Expand All @@ -324,7 +335,10 @@ void Mesh::read()
setAxisLabels(
Attribute(*aRead.resource).get<std::vector<std::string> >());
else
throw std::runtime_error(
throw error::ReadError(
error::AffectedObject::Attribute,
error::Reason::UnexpectedContent,
{},
"Unexpected Attribute datatype for 'axisLabels'");

aRead.name = "gridSpacing";
Expand All @@ -339,7 +353,10 @@ void Mesh::read()
*aRead.dtype == DT::VEC_LONG_DOUBLE || *aRead.dtype == DT::LONG_DOUBLE)
setGridSpacing(a.get<std::vector<long double> >());
else
throw std::runtime_error(
throw error::ReadError(
error::AffectedObject::Attribute,
error::Reason::UnexpectedContent,
{},
"Unexpected Attribute datatype for 'gridSpacing'");

aRead.name = "gridGlobalOffset";
Expand All @@ -349,7 +366,10 @@ void Mesh::read()
setGridGlobalOffset(
Attribute(*aRead.resource).get<std::vector<double> >());
else
throw std::runtime_error(
throw error::ReadError(
error::AffectedObject::Attribute,
error::Reason::UnexpectedContent,
{},
"Unexpected Attribute datatype for 'gridGlobalOffset'");

aRead.name = "gridUnitSI";
Expand All @@ -358,7 +378,10 @@ void Mesh::read()
if (*aRead.dtype == DT::DOUBLE)
setGridUnitSI(Attribute(*aRead.resource).get<double>());
else
throw std::runtime_error(
throw error::ReadError(
error::AffectedObject::Attribute,
error::Reason::UnexpectedContent,
{},
"Unexpected Attribute datatype for 'gridUnitSI'");

if (scalar())
Expand Down
44 changes: 38 additions & 6 deletions src/ParticlePatches.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
* and the GNU Lesser General Public License along with openPMD-api.
* If not, see <http://www.gnu.org/licenses/>.
*/

#include "openPMD/ParticlePatches.hpp"
#include "openPMD/Error.hpp"

#include <iostream>

namespace openPMD
{
Expand All @@ -43,7 +47,17 @@ void ParticlePatches::read()
PatchRecord &pr = (*this)[record_name];
pOpen.path = record_name;
IOHandler()->enqueue(IOTask(&pr, pOpen));
pr.read();
try
{
pr.read();
}
catch (error::ReadError const &err)
{
std::cerr << "Cannot read patch record '" << record_name
<< "' due to read error and will skip it:" << err.what()
<< std::endl;
this->container().erase(record_name);
}
}

Parameter<Operation::LIST_DATASETS> dList;
Expand All @@ -55,9 +69,13 @@ void ParticlePatches::read()
{
if (!("numParticles" == component_name ||
"numParticlesOffset" == component_name))
throw std::runtime_error(
"Unexpected record component" + component_name +
"in particlePatch");
{

std::cerr << "Unexpected record component" + component_name +
"in particlePatch. Will ignore it."
<< std::endl;
continue;
}

PatchRecord &pr = Container<PatchRecord>::operator[](component_name);
PatchRecordComponent &prc = pr[RecordComponent::SCALAR];
Expand All @@ -68,7 +86,10 @@ void ParticlePatches::read()
IOHandler()->flush(internal::defaultFlushParams);

if (determineDatatype<uint64_t>() != *dOpen.dtype)
throw std::runtime_error(
throw error::ReadError(
error::AffectedObject::Attribute,
error::Reason::UnexpectedContent,
{},
"Unexpected datatype for " + component_name);

/* allow all attributes to be set */
Expand All @@ -77,7 +98,18 @@ void ParticlePatches::read()
prc.written() = true;

pr.dirty() = false;
prc.read();
try
{
prc.read();
}
catch (error::ReadError const &err)
{
std::cerr
<< "Cannot read record component '" << component_name
<< "' in particle patch and will skip it due to read error:\n"
<< err.what() << std::endl;
Container<PatchRecord>::container().erase(component_name);
}
}
}
} // namespace openPMD
27 changes: 25 additions & 2 deletions src/ParticleSpecies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,17 @@ void ParticleSpecies::read()
hasParticlePatches = true;
pOpen.path = "particlePatches";
IOHandler()->enqueue(IOTask(&particlePatches, pOpen));
particlePatches.read();
try
{
particlePatches.read();
}
catch (error::ReadError const &err)
{
std::cerr << "Cannot read particle patches and will skip them "
"due to read error:\n"
<< err.what() << std::endl;
hasParticlePatches = false;
}
}
else
{
Expand All @@ -76,7 +86,20 @@ void ParticleSpecies::read()
IOHandler()->flush(internal::defaultFlushParams);
rc.get().m_isConstant = true;
}
r.read();
try
{
r.read();
}
catch (error::ReadError const &err)
{
std::cerr << "Cannot read particle record '" << record_name
<< "' and will skip it due to read error:\n"
<< err.what() << std::endl;

map.forget(record_name);
//(*this)[record_name].erase(RecordComponent::SCALAR);
// this->erase(record_name);
}
}
}

Expand Down
13 changes: 12 additions & 1 deletion src/Record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,18 @@ void Record::read()
if (scalar())
{
/* using operator[] will incorrectly update parent */
this->at(RecordComponent::SCALAR).read();
auto &scalarComponent = this->at(RecordComponent::SCALAR);
try
{
scalarComponent.read();
}
catch (error::ReadError const &err)
{
std::cerr << "Cannot read scalar record component and will skip it "
"due to read error:\n"
<< err.what() << std::endl;
this->container().erase(RecordComponent::SCALAR);
}
}
else
{
Expand Down
18 changes: 15 additions & 3 deletions src/RecordComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,11 @@ void RecordComponent::readBase()
makeConstant(a.get<bool>());
break;
default:
throw std::runtime_error("Unexpected constant datatype");
throw error::ReadError(
error::AffectedObject::Attribute,
error::Reason::UnexpectedContent,
{},
"Unexpected constant datatype");
}
written() = true;

Expand All @@ -378,7 +382,11 @@ void RecordComponent::readBase()
oss << "Unexpected datatype (" << *aRead.dtype
<< ") for attribute 'shape' (" << determineDatatype<uint64_t>()
<< " aka uint64_t)";
throw std::runtime_error(oss.str());
throw error::ReadError(
error::AffectedObject::Attribute,
error::Reason::UnexpectedContent,
{},
oss.str());
}

written() = false;
Expand All @@ -392,7 +400,11 @@ void RecordComponent::readBase()
if (*aRead.dtype == DT::DOUBLE)
setUnitSI(Attribute(*aRead.resource).get<double>());
else
throw std::runtime_error("Unexpected Attribute datatype for 'unitSI'");
throw error::ReadError(
error::AffectedObject::Attribute,
error::Reason::UnexpectedContent,
{},
"Unexpected Attribute datatype for 'unitSI'");

readAttributes(ReadMode::FullyReread);
}
Expand Down
Loading

0 comments on commit 20e1cf4

Please sign in to comment.