rapidyaml  0.12.0
parse and emit YAML, and do it fast
Error handling

Utilities to report handle errors, and to build and report error messages. More...

Classes

struct  c4::yml::Location
 holds a source or yaml file position, for example when an error is detected; See also location_format() and location_format_with_context(). More...
 
struct  c4::yml::ErrorDataBasic
 Data for a basic error. More...
 
struct  c4::yml::ErrorDataParse
 Data for a parse error. More...
 
struct  c4::yml::ErrorDataVisit
 Data for a visit error. More...
 
struct  c4::yml::ExceptionBasic
 Exception thrown by the default basic error implementation. More...
 
struct  c4::yml::ExceptionParse
 Exception thrown by the default parse error implementation. More...
 
struct  c4::yml::ExceptionVisit
 Exception thrown by the default visit error implementation. More...
 

Functions

template<class DumpFn >
size_t c4::yml::location_format (DumpFn &&dumpfn, Location const &loc)
 generic formatting of a location More...
 
template<class DumpFn >
void c4::yml::location_format_with_context (DumpFn &&dumpfn, Location const &location, csubstr source_buffer, csubstr call="", size_t num_lines_before=3, size_t num_lines_after=0, size_t first_col_highlight=0, size_t last_col_highlight=0, size_t maxlen=80u)
 Generic formatting of a location, printing the source code buffer region around the location. More...
 
template<class DumpFn >
void c4::yml::err_basic_format (DumpFn &&dumpfn, csubstr msg, ErrorDataBasic const &errdata)
 Given an error message and associated basic error data, format it fully as a basic error message. More...
 
void c4::yml::err_basic (Callbacks const &callbacks, ErrorDataBasic const &errdata, const char *msg_)
 trigger a basic error to its respective handler, with a non-formatted error message. More...
 
void c4::yml::err_basic (ErrorDataBasic const &errdata, const char *msg)
 trigger a basic error to its respective handler, with a non-formatted error message. More...
 
template<class ... Args>
void c4::yml::err_basic (Callbacks const &callbacks, ErrorDataBasic const &errdata, const char *fmt, Args const &...args)
 trigger a basic error to its respective handler, with a formatted error message. More...
 
template<class ... Args>
void c4::yml::err_basic (ErrorDataBasic const &errdata, const char *fmt, Args const &...args)
 trigger a basic error to its respective handler, with a formatted error message. More...
 
template<class DumpFn >
void c4::yml::err_parse_format (DumpFn &&dumpfn, csubstr msg, ErrorDataParse const &errdata)
 Given an error message and associated parse error data, format it fully as a parse error message. More...
 
void c4::yml::err_parse (Callbacks const &callbacks, ErrorDataParse const &errdata, const char *msg)
 trigger a parse error to its respective handler, with a non-formatted error message More...
 
void c4::yml::err_parse (ErrorDataParse const &errdata, const char *msg)
 trigger a parse error to its respective handler, with a non-formatted error message. More...
 
template<class ... Args>
void c4::yml::err_parse (Callbacks const &callbacks, ErrorDataParse const &errdata, const char *fmt, Args const &...args)
 trigger a parse error to its respective handler, with a formatted error message More...
 
template<class ... Args>
void c4::yml::err_parse (ErrorDataParse const &errdata, const char *fmt, Args const &...args)
 trigger a parse error to its respective handler, with a formatted error message. More...
 
template<class DumpFn >
void c4::yml::err_visit_format (DumpFn &&dumpfn, csubstr msg, ErrorDataVisit const &errdata)
 Given an error message and associated visit error data, format it fully as a visit error message. More...
 
void c4::yml::err_visit (Callbacks const &callbacks, ErrorDataVisit const &errdata, const char *msg)
 trigger a visit error to its respective handler, with a non-formatted error message More...
 
void c4::yml::err_visit (ErrorDataVisit const &errdata, const char *msg)
 trigger a visit error to its respective handler, with a non-formatted error message. More...
 
template<class ... Args>
void c4::yml::err_visit (Callbacks const &callbacks, ErrorDataVisit const &errdata, const char *fmt, Args const &...args)
 trigger a visit error to its respective handler, with a formatted error message More...
 
template<class ... Args>
void c4::yml::err_visit (ErrorDataVisit const &errdata, const char *fmt, Args const &...args)
 trigger a visit error to its respective handler, with a formatted error message. More...
 
template<class CharContainer >
void c4::yml::format_exc (CharContainer *out, ExceptionBasic const &exc)
 Format a basic exception to an existing char container. More...
 
template<class CharContainer >
void c4::yml::format_exc (CharContainer *out, ExceptionParse const &exc)
 Format a parse exception to an existing char container. More...
 
template<class CharContainer >
void c4::yml::format_exc (CharContainer *out, ExceptionVisit const &exc)
 Format a visit exception to an existing char container. More...
 
template<class CharContainer , class ExceptionT >
CharContainer c4::yml::format_exc (ExceptionT const &exc)
 Format a parse exception, and return a newly-created char container. More...
 

Detailed Description

Utilities to report handle errors, and to build and report error messages.

See also
sample::sample_error_handler

Function Documentation

◆ location_format()

template<class DumpFn >
size_t c4::yml::location_format ( DumpFn &&  dumpfn,
Location const &  loc 
)

generic formatting of a location

Parameters
dumpfnfunction taking a csubstr and abstracting a string concatenation operation, such as appending to a std::string or printing to terminal.
locthe location to be formatted

For example:

{c++}
/// to output to std::cerr:
location_format([&s](csubstr s){
std::cerr.write(s.str, s.len);
}, loc);
/// to build a string:
std::string msg;
location_format([&s](csubstr s){
msg.append(s.str, s.len);
}, loc);
size_t location_format(DumpFn &&dumpfn, Location const &loc)
generic formatting of a location
Definition: error.def.hpp:16

Definition at line 16 of file error.def.hpp.

17 {
18  if(!loc)
19  return 0;
20  char buf_[32];
21  substr buf(buf_);
22  size_t count = 0;
23  if(!loc.name.empty())
24  {
25  std::forward<DumpFn>(dumpfn)(loc.name);
26  std::forward<DumpFn>(dumpfn)(":");
27  count += loc.name.len + 1;
28  }
29  if(loc.line != npos)
30  {
31  csubstr val = detail::_to_chars_limited(buf, loc.line);
32  if(loc.name.empty())
33  {
34  std::forward<DumpFn>(dumpfn)("line=");
35  std::forward<DumpFn>(dumpfn)(val);
36  if(loc.col == npos)
37  {
38  std::forward<DumpFn>(dumpfn)(":");
39  ++count;
40  }
41  count += val.len + 5;
42  }
43  else
44  {
45  std::forward<DumpFn>(dumpfn)(val);
46  std::forward<DumpFn>(dumpfn)(":");
47  count += val.len + 1;
48  }
49  }
50  if(loc.col != npos)
51  {
52  csubstr val = detail::_to_chars_limited(buf, loc.col);
53  if(loc.line != npos || !loc.name.empty())
54  {
55  std::forward<DumpFn>(dumpfn)(" ");
56  ++count;
57  }
58  std::forward<DumpFn>(dumpfn)("col=");
59  std::forward<DumpFn>(dumpfn)(val);
60  count += val.len + 4;
61  if(loc.offset == npos)
62  {
63  std::forward<DumpFn>(dumpfn)(":");
64  ++count;
65  }
66  }
67  if(loc.offset != npos)
68  {
69  csubstr val = detail::_to_chars_limited(buf, loc.offset);
70  if(loc.line != npos || loc.col != npos || !loc.name.empty())
71  {
72  std::forward<DumpFn>(dumpfn)(" ");
73  ++count;
74  }
75  std::forward<DumpFn>(dumpfn)("(");
76  std::forward<DumpFn>(dumpfn)(val);
77  std::forward<DumpFn>(dumpfn)("B):");
78  count += val.len + 5;
79  }
80  return count;
81 }
@ npos
a null string position
Definition: common.hpp:258

References c4::yml::Location::col, c4::yml::Location::line, c4::yml::Location::name, c4::yml::npos, and c4::yml::Location::offset.

◆ location_format_with_context()

template<class DumpFn >
void c4::yml::location_format_with_context ( DumpFn &&  dumpfn,
Location const &  location,
csubstr  source_buffer,
csubstr  call = "",
size_t  num_lines_before = 3,
size_t  num_lines_after = 0,
size_t  first_col_highlight = 0,
size_t  last_col_highlight = 0,
size_t  maxlen = 80u 
)

Generic formatting of a location, printing the source code buffer region around the location.

Parameters
dumpfnfunction taking a csubstr and abstracting a string concatenation operation, such as appending to a std::string or printing to terminal.
locationthe location
source_bufferthe source buffer
calla string with a call of attention to print in the message (see examples below)
num_lines_beforehow many source buffer lines to print before the location line
num_lines_afterhow many source buffer lines to print after the location line
first_col_highlightthe first column to highlight around the location line
last_col_highlightthe last column to highlight around the location line
maxlenthe maximum number of columns to show in the error message; source buffer lines will have at most this number of columns shown; if the line is longer than this, the line will be trimmed as needed at the end and/or beginning, and only the relevant columns around the location are shown

For example:

{c++}
std::string out;
auto dumpfn = [&out](csubstr s){ out.append(s.str, s.len); };
format_location_with_context(dumpfn, location, source, "error");

will result in this string:

file.yaml:3: col=3 (11B): error:
error:
error: ccc
error: |
error: (here)
error:
error: see region:
error:
error: aaa
error: bbb
error: ccc
error: |
error: (here)

If an empty string is passed for the call of attention,

{c++}
format_location_with_context(dumpfn, location, source);

the returned string becomes:

file.yaml:3: col=3 (11B): ccc
|
(here)
file.yaml:3: col=3 (11B): see region:
aaa
bbb
ccc
|
(here)

Definition at line 84 of file error.def.hpp.

93 {
94  if(!location)
95  return;
96  char buf_[32];
97  substr buf(buf_);
98  auto pr = [&](csubstr s){ std::forward<DumpFn>(dumpfn)(s); };
99  auto prn = [&](csubstr s, size_t num_times){
100  for(size_t i = 0; i < num_times; ++i)
101  std::forward<DumpFn>(dumpfn)(s);
102  };
103  csubstr line = detail::_get_text_region(source_buffer, location.offset, 0, 0);
104  size_t target_col = location.col != npos ? location.col : (last_col_highlight > first_col_highlight ? first_col_highlight : npos);
105  size_t first_col_to_show = 0;
106  if(target_col != npos && target_col > maxlen)
107  first_col_to_show = target_col - maxlen + 1;
108  auto print_line_maybe_truncated = [&](csubstr contents){
109  if(contents.len <= maxlen)
110  {
111  if(first_col_to_show == 0)
112  {
113  pr(contents);
114  }
115  else if(first_col_to_show < contents.len)
116  {
117  csubstr show = contents.sub(first_col_to_show);
118  pr("[...]");
119  pr(show);
120  if(maxlen > show.len)
121  prn(" ", maxlen - show.len + 5);
122  pr(" (showing columns ");
123  pr(detail::_to_chars_limited(buf, first_col_to_show));
124  pr("-");
125  pr(detail::_to_chars_limited(buf, contents.len));
126  pr("/");
127  pr(detail::_to_chars_limited(buf, contents.len));
128  pr(")");
129  }
130  else
131  {
132  pr("[...]");
133  prn(" ", maxlen + 5);
134  pr(" (not showing, columns=");
135  pr(detail::_to_chars_limited(buf, contents.len));
136  pr(")");
137  }
138  }
139  else
140  {
141  if(first_col_to_show == 0)
142  {
143  csubstr show = contents.first(maxlen);
144  pr(show);
145  pr("[...] (showing columns 0-");
146  pr(detail::_to_chars_limited(buf, show.len));
147  pr("/");
148  pr(detail::_to_chars_limited(buf, contents.len));
149  pr(")");
150  }
151  else if(first_col_to_show < contents.len && first_col_to_show + maxlen <= contents.len)
152  {
153  csubstr show = contents.sub(first_col_to_show, maxlen);
154  pr("[...]");
155  pr(show);
156  pr("[...] (showing columns ");
157  pr(detail::_to_chars_limited(buf, first_col_to_show));
158  pr("-");
159  pr(detail::_to_chars_limited(buf, first_col_to_show + maxlen));
160  pr("/");
161  pr(detail::_to_chars_limited(buf, contents.len));
162  pr(")");
163  }
164  else if(first_col_to_show < contents.len)
165  {
166  csubstr show = contents.sub(first_col_to_show);
167  pr("[...]");
168  pr(show);
169  if(maxlen > show.len)
170  prn(" ", maxlen - show.len + 5);
171  pr(" (showing columns ");
172  pr(detail::_to_chars_limited(buf, first_col_to_show));
173  pr("-");
174  pr(detail::_to_chars_limited(buf, contents.len));
175  pr("/");
176  pr(detail::_to_chars_limited(buf, contents.len));
177  pr(")");
178  }
179  else
180  {
181  pr("[...]");
182  prn(" ", maxlen + 5);
183  pr(" (not showing, columns=");
184  pr(detail::_to_chars_limited(buf, contents.len));
185  pr(")");
186  }
187  }
188  };
189  // print the location, and compute how many cols it took
190  size_t locsize = location_format(pr, location);
191  // print line
192  if(locsize)
193  {
194  pr(" ");
195  //++locsize;
196  }
197  auto print_call = [&](csubstr after){
198  pr(call);
199  pr(":");
200  if(after.len)
201  pr(after);
202  };
203  size_t jump;
204  if(call.empty())
205  {
206  print_line_maybe_truncated(line);
207  pr("\n");
208  jump = locsize + location.col - first_col_to_show;
209  }
210  else
211  {
212  print_call("\n");
213  print_call("\n");
214  print_call(" ");
215  pr(" ");
216  print_line_maybe_truncated(line);
217  pr("\n");
218  jump = call.len + 2;
219  }
220  // when skipping to the first col, add 5 to adjust for the [...]
221  // leading the line as shown
222  const size_t first_col_jump = first_col_to_show == 0 ? 0 : 5;
223  // print a cursor pointing at the column on the previous printed line
224  auto print_cursor = [&](size_t nocall_jump){
225  if(location.offset == npos)
226  return;
227  if(call.empty())
228  {
229  if(nocall_jump != npos)
230  {
231  prn(" ", nocall_jump + first_col_jump);
232  pr("|\n");
233  prn(" ", nocall_jump + first_col_jump);
234  pr("(here)\n");
235  }
236  }
237  else if(location.col != npos)
238  {
239  print_call(" ");
240  pr(" ");
241  prn(" ", location.col - first_col_to_show + first_col_jump);
242  pr("|\n");
243  print_call(" ");
244  pr(" ");
245  prn(" ", location.col - first_col_to_show + first_col_jump);
246  pr("(here)\n");
247  }
248  };
249  // maybe highlighted zone
250  size_t firstcol = first_col_highlight < line.len ? first_col_highlight : line.len;
251  size_t lastcol = last_col_highlight < line.len ? last_col_highlight : line.len;
252  firstcol = firstcol < maxlen ? firstcol : maxlen;
253  lastcol = lastcol < maxlen ? lastcol : maxlen;
254  if(firstcol < lastcol)
255  {
256  if(!call.empty())
257  {
258  print_call(" ");
259  pr(" ");
260  }
261  else
262  {
263  for(size_t i = 0; i < locsize + firstcol; ++i)
264  pr(" ");
265  }
266  for(size_t i = locsize + firstcol; i < locsize + lastcol; ++i)
267  pr("~");
268  pr(" (cols ");
269  pr(detail::_to_chars_limited(buf, firstcol));
270  pr("-");
271  pr(detail::_to_chars_limited(buf, lastcol));
272  pr("/");
273  pr(detail::_to_chars_limited(buf, line.len));
274  pr(")\n");
275  }
276  if(location.col != npos)
277  {
278  print_cursor(jump);
279  }
280  // maybe print the region
281  if(num_lines_before || num_lines_after)
282  {
283  if(!call.empty())
284  {
285  print_call("\n");
286  print_call(" ");
287  pr("see region:\n");
288  print_call("\n");
289  }
290  else
291  {
292  if(location)
293  {
294  location_format(pr, location);
295  pr(" ");
296  }
297  pr("see region:\n");
298  }
299  csubstr region = detail::_get_text_region(source_buffer, location.offset, num_lines_before, num_lines_after);
300  for(csubstr contents : region.split('\n'))
301  {
302  if(!call.empty())
303  {
304  print_call(" ");
305  }
306  print_line_maybe_truncated(contents);
307  pr("\n");
308  }
309  assert(location.col == npos || location.col >= first_col_to_show);
310  print_cursor(location.col - first_col_to_show);
311  }
312 }
csubstr _get_text_region(csubstr text, size_t pos, size_t num_lines_before, size_t num_lines_after)
Definition: common.cpp:283

References c4::yml::detail::_get_text_region(), c4::yml::Location::col, c4::yml::location_format(), c4::yml::npos, and c4::yml::Location::offset.

◆ err_basic_format()

template<class DumpFn >
void c4::yml::err_basic_format ( DumpFn &&  dumpfn,
csubstr  msg,
ErrorDataBasic const &  errdata 
)

Given an error message and associated basic error data, format it fully as a basic error message.

Parameters
dumpfnfunction taking a csubstr and abstracting a string concatenation operation, such as appending to a std::string or printing to terminal.
msgthe error message
errdatathe error data

For example:

{c++}
/// to output to cerr:
err_basic_format([](csubstr s){
std::cerr.write(s.str, s.len);
}, errmsg, errdata);
/// to build a string:
std::string msg;
error_basic_format([&msg](csubstr s){
msg.append(s.str, s.len);
}, errmsg, errdata);
void err_basic_format(DumpFn &&dumpfn, csubstr msg, ErrorDataBasic const &errdata)
Given an error message and associated basic error data, format it fully as a basic error message.
Definition: error.def.hpp:316

Definition at line 316 of file error.def.hpp.

317 {
318  if(errdata.location)
319  {
320  location_format(dumpfn, errdata.location);
321  std::forward<DumpFn>(dumpfn)(" ");
322  }
323  std::forward<DumpFn>(dumpfn)("ERROR: [basic] ");
324  std::forward<DumpFn>(dumpfn)(msg);
325 }

References c4::yml::ErrorDataBasic::location, and c4::yml::location_format().

◆ err_basic() [1/4]

void c4::yml::err_basic ( Callbacks const &  callbacks,
ErrorDataBasic const &  errdata,
const char *  msg_ 
)

trigger a basic error to its respective handler, with a non-formatted error message.

Definition at line 201 of file common.cpp.

202 {
203  csubstr msg = to_csubstr(msg_);
204  callbacks.m_error_basic(msg, errdata, callbacks.m_user_data);
205  abort(); // the call above should not return, so force it here in case it does // LCOV_EXCL_LINE
206  C4_UNREACHABLE_AFTER_ERR();
207 }
csubstr to_csubstr(substr s) noexcept
neutral version for use in generic code
Definition: substr.hpp:2210

References c4::yml::Callbacks::m_error_basic, c4::yml::Callbacks::m_user_data, and c4::to_csubstr().

◆ err_basic() [2/4]

void c4::yml::err_basic ( ErrorDataBasic const &  errdata,
const char *  msg 
)

trigger a basic error to its respective handler, with a non-formatted error message.

Like (1), but use the current global callbacks.

Definition at line 196 of file common.cpp.

197 {
198  err_basic(get_callbacks(), errdata, msg);
199  C4_UNREACHABLE_AFTER_ERR();
200 }
Callbacks const & get_callbacks()
get the global callbacks
Definition: common.cpp:94
void err_basic(Callbacks const &callbacks, ErrorDataBasic const &errdata, const char *msg_)
trigger a basic error to its respective handler, with a non-formatted error message.
Definition: common.cpp:201

References c4::yml::get_callbacks().

◆ err_basic() [3/4]

template<class ... Args>
void c4::yml::err_basic ( Callbacks const &  callbacks,
ErrorDataBasic const &  errdata,
const char *  fmt,
Args const &...  args 
)

trigger a basic error to its respective handler, with a formatted error message.

Definition at line 326 of file error.hpp.

327 {
328  char errbuf[RYML_ERRMSG_SIZE];
329  csubstr msg = detail::_mk_err_msg(errbuf, to_csubstr(fmt), args...);
330  callbacks.m_error_basic(msg, errdata, callbacks.m_user_data);
331  abort(); // the call above should not return, but force it here in case it does // LCOV_EXCL_LINE
332  C4_UNREACHABLE_AFTER_ERR();
333 }
#define RYML_ERRMSG_SIZE
size for the error message buffer
Definition: common.hpp:34

References c4::yml::Callbacks::m_error_basic, c4::yml::Callbacks::m_user_data, RYML_ERRMSG_SIZE, and c4::to_csubstr().

◆ err_basic() [4/4]

template<class ... Args>
void c4::yml::err_basic ( ErrorDataBasic const &  errdata,
const char *  fmt,
Args const &...  args 
)

trigger a basic error to its respective handler, with a formatted error message.

Like (1), but use the current global callbacks.

Definition at line 336 of file error.hpp.

337 {
338  err_basic(get_callbacks(), errdata, fmt, args...);
339  C4_UNREACHABLE_AFTER_ERR();
340 }
void err_basic(ErrorDataBasic const &errdata, const char *fmt, Args const &...args)
trigger a basic error to its respective handler, with a formatted error message.
Definition: error.hpp:336

References c4::yml::err_basic(), and c4::yml::get_callbacks().

◆ err_parse_format()

template<class DumpFn >
void c4::yml::err_parse_format ( DumpFn &&  dumpfn,
csubstr  msg,
ErrorDataParse const &  errdata 
)

Given an error message and associated parse error data, format it fully as a parse error message.

Parameters
dumpfnfunction taking a csubstr and abstracting a string concatenation operation, such as appending to a std::string or printing to terminal.
msgthe error message
errdatathe error data

For example:

{c++}
/// to output to cerr:
/// this is what err_parse_print() does
err_parse_format([](csubstr s){
std::cerr.write(s.str, s.len);
}, errmsg, errdata);
/// to build a string:
std::string msg;
err_parse_format([](csubstr s){
s.append(s.str, s.len);
}, errmsg, errdata);
void err_parse_format(DumpFn &&dumpfn, csubstr msg, ErrorDataParse const &errdata)
Given an error message and associated parse error data, format it fully as a parse error message.
Definition: error.def.hpp:329
Note
if the (preferably original) source buffer is kept, location_format_with_context() can be used to also an additional rich error message showing the YAML source buffer region around that location.

Definition at line 329 of file error.def.hpp.

330 {
331  if(errdata.ymlloc)
332  {
333  location_format(std::forward<DumpFn>(dumpfn), errdata.ymlloc);
334  std::forward<DumpFn>(dumpfn)(" ");
335  }
336  std::forward<DumpFn>(dumpfn)("ERROR: [parse] ");
337  std::forward<DumpFn>(dumpfn)(msg);
338  if(errdata.cpploc)
339  {
340  std::forward<DumpFn>(dumpfn)("\n");
341  location_format(std::forward<DumpFn>(dumpfn), errdata.cpploc);
342  std::forward<DumpFn>(dumpfn)(" (detected here)");
343  }
344 }

References c4::yml::ErrorDataParse::cpploc, c4::yml::location_format(), and c4::yml::ErrorDataParse::ymlloc.

◆ err_parse() [1/4]

void c4::yml::err_parse ( Callbacks const &  callbacks,
ErrorDataParse const &  errdata,
const char *  msg_ 
)

trigger a parse error to its respective handler, with a non-formatted error message

Definition at line 215 of file common.cpp.

216 {
217  csubstr msg = to_csubstr(msg_);
218  if(callbacks.m_error_parse)
219  callbacks.m_error_parse(msg, errdata, callbacks.m_user_data);
220  // fall to basic error if there is no parse handler set
221  else if(callbacks.m_error_basic)
222  callbacks.m_error_basic(msg, errdata.ymlloc, callbacks.m_user_data);
223  abort(); // the call above should not return, so force it here in case it does // LCOV_EXCL_LINE
224  C4_UNREACHABLE_AFTER_ERR();
225 }

References c4::yml::Callbacks::m_error_basic, c4::yml::Callbacks::m_error_parse, c4::yml::Callbacks::m_user_data, c4::to_csubstr(), and c4::yml::ErrorDataParse::ymlloc.

◆ err_parse() [2/4]

void c4::yml::err_parse ( ErrorDataParse const &  errdata,
const char *  msg 
)

trigger a parse error to its respective handler, with a non-formatted error message.

Like (1), but use the current global callbacks.

Definition at line 210 of file common.cpp.

211 {
212  err_parse(get_callbacks(), errdata, msg);
213  C4_UNREACHABLE_AFTER_ERR();
214 }
void err_parse(Callbacks const &callbacks, ErrorDataParse const &errdata, const char *msg_)
trigger a parse error to its respective handler, with a non-formatted error message
Definition: common.cpp:215

References c4::yml::get_callbacks().

◆ err_parse() [3/4]

template<class ... Args>
void c4::yml::err_parse ( Callbacks const &  callbacks,
ErrorDataParse const &  errdata,
const char *  fmt,
Args const &...  args 
)

trigger a parse error to its respective handler, with a formatted error message

Definition at line 381 of file error.hpp.

382 {
383  char errbuf[RYML_ERRMSG_SIZE];
384  csubstr msg = detail::_mk_err_msg(errbuf, to_csubstr(fmt), args...);
385  if(callbacks.m_error_parse)
386  callbacks.m_error_parse(msg, errdata, callbacks.m_user_data);
387  // fall to basic error if there is no parse handler set, but use errdata.ymlloc instead of errdata.cpploc
388  else if(callbacks.m_error_basic)
389  callbacks.m_error_basic(msg, errdata.ymlloc, callbacks.m_user_data);
390  abort(); // the call above should not return, so force it here in case it does // LCOV_EXCL_LINE
391  C4_UNREACHABLE_AFTER_ERR();
392 }

References c4::yml::Callbacks::m_error_basic, c4::yml::Callbacks::m_error_parse, c4::yml::Callbacks::m_user_data, RYML_ERRMSG_SIZE, c4::to_csubstr(), and c4::yml::ErrorDataParse::ymlloc.

◆ err_parse() [4/4]

template<class ... Args>
void c4::yml::err_parse ( ErrorDataParse const &  errdata,
const char *  fmt,
Args const &...  args 
)

trigger a parse error to its respective handler, with a formatted error message.

Like (1), but use the current global callbacks.

Definition at line 395 of file error.hpp.

396 {
397  err_parse(get_callbacks(), errdata, fmt, args...);
398  C4_UNREACHABLE_AFTER_ERR();
399 }
void err_parse(ErrorDataParse const &errdata, const char *fmt, Args const &...args)
trigger a parse error to its respective handler, with a formatted error message.
Definition: error.hpp:395

References c4::yml::err_parse(), and c4::yml::get_callbacks().

◆ err_visit_format()

template<class DumpFn >
void c4::yml::err_visit_format ( DumpFn &&  dumpfn,
csubstr  msg,
ErrorDataVisit const &  errdata 
)

Given an error message and associated visit error data, format it fully as a visit error message.

Parameters
dumpfnfunction taking a csubstr and abstracting a string concatenation operation, such as appending to a std::string or printing to terminal.
msgthe error message
errdatathe error data

For example:

{c++}
/// to output to cerr:
err_visit_format([](csubstr s){
std::cerr.write(s.str, s.len);
}, errmsg, errdata);
/// to build a string:
std::string msg;
err_visit_format([&msg](csubstr s){
msg.append(s.str, s.len);
}, errmsg, errdata);
@note under certain conditions, it is possible to obtain an
associated location, and subsequently use @ref
location_format_with_context() to also create a rich error message
showing the YAML source buffer region around that location. This is
possible if the (preferably original) source buffer is kept, and
the node location can be retrieved from the parser.
void err_visit_format(DumpFn &&dumpfn, csubstr msg, ErrorDataVisit const &errdata)
Given an error message and associated visit error data, format it fully as a visit error message.
Definition: error.def.hpp:348
void location_format_with_context(DumpFn &&dumpfn, Location const &location, csubstr source_buffer, csubstr call, size_t num_lines_before, size_t num_lines_after, size_t first_col_highlight, size_t last_col_highlight, size_t maxlen)
Generic formatting of a location, printing the source code buffer region around the location.
Definition: error.def.hpp:84
@ YAML
yaml directive: \YAML <version>

Definition at line 348 of file error.def.hpp.

349 {
350  char buf_[32];
351  substr buf(buf_);
352  if(errdata.cpploc)
353  {
354  location_format(dumpfn, errdata.cpploc);
355  std::forward<DumpFn>(dumpfn)(" ");
356  }
357  std::forward<DumpFn>(dumpfn)("ERROR: [visit] ");
358  std::forward<DumpFn>(dumpfn)(msg);
359  if(errdata.node != NONE && errdata.tree != nullptr)
360  {
361  if(errdata.cpploc)
362  {
363  std::forward<DumpFn>(dumpfn)("\n");
364  location_format(dumpfn, errdata.cpploc);
365  std::forward<DumpFn>(dumpfn)(" ");
366  }
367  std::forward<DumpFn>(dumpfn)("ERROR: (");
368  if(errdata.node != NONE)
369  {
370  std::forward<DumpFn>(dumpfn)("node=");
371  std::forward<DumpFn>(dumpfn)(detail::_to_chars_limited(buf, errdata.node));
372  if(errdata.tree != nullptr)
373  std::forward<DumpFn>(dumpfn)(" ");
374  }
375  if(errdata.tree != nullptr)
376  {
377  std::forward<DumpFn>(dumpfn)("tree=");
378  std::forward<DumpFn>(dumpfn)(detail::_to_chars_limited(buf, static_cast<void const*>(errdata.tree)));
379  }
380  std::forward<DumpFn>(dumpfn)(")");
381  }
382 }
@ NONE
an index to none
Definition: common.hpp:251

References c4::yml::ErrorDataVisit::cpploc, c4::yml::location_format(), c4::yml::ErrorDataVisit::node, c4::yml::NONE, and c4::yml::ErrorDataVisit::tree.

◆ err_visit() [1/4]

void c4::yml::err_visit ( Callbacks const &  callbacks,
ErrorDataVisit const &  errdata,
const char *  msg_ 
)

trigger a visit error to its respective handler, with a non-formatted error message

Definition at line 233 of file common.cpp.

234 {
235  csubstr msg = to_csubstr(msg_);
236  if(callbacks.m_error_visit)
237  callbacks.m_error_visit(msg, errdata, callbacks.m_user_data);
238  // fall to basic error if there is no visit handler set
239  else if(callbacks.m_error_basic)
240  callbacks.m_error_basic(msg, errdata.cpploc, callbacks.m_user_data);
241  abort(); // the call above should not return, so force it here in case it does // LCOV_EXCL_LINE
242  C4_UNREACHABLE_AFTER_ERR();
243 }

References c4::yml::ErrorDataVisit::cpploc, c4::yml::Callbacks::m_error_basic, c4::yml::Callbacks::m_error_visit, c4::yml::Callbacks::m_user_data, and c4::to_csubstr().

◆ err_visit() [2/4]

void c4::yml::err_visit ( ErrorDataVisit const &  errdata,
const char *  msg 
)

trigger a visit error to its respective handler, with a non-formatted error message.

Like (1), but uses the current global callbacks.

Definition at line 228 of file common.cpp.

229 {
230  err_visit(get_callbacks(), errdata, msg);
231  C4_UNREACHABLE_AFTER_ERR();
232 }
void err_visit(Callbacks const &callbacks, ErrorDataVisit const &errdata, const char *msg_)
trigger a visit error to its respective handler, with a non-formatted error message
Definition: common.cpp:233

References c4::yml::get_callbacks().

◆ err_visit() [3/4]

template<class ... Args>
void c4::yml::err_visit ( Callbacks const &  callbacks,
ErrorDataVisit const &  errdata,
const char *  fmt,
Args const &...  args 
)

trigger a visit error to its respective handler, with a formatted error message

Definition at line 443 of file error.hpp.

444 {
445  char errbuf[RYML_ERRMSG_SIZE];
446  csubstr msg = detail::_mk_err_msg(errbuf, to_csubstr(fmt), args...);
447  if(callbacks.m_error_visit)
448  callbacks.m_error_visit(msg, errdata, callbacks.m_user_data);
449  // fall to basic error if there is no visit handler set
450  else if(callbacks.m_error_basic)
451  callbacks.m_error_basic(msg, errdata.cpploc, callbacks.m_user_data);
452  abort(); // the call above should not return, so force it here in case it does // LCOV_EXCL_LINE
453  C4_UNREACHABLE_AFTER_ERR();
454 }

References c4::yml::ErrorDataVisit::cpploc, c4::yml::Callbacks::m_error_basic, c4::yml::Callbacks::m_error_visit, c4::yml::Callbacks::m_user_data, RYML_ERRMSG_SIZE, and c4::to_csubstr().

◆ err_visit() [4/4]

template<class ... Args>
void c4::yml::err_visit ( ErrorDataVisit const &  errdata,
const char *  fmt,
Args const &...  args 
)

trigger a visit error to its respective handler, with a formatted error message.

Like (1), but use the current global callbacks.

Definition at line 457 of file error.hpp.

458 {
459  err_visit(get_callbacks(), errdata, fmt, args...);
460  C4_UNREACHABLE_AFTER_ERR();
461 }
void err_visit(ErrorDataVisit const &errdata, const char *fmt, Args const &...args)
trigger a visit error to its respective handler, with a formatted error message.
Definition: error.hpp:457

References c4::yml::err_visit(), and c4::yml::get_callbacks().

◆ format_exc() [1/4]

template<class CharContainer >
void c4::yml::format_exc ( CharContainer *  out,
ExceptionBasic const &  exc 
)

Format a basic exception to an existing char container.

Note
Available only if RYML_DEFAULT_CALLBACK_USES_EXCEPTIONS is defined, and RYML_NO_DEFAULT_CALLBACKS is NOT defined.

Definition at line 524 of file error.hpp.

525 {
526  out->clear();
527  err_basic_format([out](csubstr s){
528  out->append(s.str, s.len);
529  }, csubstr{exc.msg, strlen(exc.msg)}, exc.errdata_basic);
530 }

References c4::yml::err_basic_format(), c4::yml::ExceptionBasic::errdata_basic, and c4::yml::ExceptionBasic::msg.

◆ format_exc() [2/4]

template<class CharContainer >
void c4::yml::format_exc ( CharContainer *  out,
ExceptionParse const &  exc 
)

Format a parse exception to an existing char container.

Note
Available only if RYML_DEFAULT_CALLBACK_USES_EXCEPTIONS is defined, and RYML_NO_DEFAULT_CALLBACKS is NOT defined.

Definition at line 537 of file error.hpp.

538 {
539  out->clear();
540  err_parse_format([out](csubstr s){
541  out->append(s.str, s.len);
542  }, csubstr{exc.msg, strlen(exc.msg)}, exc.errdata_parse);
543 }

References c4::yml::err_parse_format(), c4::yml::ExceptionParse::errdata_parse, and c4::yml::ExceptionBasic::msg.

◆ format_exc() [3/4]

template<class CharContainer >
void c4::yml::format_exc ( CharContainer *  out,
ExceptionVisit const &  exc 
)

Format a visit exception to an existing char container.

Note
Available only if RYML_DEFAULT_CALLBACK_USES_EXCEPTIONS is defined, and RYML_NO_DEFAULT_CALLBACKS is NOT defined.

Definition at line 550 of file error.hpp.

551 {
552  out->clear();
553  err_visit_format([out](csubstr s){
554  out->append(s.str, s.len);
555  }, csubstr{exc.msg, strlen(exc.msg)}, exc.errdata_visit);
556 }

References c4::yml::err_visit_format(), c4::yml::ExceptionVisit::errdata_visit, and c4::yml::ExceptionBasic::msg.

◆ format_exc() [4/4]

template<class CharContainer , class ExceptionT >
CharContainer c4::yml::format_exc ( ExceptionT const &  exc)

Format a parse exception, and return a newly-created char container.

Note
Available only if RYML_DEFAULT_CALLBACK_USES_EXCEPTIONS is defined, and RYML_NO_DEFAULT_CALLBACKS is NOT defined.

Definition at line 564 of file error.hpp.

565 {
566  CharContainer str;
567  format_exc(&str, exc);
568  return str;
569 }
CharContainer format_exc(ExceptionT const &exc)
Format a parse exception, and return a newly-created char container.
Definition: error.hpp:564

References c4::yml::format_exc().