rapidyaml  0.11.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:15

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

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

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

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

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

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 306 of file error.hpp.

307 {
308  char errbuf[RYML_ERRMSG_SIZE];
309  csubstr msg = detail::_mk_err_msg(errbuf, to_csubstr(fmt), args...);
310  callbacks.m_error_basic(msg, errdata, callbacks.m_user_data);
311  abort(); // the call above should not return, but force it here in case it does // LCOV_EXCL_LINE
312  C4_UNREACHABLE_AFTER_ERR();
313 }
#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 316 of file error.hpp.

317 {
318  err_basic(get_callbacks(), errdata, fmt, args...);
319  C4_UNREACHABLE_AFTER_ERR();
320 }
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:316

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:328
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 328 of file error.def.hpp.

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

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 361 of file error.hpp.

362 {
363  char errbuf[RYML_ERRMSG_SIZE];
364  csubstr msg = detail::_mk_err_msg(errbuf, to_csubstr(fmt), args...);
365  if(callbacks.m_error_parse)
366  callbacks.m_error_parse(msg, errdata, callbacks.m_user_data);
367  // fall to basic error if there is no parse handler set, but use errdata.ymlloc instead of errdata.cpploc
368  else if(callbacks.m_error_basic)
369  callbacks.m_error_basic(msg, errdata.ymlloc, callbacks.m_user_data);
370  abort(); // the call above should not return, so force it here in case it does // LCOV_EXCL_LINE
371  C4_UNREACHABLE_AFTER_ERR();
372 }

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 375 of file error.hpp.

376 {
377  err_parse(get_callbacks(), errdata, fmt, args...);
378  C4_UNREACHABLE_AFTER_ERR();
379 }
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:375

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:347
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:83
@ YAML
yaml directive: \YAML <version>

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

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

424 {
425  char errbuf[RYML_ERRMSG_SIZE];
426  csubstr msg = detail::_mk_err_msg(errbuf, to_csubstr(fmt), args...);
427  if(callbacks.m_error_visit)
428  callbacks.m_error_visit(msg, errdata, callbacks.m_user_data);
429  // fall to basic error if there is no visit handler set
430  else if(callbacks.m_error_basic)
431  callbacks.m_error_basic(msg, errdata.cpploc, callbacks.m_user_data);
432  abort(); // the call above should not return, so force it here in case it does // LCOV_EXCL_LINE
433  C4_UNREACHABLE_AFTER_ERR();
434 }

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 437 of file error.hpp.

438 {
439  err_visit(get_callbacks(), errdata, fmt, args...);
440  C4_UNREACHABLE_AFTER_ERR();
441 }
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:437

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 504 of file error.hpp.

505 {
506  out->clear();
507  err_basic_format([out](csubstr s){
508  out->append(s.str, s.len);
509  }, csubstr{exc.msg, strlen(exc.msg)}, exc.errdata_basic);
510 }

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 517 of file error.hpp.

518 {
519  out->clear();
520  err_parse_format([out](csubstr s){
521  out->append(s.str, s.len);
522  }, csubstr{exc.msg, strlen(exc.msg)}, exc.errdata_parse);
523 }

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 530 of file error.hpp.

531 {
532  out->clear();
533  err_visit_format([out](csubstr s){
534  out->append(s.str, s.len);
535  }, csubstr{exc.msg, strlen(exc.msg)}, exc.errdata_visit);
536 }

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 544 of file error.hpp.

545 {
546  CharContainer str;
547  format_exc(&str, exc);
548  return str;
549 }
CharContainer format_exc(ExceptionT const &exc)
Format a parse exception, and return a newly-created char container.
Definition: error.hpp:544

References c4::yml::format_exc().