OpenPFC  0.1.4
Phase Field Crystal simulation framework
Loading...
Searching...
No Matches
errors.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 VTT Technical Research Centre of Finland Ltd
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
49#ifndef PFC_UI_ERRORS_HPP
50#define PFC_UI_ERRORS_HPP
51
52#include <nlohmann/json.hpp>
53#include <sstream>
54#include <string>
55#include <vector>
56
57namespace pfc {
58namespace ui {
59
117inline std::string
118format_config_error(const std::string &field_name, const std::string &description,
119 const std::string &expected_type,
120 const std::string &actual_value,
121 const std::vector<std::string> &valid_options = {},
122 const std::string &example = "") {
123 std::ostringstream oss;
124 oss << "Invalid configuration: Field '" << field_name << "' ";
125
126 if (actual_value == "missing") {
127 oss << "is missing.\n";
128 } else {
129 oss << "has invalid value.\n";
130 }
131
132 oss << " Description: " << description << "\n";
133 oss << " Expected: " << expected_type << "\n";
134 oss << " Got: " << actual_value << "\n";
135
136 if (!valid_options.empty()) {
137 oss << " Valid options: ";
138 for (size_t i = 0; i < valid_options.size(); ++i) {
139 oss << "'" << valid_options[i] << "'";
140 if (i < valid_options.size() - 1) oss << ", ";
141 }
142 oss << "\n";
143 }
144
145 if (!example.empty()) {
146 oss << " Example: " << example;
147 }
148
149 return oss.str();
150}
151
190inline std::string get_json_value_string(const nlohmann::json &j,
191 const std::string &field_name) {
192 if (!j.contains(field_name)) {
193 return "missing";
194 }
195
196 const auto &value = j[field_name];
197 std::ostringstream oss;
198 oss << value.dump(); // JSON representation
199
200 // Add type information
201 if (value.is_null()) {
202 oss << " (type: null)";
203 } else if (value.is_boolean()) {
204 oss << " (type: boolean)";
205 } else if (value.is_number_integer()) {
206 oss << " (type: integer)";
207 } else if (value.is_number_float()) {
208 oss << " (type: float)";
209 } else if (value.is_string()) {
210 oss << " (type: string)";
211 } else if (value.is_array()) {
212 oss << " (type: array)";
213 } else if (value.is_object()) {
214 oss << " (type: object)";
215 }
216
217 return oss.str();
218}
219
248std::vector<std::string> list_valid_field_modifiers();
249
288inline std::string
290 const std::string &context = "field modifier") {
292
293 std::ostringstream oss;
294 oss << "Unknown " << context << " type: '" << invalid_type << "'\n";
295 oss << " Valid types:\n";
296 for (const auto &type : valid_types) {
297 oss << " - " << type << "\n";
298 }
299 oss << " Check spelling and see documentation for details.";
300
301 return oss.str();
302}
303
304} // namespace ui
305} // namespace pfc
306
307#endif // PFC_UI_ERRORS_HPP
308
std::string format_config_error(const std::string &field_name, const std::string &description, const std::string &expected_type, const std::string &actual_value, const std::vector< std::string > &valid_options={}, const std::string &example="")
Format a helpful configuration error message.
Definition errors.hpp:118
std::vector< std::string > list_valid_field_modifiers()
List all registered field modifiers.
std::string get_json_value_string(const nlohmann::json &j, const std::string &field_name)
Get JSON value as string for error messages.
Definition errors.hpp:190
std::string format_unknown_modifier_error(const std::string &invalid_type, const std::string &context="field modifier")
Format error for unknown field modifier type.
Definition errors.hpp:289
Represents the global simulation domain (the "world").
Definition world.hpp:91