Skip to content

Commit b4af694

Browse files
committed
Better error messages in verifyDataset
1 parent f4dfc30 commit b4af694

File tree

3 files changed

+63
-35
lines changed

3 files changed

+63
-35
lines changed

include/openPMD/IO/ADIOS/ADIOS2IOHandler.hpp

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "openPMD/IterationEncoding.hpp"
3535
#include "openPMD/ThrowError.hpp"
3636
#include "openPMD/auxiliary/JSON_internal.hpp"
37+
#include "openPMD/auxiliary/StringManip.hpp"
3738
#include "openPMD/backend/Writable.hpp"
3839
#include "openPMD/config.hpp"
3940
#include <stdexcept>
@@ -489,28 +490,44 @@ class ADIOS2IOHandlerImpl
489490
}
490491
}
491492
auto joinedDim = joinedDimension(shape);
493+
auto make_runtime_error = [&](char const *message) {
494+
std::stringstream s;
495+
s << "[ADIOS2IOHandlerImpl::verifyDataset()] " << message;
496+
s << "\nNote: Variable '" << varName << "' has shape ";
497+
auxiliary::write_vec_to_stream(s, shape)
498+
<< ", is accessed from offset ";
499+
auxiliary::write_vec_to_stream(s, offset) << " with extent ";
500+
auxiliary::write_vec_to_stream(s, extent);
501+
if (joinedDim.has_value())
502+
{
503+
s << " (joined dimension on index " << *joinedDim << ").";
504+
}
505+
else
506+
{
507+
s << " (no joined dimension).";
508+
}
509+
return std::runtime_error(s.str());
510+
};
492511
if (joinedDim.has_value() ||
493512
var.ShapeID() == adios2::ShapeID::JoinedArray)
494513
{
495514
if (!offset.empty())
496515
{
497-
throw std::runtime_error(
498-
"[ADIOS2] Offset must be an empty vector in case of joined "
499-
"array.");
516+
throw make_runtime_error(
517+
"Offset must be an empty vector in case of joined array.");
500518
}
501519
if (!joinedDim.has_value())
502520
{
503-
throw std::runtime_error(
504-
"[ADIOS2] Trying to access a dataset as a non-joined "
505-
"array, but it has previously been configured as a joined "
506-
"array.");
521+
throw make_runtime_error(
522+
"Trying to access a dataset as a non-joined array, but it "
523+
"has previously been array.");
507524
}
508525
for (unsigned int i = 0; i < actualDim; i++)
509526
{
510527
if (*joinedDim != i && extent[i] != shape[i])
511528
{
512-
throw std::runtime_error(
513-
"[ADIOS2] store_chunk extent of non-joined dimensions "
529+
throw make_runtime_error(
530+
"store_chunk extent of non-joined dimensions "
514531
"must be equivalent to the total extent.");
515532
}
516533
}
@@ -522,8 +539,7 @@ class ADIOS2IOHandlerImpl
522539
if (!(joinedDim.has_value() && *joinedDim == i) &&
523540
offset[i] + extent[i] > shape[i])
524541
{
525-
throw std::runtime_error(
526-
"[ADIOS2] Dataset access out of bounds.");
542+
throw make_runtime_error("Dataset access out of bounds.");
527543
}
528544
}
529545
} // else

include/openPMD/auxiliary/StringManip.hpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,5 +242,40 @@ namespace auxiliary
242242
});
243243
return std::forward<S>(s);
244244
}
245+
246+
template <typename Stream, typename Vec>
247+
auto write_vec_to_stream(Stream &&s, Vec const &vec) -> Stream &&
248+
{
249+
if (vec.empty())
250+
{
251+
s << "[]";
252+
}
253+
else
254+
{
255+
s << '[';
256+
auto it = vec.begin();
257+
s << *it++;
258+
auto end = vec.end();
259+
for (; it != end; ++it)
260+
{
261+
s << ", " << *it;
262+
}
263+
s << ']';
264+
}
265+
return std::forward<Stream>(s);
266+
}
267+
268+
template <typename Vec>
269+
auto vec_as_string(Vec const &vec) -> std::string
270+
{
271+
if (vec.empty())
272+
{
273+
return "[]";
274+
}
275+
else
276+
{
277+
return write_vec_to_stream(std::stringstream(), vec).str();
278+
}
279+
}
245280
} // namespace auxiliary
246281
} // namespace openPMD

src/IO/AbstractIOHandlerImpl.cpp

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
#include "openPMD/IO/IOTask.hpp"
2525
#include "openPMD/Streaming.hpp"
2626
#include "openPMD/auxiliary/Environment.hpp"
27+
#include "openPMD/auxiliary/StringManip.hpp"
2728
#include "openPMD/auxiliary/Variant.hpp"
28-
#include "openPMD/backend/Attribute.hpp"
2929
#include "openPMD/backend/Writable.hpp"
3030

3131
#include <iostream>
@@ -47,29 +47,6 @@ AbstractIOHandlerImpl::AbstractIOHandlerImpl(AbstractIOHandler *handler)
4747

4848
namespace
4949
{
50-
template <typename Vec>
51-
auto vec_as_string(Vec const &vec) -> std::string
52-
{
53-
if (vec.empty())
54-
{
55-
return "[]";
56-
}
57-
else
58-
{
59-
std::stringstream res;
60-
res << '[';
61-
auto it = vec.begin();
62-
res << *it++;
63-
auto end = vec.end();
64-
for (; it != end; ++it)
65-
{
66-
res << ", " << *it;
67-
}
68-
res << ']';
69-
return res.str();
70-
}
71-
}
72-
7350
template <typename T, typename SFINAE = void>
7451
struct self_or_invoked
7552
{

0 commit comments

Comments
 (0)