Skip to content

Commit acda554

Browse files
committed
adding new exception types
1 parent 4da1f8a commit acda554

File tree

14 files changed

+92
-74
lines changed

14 files changed

+92
-74
lines changed

libtokamap/TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
- [x] ~~Replace reinterpret_cast with bit_cast~~ (still exist in map_arguments.hpp)
44
- [x] ~~Replace boost::split with std::views::split~~
55
- [ ] Switch from using std::type_index to DataType enum?
6-
- [ ] Add exception types
6+
- [x] ~~Add exception types~~
77
- [ ] Add README and docs for library
88
- [ ] Tidy up DataSource get(...) arguments
99
- [ ] Adding system packaging to CMake

libtokamap/examples/simple_mapper/src/json_data_source.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <filesystem>
99
#include <fstream>
1010
#include <libtokamap.hpp>
11+
#include "exceptions/exceptions.hpp"
1112
#include <nlohmann/json.hpp>
1213
#include <stdexcept>
1314
#include <string>
@@ -53,7 +54,7 @@ libtokamap::TypedDataArray parse(const nlohmann::json& value)
5354
std::ranges::fill(vector.begin(), vector.end(), "<object>"s);
5455
return libtokamap::TypedDataArray{vector};
5556
}
56-
throw std::runtime_error{"invalid json value"};
57+
throw libtokamap::JsonError{"invalid json value"};
5758
}
5859

5960
constexpr auto number_re = ctll::fixed_string{R"(\d+)"};
@@ -65,10 +66,10 @@ libtokamap::TypedDataArray JSONDataSource::get(const libtokamap::DataSourceArgs&
6566
libtokamap::RamCache* /*ram_cache*/)
6667
{
6768
if (!map_args.contains("file_name")) {
68-
throw std::runtime_error{"required argument 'shot' not provided"};
69+
throw libtokamap::ParameterError{"required argument 'shot' not provided"};
6970
}
7071
if (!map_args.contains("signal")) {
71-
throw std::runtime_error{"required argument 'signal' not provided"};
72+
throw libtokamap::ParameterError{"required argument 'signal' not provided"};
7273
}
7374

7475
std::string file_name = map_args.at("file_name");
@@ -77,7 +78,7 @@ libtokamap::TypedDataArray JSONDataSource::get(const libtokamap::DataSourceArgs&
7778
if (!m_data.contains(path)) {
7879
std::ifstream file{path};
7980
if (!file) {
80-
throw std::runtime_error{"failed to open data file '" + path.string() + "'"};
81+
throw libtokamap::FileError{"failed to open data file '" + path.string() + "'"};
8182
}
8283
m_data[path] = nlohmann::json::parse(file);
8384
}

libtokamap/examples/simple_mapper/src/json_data_source.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <libtokamap.hpp>
22
#include <nlohmann/json.hpp>
33
#include <stdexcept>
4+
#include "exceptions/exceptions.hpp"
45
#include <string>
56
#include <unordered_map>
67
#include <utility>
@@ -11,10 +12,10 @@ class JSONDataSource : public libtokamap::DataSource
1112
public:
1213
explicit JSONDataSource(std::filesystem::path data_root) : m_data_root{std::move(data_root)} {
1314
if (!std::filesystem::exists(m_data_root)) {
14-
throw std::runtime_error{"data root does not exist"};
15+
throw libtokamap::FileError{"data root does not exist"};
1516
}
1617
if (!std::filesystem::is_directory(m_data_root)) {
17-
throw std::runtime_error{"data root is not a directory"};
18+
throw libtokamap::FileError{"data root is not a directory"};
1819
}
1920
}
2021
libtokamap::TypedDataArray get(const libtokamap::DataSourceArgs& map_args,

libtokamap/include/libtokamap.hpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
#include <version.hpp> // IWYU pragma: export.
2-
#include <utils/ram_cache.hpp> // IWYU pragma: export.
3-
#include <utils/scale_offset.hpp> // IWYU pragma: export.
4-
#include <utils/indices.hpp> // IWYU pragma: export.
5-
#include <utils/subset.hpp> // IWYU pragma: export.
6-
#include <utils/syntax_parser.hpp> // IWYU pragma: export.
7-
#include <map_types/dim_mapping.hpp> // IWYU pragma: export.
8-
#include <map_types/map_arguments.hpp> // IWYU pragma: export.
9-
#include <map_types/custom_mapping.hpp> // IWYU pragma: export.
10-
#include <map_types/expr_mapping.hpp> // IWYU pragma: export.
11-
#include <map_types/value_mapping.hpp> // IWYU pragma: export.
12-
#include <map_types/data_source_mapping.hpp> // IWYU pragma: export.
13-
#include <map_types/base_mapping.hpp> // IWYU pragma: export.
14-
#include <handlers/mapping_handler.hpp> // IWYU pragma: export.
1+
#include <exceptions/exceptions.hpp> // IWYU pragma: export.
2+
#include <handlers/mapping_handler.hpp> // IWYU pragma: export.
3+
#include <map_types/base_mapping.hpp> // IWYU pragma: export.
4+
#include <map_types/custom_mapping.hpp> // IWYU pragma: export.
5+
#include <map_types/data_source_mapping.hpp> // IWYU pragma: export.
6+
#include <map_types/dim_mapping.hpp> // IWYU pragma: export.
7+
#include <map_types/expr_mapping.hpp> // IWYU pragma: export.
8+
#include <map_types/map_arguments.hpp> // IWYU pragma: export.
9+
#include <map_types/value_mapping.hpp> // IWYU pragma: export.
10+
#include <utils/indices.hpp> // IWYU pragma: export.
11+
#include <utils/ram_cache.hpp> // IWYU pragma: export.
12+
#include <utils/scale_offset.hpp> // IWYU pragma: export.
13+
#include <utils/subset.hpp> // IWYU pragma: export.
14+
#include <utils/syntax_parser.hpp> // IWYU pragma: export.
15+
#include <version.hpp> // IWYU pragma: export.

libtokamap/src/handlers/mapping_handler.cpp

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <utility>
2727
#include <vector>
2828

29+
#include "exceptions/exceptions.hpp"
2930
#include "map_types/base_mapping.hpp"
3031
#include "map_types/custom_mapping.hpp"
3132
#include "map_types/data_source_mapping.hpp"
@@ -136,7 +137,7 @@ std::string libtokamap::TypedDataArray::to_string() const
136137
print<double>(out, m_buffer, m_size);
137138
break;
138139
default:
139-
throw std::runtime_error{"unhandled data type: '" + ::to_string(m_type_index) + "'"};
140+
throw libtokamap::DataTypeError{"unhandled data type: '" + ::to_string(m_type_index) + "'"};
140141
}
141142
out << " }";
142143
return out.str();
@@ -156,7 +157,7 @@ void libtokamap::MappingHandler::init(const nlohmann::json& config)
156157
}
157158

158159
if (!config.contains("mapping_directory")) {
159-
throw std::runtime_error{"mapping_directory not specified in config"};
160+
throw libtokamap::ConfigurationError{"mapping_directory not specified in config"};
160161
}
161162
m_mapping_dir = config.at("mapping_directory").get<std::string>();
162163

@@ -181,7 +182,7 @@ libtokamap::TypedDataArray libtokamap::MappingHandler::map(const std::string& ma
181182
std::deque<std::string_view> path_tokens;
182183
libtokamap::split(path_tokens, path, "/");
183184
if (path_tokens.empty()) {
184-
throw std::runtime_error{"IDS path could not be split"};
185+
throw libtokamap::PathError{"IDS path could not be split"};
185186
}
186187

187188
auto [indices, new_tokens] = extract_indices(path_tokens);
@@ -199,7 +200,8 @@ libtokamap::TypedDataArray libtokamap::MappingHandler::map(const std::string& ma
199200
const auto maybe_mappings = read_mappings(machine_string, ids_name, extra_attributes);
200201

201202
if (!maybe_mappings) {
202-
throw std::runtime_error{"no mappings found for machine '" + machine_string + "' and IDS '" + ids_name + "'"};
203+
throw libtokamap::MappingError{"no mappings found for machine '" + machine_string + "' and IDS '" + ids_name +
204+
"'"};
203205
}
204206

205207
const auto& [attributes, mappings] = maybe_mappings.value();
@@ -210,7 +212,7 @@ libtokamap::TypedDataArray libtokamap::MappingHandler::map(const std::string& ma
210212

211213
std::string const map_path = generate_map_path(new_tokens, indices, mappings, path);
212214
if (map_path.empty()) {
213-
throw std::runtime_error{"failed to find mapping for '" + path + "'"};
215+
throw libtokamap::MappingError{"failed to find mapping for '" + path + "'"};
214216
}
215217

216218
// Add request indices to globals
@@ -306,7 +308,7 @@ void libtokamap::MappingHandler::load_machine(const MachineName& machine)
306308
if (map_cfg_file) {
307309
map_cfg_file >> m_mapping_config;
308310
} else {
309-
throw std::runtime_error{"Cannot open JSON mapping config file"};
311+
throw libtokamap::FileError{"Cannot open JSON mapping config file"};
310312
}
311313

312314
m_machine_register[machine] = {.mappings = {}, .attributes = {}};
@@ -329,10 +331,10 @@ nlohmann::json libtokamap::MappingHandler::load_toplevel(const MachineName& mach
329331
try {
330332
globals_file >> toplevel_globals;
331333
} catch (nlohmann::json::exception& ex) {
332-
throw std::runtime_error{ex.what()};
334+
throw libtokamap::JsonError{ex.what()};
333335
}
334336
} else {
335-
throw std::runtime_error{"Cannot open top-level globals file"};
337+
throw libtokamap::FileError{"Cannot open top-level globals file"};
336338
}
337339
return toplevel_globals;
338340
}
@@ -348,13 +350,13 @@ void libtokamap::MappingHandler::load_shot_globals(const MachineName& machine, c
348350
try {
349351
globals_file >> temp_globals;
350352
} catch (nlohmann::json::exception& ex) {
351-
throw std::runtime_error{ex.what()};
353+
throw libtokamap::JsonError{ex.what()};
352354
}
353355

354356
temp_globals.update(load_toplevel(machine));
355357
m_machine_register[machine].attributes[ids_name].map[shot] = temp_globals; // Record globals
356358
} else {
357-
throw std::runtime_error{"Cannot open JSON globals file"};
359+
throw libtokamap::FileError{"Cannot open JSON globals file"};
358360
}
359361
}
360362

@@ -380,12 +382,12 @@ void libtokamap::MappingHandler::load_shot_mappings(const MachineName& machine,
380382
try {
381383
map_file >> temp_mappings;
382384
} catch (nlohmann::json::exception& ex) {
383-
throw std::runtime_error{ex.what()};
385+
throw libtokamap::JsonError{ex.what()};
384386
}
385387

386388
init_mappings(machine, ids_name, temp_mappings, shot);
387389
} else {
388-
throw std::runtime_error{"Cannot open JSON mapping file"};
390+
throw libtokamap::FileError{"Cannot open JSON mapping file"};
389391
}
390392
}
391393

@@ -476,13 +478,15 @@ void init_data_source_mapping(libtokamap::IDSMapRegister& map_reg, const std::st
476478
const nlohmann::json& ids_attributes, std::shared_ptr<libtokamap::RamCache>& ram_cache)
477479
{
478480
if (!value.contains("DATA_SOURCE")) {
479-
throw std::runtime_error{"required DATA_SOURCE argument not provided in DATA_SOURCE mapping '" + key + "'"};
481+
throw libtokamap::ConfigurationError{"required DATA_SOURCE argument not provided in DATA_SOURCE mapping '" +
482+
key + "'"};
480483
}
481484
std::string data_source_name = value["DATA_SOURCE"].get<std::string>();
482485
libtokamap::to_upper(data_source_name);
483486

484487
if (!value.contains("ARGS")) {
485-
throw std::runtime_error{"required ARGS argument not provided in DATA_SOURCE mapping '" + key + "'"};
488+
throw libtokamap::ConfigurationError{"required ARGS argument not provided in DATA_SOURCE mapping '" + key +
489+
"'"};
486490
}
487491
auto args = value["ARGS"].get<libtokamap::DataSourceArgs>();
488492
auto offset = get_float_value("OFFSET", value, ids_attributes);
@@ -529,7 +533,7 @@ void libtokamap::MappingHandler::init_mappings(const MachineName& machine, const
529533
auto parsed_value = libtokamap::parse(value);
530534

531535
if (!parsed_value.contains("MAP_TYPE")) {
532-
throw std::runtime_error{"required MAP_TYPE argument not found in mapping '" + key + "'"};
536+
throw libtokamap::MappingError{"required MAP_TYPE argument not found in mapping '" + key + "'"};
533537
}
534538

535539
// TODO: make this case insensitive?

libtokamap/src/map_types/data_source_mapping.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
#include <inja/inja.hpp>
44
#include <memory>
55
#include <optional>
6-
#include <stdexcept>
76
#include <string>
87
#include <unordered_map>
98
#include <utility>
109

10+
#include "exceptions/exceptions.hpp"
1111
#include "map_types/map_arguments.hpp"
1212
#include "utils/ram_cache.hpp"
1313
#include "utils/subset.hpp"
@@ -36,7 +36,7 @@ libtokamap::DataSourceMapping::DataSourceMapping(const std::string& data_source_
3636
m_ram_cache{std::move(ram_cache)}, m_cache_enabled(m_ram_cache != nullptr)
3737
{
3838
if (!m_data_sources.contains(data_source_name)) {
39-
throw std::runtime_error{"data source " + data_source_name + " not registered"};
39+
throw libtokamap::DataSourceError{"data source " + data_source_name + " not registered"};
4040
}
4141
m_data_source = m_data_sources[data_source_name].get();
4242
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#include "map_types/dim_mapping.hpp"
22

33
#include <cstdlib>
4-
#include <stdexcept>
54

5+
#include "exceptions/exceptions.hpp"
66
#include "map_types/base_mapping.hpp"
77
#include "map_types/map_arguments.hpp"
88

99
libtokamap::TypedDataArray libtokamap::DimMapping::map(const MapArguments& arguments) const
1010
{
1111
if (!arguments.entries.contains(m_dim_probe)) {
12-
throw std::runtime_error{"invalid DIM_PROBE '" + m_dim_probe + "'"};
12+
throw libtokamap::MappingError{"invalid DIM_PROBE '" + m_dim_probe + "'"};
1313
}
1414

1515
auto array = arguments.entries.at(m_dim_probe)->map(arguments);
16-
return TypedDataArray{ array.size() };
16+
return TypedDataArray{array.size()};
1717
}

libtokamap/src/map_types/map_arguments.hpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,18 @@ class TypedDataArray
253253
}
254254
#endif
255255

256-
template <typename T> [[nodiscard]] std::vector<T> as_vector() const
256+
template <typename T> [[nodiscard]] const T* data() const
257257
{
258258
if (m_type_index != std::type_index{typeid(T)}) {
259-
throw libtokamap::DataTypeError{"invalid type given to span"};
259+
throw libtokamap::DataTypeError{"invalid type given to data"};
260+
}
261+
return reinterpret_cast<T*>(m_buffer);
262+
}
263+
264+
template <typename T> [[nodiscard]] std::vector<T> to_vector() const
265+
{
266+
if (m_type_index != std::type_index{typeid(T)}) {
267+
throw libtokamap::DataTypeError{"invalid type given to to_vector"};
260268
}
261269
const T* ptr = reinterpret_cast<T*>(m_buffer);
262270
return std::vector<T>{ptr, ptr + m_size};

libtokamap/src/map_types/value_mapping.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
#include <cstddef>
55
#include <cstdint>
66
#include <inja/inja.hpp>
7-
#include <map_types/data_source_mapping.hpp>
87
#include <stdexcept>
98
#include <string>
109
#include <type_traits>
1110
#include <typeindex>
1211
#include <vector>
1312

13+
#include "exceptions/exceptions.hpp"
1414
#include "map_types/map_arguments.hpp"
1515

1616
using namespace inja;
@@ -103,7 +103,7 @@ std::string render_string(const std::string& input, const json& global_data)
103103
}
104104

105105
libtokamap::TypedDataArray type_deduce_primitive(const json& temp_val, const json& global_data,
106-
std::type_index data_type, int rank)
106+
std::type_index data_type, int rank)
107107
{
108108
switch (temp_val.type()) {
109109
case json::value_t::number_float:
@@ -157,7 +157,7 @@ libtokamap::TypedDataArray type_deduce_primitive(const json& temp_val, const jso
157157
break;
158158
}
159159
default:
160-
throw std::runtime_error{"unknown json type"};
160+
throw libtokamap::JsonError{"unknown json type"};
161161
}
162162

163163
return {};
@@ -169,7 +169,7 @@ libtokamap::TypedDataArray libtokamap::ValueMapping::map(const MapArguments& arg
169169
{
170170
const auto temp_val = m_value;
171171
if (temp_val.is_discarded() or temp_val.is_binary() or temp_val.is_null()) {
172-
throw std::runtime_error{"map unrecognised json value type"};
172+
throw libtokamap::JsonError{"map unrecognised json value type"};
173173
}
174174

175175
if (temp_val.is_array()) {
@@ -186,7 +186,7 @@ libtokamap::TypedDataArray libtokamap::ValueMapping::map(const MapArguments& arg
186186
} else if (temp_val.is_primitive()) {
187187
return type_deduce_primitive(temp_val, arguments.global_data, arguments.data_type, arguments.rank);
188188
} else {
189-
throw std::runtime_error{"map not structured or primitive"};
189+
throw libtokamap::ProcessingError{"map not structured or primitive"};
190190
}
191191

192192
return {};

libtokamap/src/utils/scale_offset.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <cstdlib>
55
#include <span>
66
#include <stdexcept>
7+
#include "exceptions/exceptions.hpp"
78

89
#include "map_types/map_arguments.hpp"
910

@@ -78,7 +79,7 @@ int libtokamap::map_transform::transform_offset(TypedDataArray& array, float off
7879
break;
7980
}
8081
default:
81-
throw std::runtime_error{"unrecognised type"};
82+
throw libtokamap::DataTypeError{"unrecognised type"};
8283
}
8384
} else {
8485
switch (type_index_map(array.type_index())) {
@@ -108,7 +109,7 @@ int libtokamap::map_transform::transform_offset(TypedDataArray& array, float off
108109
break;
109110
}
110111
default:
111-
throw std::runtime_error{"unrecognised type"};
112+
throw libtokamap::DataTypeError{"unrecognised type"};
112113
}
113114
}
114115

@@ -147,7 +148,7 @@ int libtokamap::map_transform::transform_scale(TypedDataArray& array, float scal
147148
break;
148149
}
149150
default:
150-
throw std::runtime_error{"unrecognised type"};
151+
throw libtokamap::DataTypeError{"unrecognised type"};
151152
}
152153
} else {
153154
switch (type_index_map(array.type_index())) {
@@ -177,7 +178,7 @@ int libtokamap::map_transform::transform_scale(TypedDataArray& array, float scal
177178
break;
178179
}
179180
default:
180-
throw std::runtime_error{"unrecognised type"};
181+
throw libtokamap::DataTypeError{"unrecognised type"};
181182
}
182183
}
183184

0 commit comments

Comments
 (0)