mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-17 09:08:08 +08:00
124 lines
28 KiB
HTML
124 lines
28 KiB
HTML
<!doctype html><html lang=en dir=ltr><head><meta charset=UTF-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=description content="
|
|
Outputting Error Messages
|
|
#
|
|
|
|
toml11 provides error messages that include location information within the file when using functions like toml::parse, toml::get<T>/find<T>, and as_integer(), among others.
|
|
For instance, if a syntax error in an integer is detected during parsing, an error message might look like this:
|
|
[error] bad integer: `_` must be surrounded by digits
|
|
--> internal string at line 64 in file main.cpp
|
|
|
|
|
1 | a = 123__456
|
|
| ^-- invalid underscore
|
|
Hint: valid : -42, 1_000, 1_2_3_4_5, 0xC0FFEE, 0b0010, 0o755
|
|
Hint: invalid: _42, 1__000, 0123
|
|
Or, if a type different from the one actually stored is requested:"><meta name=theme-color media="(prefers-color-scheme: light)" content="#ffffff"><meta name=theme-color media="(prefers-color-scheme: dark)" content="#343a40"><meta name=color-scheme content="light dark"><meta property="og:url" content="https://toruniina.github.io/toml11/docs/features/error_message/"><meta property="og:site_name" content="toml11"><meta property="og:title" content="error message"><meta property="og:description" content="Outputting Error Messages # toml11 provides error messages that include location information within the file when using functions like toml::parse, toml::get<T>/find<T>, and as_integer(), among others.
|
|
For instance, if a syntax error in an integer is detected during parsing, an error message might look like this:
|
|
[error] bad integer: `_` must be surrounded by digits --> internal string at line 64 in file main.cpp | 1 | a = 123__456 | ^-- invalid underscore Hint: valid : -42, 1_000, 1_2_3_4_5, 0xC0FFEE, 0b0010, 0o755 Hint: invalid: _42, 1__000, 0123 Or, if a type different from the one actually stored is requested:"><meta property="og:locale" content="en"><meta property="og:type" content="article"><meta property="article:section" content="docs"><title>error message | toml11</title>
|
|
<link rel=manifest href=/toml11/manifest.json><link rel=icon href=/toml11/favicon.png><link rel=canonical href=https://toruniina.github.io/toml11/docs/features/error_message/><link rel=alternate hreflang=ja href=https://toruniina.github.io/toml11/ja/docs/features/error_message/ title="error message"><link rel=stylesheet href=/toml11/book.min.ddac3efe7ea4c1007ff53e5a8fd67f12517e5d2e99984d8c67a18668f7ecaa03.css integrity="sha256-3aw+/n6kwQB/9T5aj9Z/ElF+XS6ZmE2MZ6GGaPfsqgM=" crossorigin=anonymous><script defer src=/toml11/fuse.min.js></script><script defer src=/toml11/en.search.min.db4a558804ac41f081539588cec8e4a415587801da466728166bb5341dd62411.js integrity="sha256-20pViASsQfCBU5WIzsjkpBVYeAHaRmcoFmu1NB3WJBE=" crossorigin=anonymous></script></head><body dir=ltr><input type=checkbox class="hidden toggle" id=menu-control>
|
|
<input type=checkbox class="hidden toggle" id=toc-control><main class="container flex"><aside class=book-menu><div class=book-menu-content><nav><h2 class=book-brand><a class="flex align-center" href=/toml11/><span>toml11</span></a></h2><div class="book-search hidden"><input type=text id=book-search-input placeholder=Search aria-label=Search maxlength=64 data-hotkeys=s/><div class="book-search-spinner hidden"></div><ul id=book-search-results></ul></div><script>document.querySelector(".book-search").classList.remove("hidden")</script><ul class=book-languages><li><input type=checkbox id=languages class=toggle>
|
|
<label for=languages class="flex justify-between"><a role=button class="flex align-center"><img src=/toml11/svg/translate.svg class=book-icon alt=Languages>
|
|
English</a></label><ul><li><a href=https://toruniina.github.io/toml11/ja/docs/features/error_message/>日本語</a></li></ul></li></ul><ul><li><a href=/toml11/docs/installation/>installation</a><ul></ul></li><li><input type=checkbox id=section-291123c850af98d8b815ccdbb4c33657 class=toggle checked>
|
|
<label for=section-291123c850af98d8b815ccdbb4c33657 class="flex justify-between"><a href=/toml11/docs/features/>features</a></label><ul><li><a href=/toml11/docs/features/parsing_files/>parsing files</a></li><li><a href=/toml11/docs/features/value/>getting values</a></li><li><a href=/toml11/docs/features/error_message/ class=active>error message</a></li><li><a href=/toml11/docs/features/serialize/>serializing values</a></li><li><a href=/toml11/docs/features/configure_types/>configuring types</a></li><li><a href=/toml11/docs/features/literal/>toml literal</a></li><li><a href=/toml11/docs/features/toml_spec/>toml spec</a></li><li><a href=/toml11/docs/features/extension/>extension</a></li></ul></li><li><input type=checkbox id=section-e2e812e55021b82e114feaae98f1a02e class=toggle>
|
|
<label for=section-e2e812e55021b82e114feaae98f1a02e class="flex justify-between"><a href=/toml11/docs/reference/>reference</a></label><ul><li><a href=/toml11/docs/reference/color/>color.hpp</a></li><li><a href=/toml11/docs/reference/comments/>comments.hpp</a></li><li><a href=/toml11/docs/reference/conversion/>conversion.hpp</a></li><li><a href=/toml11/docs/reference/datetime/>datetime.hpp</a></li><li><a href=/toml11/docs/reference/error_info/>error_info.hpp</a></li><li><a href=/toml11/docs/reference/exception/>exception.hpp</a></li><li><a href=/toml11/docs/reference/find/>find.hpp</a></li><li><a href=/toml11/docs/reference/format/>format.hpp</a></li><li><a href=/toml11/docs/reference/from/>from.hpp</a></li><li><a href=/toml11/docs/reference/get/>get.hpp</a></li><li><a href=/toml11/docs/reference/into/>into.hpp</a></li><li><a href=/toml11/docs/reference/literal/>literal.hpp</a></li><li><a href=/toml11/docs/reference/ordered_map/>ordered_map.hpp</a></li><li><a href=/toml11/docs/reference/parser/>parser.hpp</a></li><li><a href=/toml11/docs/reference/result/>result.hpp</a></li><li><a href=/toml11/docs/reference/serializer/>serializer.hpp</a></li><li><a href=/toml11/docs/reference/source_location/>source_location.hpp</a></li><li><a href=/toml11/docs/reference/spec/>spec.hpp</a></li><li><a href=/toml11/docs/reference/toml_fwd/>toml_fwd.hpp</a></li><li><a href=/toml11/docs/reference/toml/>toml.hpp</a></li><li><a href=/toml11/docs/reference/types/>types.hpp</a></li><li><a href=/toml11/docs/reference/value_t/>value_t.hpp</a></li><li><a href=/toml11/docs/reference/value/>value.hpp</a></li><li><a href=/toml11/docs/reference/version/>version.hpp</a></li><li><a href=/toml11/docs/reference/visit/>visit.hpp</a></li></ul></li><li><a href=/toml11/docs/changelog/>changelog</a><ul></ul></li></ul><ul><li><a href=https://github.com/ToruNiina/toml11 target=_blank rel=noopener>GitHub</a></li></ul></nav><script>(function(){var e=document.querySelector("aside .book-menu-content");addEventListener("beforeunload",function(){localStorage.setItem("menu.scrollTop",e.scrollTop)}),e.scrollTop=localStorage.getItem("menu.scrollTop")})()</script></div></aside><div class=book-page><header class=book-header><div class="flex align-center justify-between"><label for=menu-control><img src=/toml11/svg/menu.svg class=book-icon alt=Menu>
|
|
</label><strong>error message</strong>
|
|
<label for=toc-control><img src=/toml11/svg/toc.svg class=book-icon alt="Table of Contents"></label></div><aside class="hidden clearfix"><nav id=TableOfContents><ul><li><a href=#outputting-error-messages>Outputting Error Messages</a><ul><li><a href=#creating-error-messages-from-tomlvalue-location-information>Creating Error Messages from <code>toml::value</code> Location Information</a></li><li><a href=#adding-colors-to-strings>Adding Colors to Strings</a></li><li><a href=#changing-the-prefix-of-error-messages-from-error>Changing the Prefix of Error Messages from <code>[error]</code></a></li><li><a href=#creating-error-messages-referencing-multiple-tomlvalue>Creating Error Messages Referencing Multiple <code>toml::value</code></a></li></ul></li></ul></nav></aside></header><article class="markdown book-article"><h1 id=outputting-error-messages>Outputting Error Messages
|
|
<a class=anchor href=#outputting-error-messages>#</a></h1><p><code>toml11</code> provides error messages that include location information within the file when using functions like <code>toml::parse</code>, <code>toml::get<T>/find<T></code>, and <code>as_integer()</code>, among others.</p><p>For instance, if a syntax error in an integer is detected during parsing, an error message might look like this:</p><pre tabindex=0><code>[error] bad integer: `_` must be surrounded by digits
|
|
--> internal string at line 64 in file main.cpp
|
|
|
|
|
1 | a = 123__456
|
|
| ^-- invalid underscore
|
|
Hint: valid : -42, 1_000, 1_2_3_4_5, 0xC0FFEE, 0b0010, 0o755
|
|
Hint: invalid: _42, 1__000, 0123
|
|
</code></pre><p>Or, if a type different from the one actually stored is requested:</p><pre tabindex=0><code>[error] toml::value::as_string(): bad_cast to string
|
|
--> input.toml
|
|
|
|
|
1 | a = 123_456
|
|
| ^^^^^^^-- the actual type is integer
|
|
</code></pre><p><code>toml11</code> provides methods to create such error messages from <code>toml::value</code>.</p><p>By utilizing this feature, you can inform users not only about TOML syntax errors but also about application-specific errors. For example, if a negative number appears where only positive values are allowed, you can highlight the location within the TOML file to convey the error to the user.</p><h2 id=creating-error-messages-from-tomlvalue-location-information>Creating Error Messages from <code>toml::value</code> Location Information
|
|
<a class=anchor href=#creating-error-messages-from-tomlvalue-location-information>#</a></h2><p><code>toml::value</code> retains information about the location where it was parsed.</p><p>This information is encapsulated in <code>toml::source_location</code> and can be retrieved using <code>toml::value::location()</code>.</p><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-cpp data-lang=cpp><span style=display:flex><span><span style=color:#66d9ef>const</span> toml<span style=color:#f92672>::</span>value<span style=color:#f92672>&</span> a <span style=color:#f92672>=</span> input.at(<span style=color:#e6db74>"a"</span>);
|
|
</span></span><span style=display:flex><span><span style=color:#66d9ef>const</span> toml<span style=color:#f92672>::</span>source_location src <span style=color:#f92672>=</span> a.location();
|
|
</span></span></code></pre></div><p>When a file is parsed with <code>toml::parse</code>, the TOML filename and line numbers are stored.</p><p>If parsed with <code>toml::parse_str</code>, the TOML filename is not available, but instead, the filename and line number of the C++ source code that called <code>toml::parse_str</code> are stored as the TOML filename. The first example on this page was output from <code>toml::parse_str</code>. Note the filename part.</p><p>For details, see the <a href=https://toruniina.github.io/toml11/docs/reference/source_location/>reference</a>.</p><p>You can build error information by passing a <code>toml::source_location</code> or <code>toml::value</code> and the associated error message to <code>toml::make_error_info</code>. Passing this to <code>toml::format_error</code> formats the error message into a <code>std::string</code>.</p><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-cpp data-lang=cpp><span style=display:flex><span><span style=color:#66d9ef>const</span> toml<span style=color:#f92672>::</span>value<span style=color:#f92672>&</span> a <span style=color:#f92672>=</span> input.at(<span style=color:#e6db74>"a"</span>);
|
|
</span></span><span style=display:flex><span><span style=color:#66d9ef>if</span>(a.as_integer() <span style=color:#f92672><</span> <span style=color:#ae81ff>0</span>)
|
|
</span></span><span style=display:flex><span>{
|
|
</span></span><span style=display:flex><span> <span style=color:#66d9ef>const</span> toml<span style=color:#f92672>::</span>error_info err <span style=color:#f92672>=</span> toml<span style=color:#f92672>::</span>make_error_info(
|
|
</span></span><span style=display:flex><span> <span style=color:#e6db74>"positive integer is required"</span>, <span style=color:#75715e>// Error title
|
|
</span></span></span><span style=display:flex><span><span style=color:#75715e></span> a, <span style=color:#e6db74>"but got negative value"</span> <span style=color:#75715e>// Message next to the value
|
|
</span></span></span><span style=display:flex><span><span style=color:#75715e></span> );
|
|
</span></span><span style=display:flex><span> std<span style=color:#f92672>::</span>cerr <span style=color:#f92672><<</span> toml<span style=color:#f92672>::</span>format_error(err) <span style=color:#f92672><<</span> std<span style=color:#f92672>::</span>endl;
|
|
</span></span><span style=display:flex><span>}
|
|
</span></span></code></pre></div><p>This will output:</p><pre tabindex=0><code>[error] positive integer is required
|
|
--> input.toml
|
|
|
|
|
1 | a = -123456
|
|
| ^^^^^^^-- but got negative value
|
|
</code></pre><p>You can also add a supplementary message at the end. This part is not indented.</p><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-cpp data-lang=cpp><span style=display:flex><span><span style=color:#66d9ef>const</span> toml<span style=color:#f92672>::</span>value<span style=color:#f92672>&</span> a <span style=color:#f92672>=</span> input.at(<span style=color:#e6db74>"a"</span>);
|
|
</span></span><span style=display:flex><span><span style=color:#66d9ef>if</span>(a.as_integer() <span style=color:#f92672><</span> <span style=color:#ae81ff>0</span>)
|
|
</span></span><span style=display:flex><span>{
|
|
</span></span><span style=display:flex><span> <span style=color:#66d9ef>const</span> toml<span style=color:#f92672>::</span>error_info err <span style=color:#f92672>=</span> toml<span style=color:#f92672>::</span>make_error_info(
|
|
</span></span><span style=display:flex><span> <span style=color:#e6db74>"positive integer is required"</span>, <span style=color:#75715e>// Error title
|
|
</span></span></span><span style=display:flex><span><span style=color:#75715e></span> a, <span style=color:#e6db74>"but got negative value"</span>, <span style=color:#75715e>// Message next to the value
|
|
</span></span></span><span style=display:flex><span><span style=color:#75715e></span> <span style=color:#e6db74>"Hint: `a` means length of the data"</span> <span style=color:#75715e>// Supplementary message
|
|
</span></span></span><span style=display:flex><span><span style=color:#75715e></span> );
|
|
</span></span><span style=display:flex><span> std<span style=color:#f92672>::</span>cerr <span style=color:#f92672><<</span> toml<span style=color:#f92672>::</span>format_error(err) <span style=color:#f92672><<</span> std<span style=color:#f92672>::</span>endl;
|
|
</span></span><span style=display:flex><span>}
|
|
</span></span></code></pre></div><p>This will output:</p><pre tabindex=0><code>[error] positive integer is required
|
|
--> input.toml
|
|
|
|
|
1 | a = -123456
|
|
| ^^^^^^^-- but got negative value
|
|
Hint: `a` means length of the data
|
|
</code></pre><blockquote class="book-hint info"><p>The ability to output lines from the file using <code>toml::value</code> is because the parsed file is retained in memory as a string.</p><p>The parsed string is shared by <code>toml::value</code> via a <code>std::shared_ptr</code>. Copying it does not duplicate the entire file string. The file information is freed from memory when all <code>toml::value</code> instances constructed from parsing the file are destructed.</p><p>Therefore, when using this in an application, it is recommended to extract and store the required values during loading rather than directly storing <code>toml::value</code>.</p></blockquote><h2 id=adding-colors-to-strings>Adding Colors to Strings
|
|
<a class=anchor href=#adding-colors-to-strings>#</a></h2><p>You can add color to error messages using ANSI escape codes.</p><p>If <code>TOML11_COLORIZE_ERROR_MESSAGE</code> is defined at compile time, the error messages output by toml11 will be colored by default.</p><p>If not, you can enable color for subsequent error messages by calling <code>toml::color::enable()</code>. Conversely, if you do not want colored output, for example, because the output is not to a console, call <code>toml::color::disable()</code>. You can check whether coloring is enabled at any point by calling <code>toml::color::should_color()</code>.</p><p>Additionally, while the error title, error message, and supplementary information are not colored by default, you can use manipulators from toml::color to add color to them.</p><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-cpp data-lang=cpp><span style=display:flex><span>std<span style=color:#f92672>::</span>ostringstream oss;
|
|
</span></span><span style=display:flex><span>oss <span style=color:#f92672><<</span> toml<span style=color:#f92672>::</span>color<span style=color:#f92672>::</span>red <span style=color:#f92672><<</span> <span style=color:#e6db74>"but got negative value"</span>;
|
|
</span></span><span style=display:flex><span>
|
|
</span></span><span style=display:flex><span><span style=color:#66d9ef>const</span> toml<span style=color:#f92672>::</span>error_info err <span style=color:#f92672>=</span> toml<span style=color:#f92672>::</span>make_error_info(
|
|
</span></span><span style=display:flex><span> <span style=color:#e6db74>"positive integer is required"</span>, <span style=color:#75715e>// Error title
|
|
</span></span></span><span style=display:flex><span><span style=color:#75715e></span> a, oss.str(), <span style=color:#75715e>// Message next to the value
|
|
</span></span></span><span style=display:flex><span><span style=color:#75715e></span> <span style=color:#e6db74>"Hint: `a` means length of the data"</span> <span style=color:#75715e>// Supplementary message
|
|
</span></span></span><span style=display:flex><span><span style=color:#75715e></span> );
|
|
</span></span></code></pre></div><p>For more details, see the <a href=https://toruniina.github.io/toml11/docs/reference/color/>reference</a>.</p><h2 id=changing-the-prefix-of-error-messages-from-error>Changing the Prefix of Error Messages from <code>[error]</code>
|
|
<a class=anchor href=#changing-the-prefix-of-error-messages-from-error>#</a></h2><p>There may be different types of errors, and the default <code>[error]</code> prefix might not always be appropriate.</p><p>With <code>toml::format_error</code>, you can provide a <code>std::string</code> before <code>toml::error_info</code> to replace the <code>[error]</code> prefix.</p><p>For example:</p><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-cpp data-lang=cpp><span style=display:flex><span><span style=color:#66d9ef>const</span> toml<span style=color:#f92672>::</span>value<span style=color:#f92672>&</span> a <span style=color:#f92672>=</span> input.at(<span style=color:#e6db74>"a"</span>);
|
|
</span></span><span style=display:flex><span><span style=color:#66d9ef>if</span>(a.as_integer() <span style=color:#f92672><</span> <span style=color:#ae81ff>0</span>)
|
|
</span></span><span style=display:flex><span>{
|
|
</span></span><span style=display:flex><span> <span style=color:#66d9ef>const</span> toml<span style=color:#f92672>::</span>error_info err <span style=color:#f92672>=</span> toml<span style=color:#f92672>::</span>make_error_info(
|
|
</span></span><span style=display:flex><span> <span style=color:#e6db74>"positive integer is required"</span>, <span style=color:#75715e>// Error title
|
|
</span></span></span><span style=display:flex><span><span style=color:#75715e></span> a, <span style=color:#e6db74>"but got negative value"</span> <span style=color:#75715e>// Message next to the value
|
|
</span></span></span><span style=display:flex><span><span style=color:#75715e></span> );
|
|
</span></span><span style=display:flex><span>
|
|
</span></span><span style=display:flex><span> std<span style=color:#f92672>::</span>ostringstream prefix;
|
|
</span></span><span style=display:flex><span> prefix <span style=color:#f92672><<</span> toml<span style=color:#f92672>::</span>color<span style=color:#f92672>::</span>bold <span style=color:#f92672><<</span> toml<span style=color:#f92672>::</span>color<span style=color:#f92672>::</span>yellow <span style=color:#f92672><<</span> <span style=color:#e6db74>"[warn]"</span>;
|
|
</span></span><span style=display:flex><span> std<span style=color:#f92672>::</span>cerr <span style=color:#f92672><<</span> toml<span style=color:#f92672>::</span>format_error(prefix.str(), err) <span style=color:#f92672><<</span> std<span style=color:#f92672>::</span>endl;
|
|
</span></span><span style=display:flex><span> <span style=color:#66d9ef>return</span> <span style=color:#ae81ff>0</span>;
|
|
</span></span><span style=display:flex><span>}
|
|
</span></span><span style=display:flex><span><span style=color:#66d9ef>else</span>
|
|
</span></span><span style=display:flex><span>{
|
|
</span></span><span style=display:flex><span> <span style=color:#66d9ef>return</span> a.as_integer();
|
|
</span></span><span style=display:flex><span>}
|
|
</span></span></code></pre></div><p>This will output a warning starting with <code>[warn]</code>.</p><p>Additionally, you can create error messages without the <code>[error]</code> prefix by directly passing the components of <code>error_info</code> to <code>toml::format_error</code>.</p><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-cpp data-lang=cpp><span style=display:flex><span><span style=color:#66d9ef>const</span> toml<span style=color:#f92672>::</span>value<span style=color:#f92672>&</span> a <span style=color:#f92672>=</span> input.at(<span style=color:#e6db74>"a"</span>);
|
|
</span></span><span style=display:flex><span><span style=color:#66d9ef>if</span>(a.as_integer() <span style=color:#f92672><</span> <span style=color:#ae81ff>0</span>)
|
|
</span></span><span style=display:flex><span>{
|
|
</span></span><span style=display:flex><span> std<span style=color:#f92672>::</span>cerr <span style=color:#f92672><<</span> toml<span style=color:#f92672>::</span>format_error(
|
|
</span></span><span style=display:flex><span> <span style=color:#e6db74>"[warn] positive integer is required"</span>, <span style=color:#75715e>// Error title
|
|
</span></span></span><span style=display:flex><span><span style=color:#75715e></span> a, <span style=color:#e6db74>"but got negative value"</span> <span style=color:#75715e>// Message next to the value
|
|
</span></span></span><span style=display:flex><span><span style=color:#75715e></span> ) <span style=color:#f92672><<</span> std<span style=color:#f92672>::</span>endl;
|
|
</span></span><span style=display:flex><span> <span style=color:#66d9ef>return</span> <span style=color:#ae81ff>0</span>;
|
|
</span></span><span style=display:flex><span>}
|
|
</span></span><span style=display:flex><span><span style=color:#66d9ef>else</span>
|
|
</span></span><span style=display:flex><span>{
|
|
</span></span><span style=display:flex><span> <span style=color:#66d9ef>return</span> a.as_integer()
|
|
</span></span><span style=display:flex><span>}
|
|
</span></span></code></pre></div><h2 id=creating-error-messages-referencing-multiple-tomlvalue>Creating Error Messages Referencing Multiple <code>toml::value</code>
|
|
<a class=anchor href=#creating-error-messages-referencing-multiple-tomlvalue>#</a></h2><p>In application settings, the range of permissible values might change based on previously read values.</p><p>In such cases, you may want to output the values causing the error simultaneously.</p><p><code>toml::format_error</code> and <code>toml::make_error_info</code> can take multiple pairs of <code>toml::value</code> and their corresponding error messages as <code>std::string</code>.</p><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-cpp data-lang=cpp><span style=display:flex><span>std<span style=color:#f92672>::</span>cerr <span style=color:#f92672><<</span> toml<span style=color:#f92672>::</span>format_error(
|
|
</span></span><span style=display:flex><span> <span style=color:#e6db74>"[error] invalid range"</span>,
|
|
</span></span><span style=display:flex><span> a, <span style=color:#e6db74>"minimum value is defined here"</span>,
|
|
</span></span><span style=display:flex><span> b, <span style=color:#e6db74>"maximum value is defined here"</span>,
|
|
</span></span><span style=display:flex><span> c, <span style=color:#e6db74>"and it exceeds the range"</span>
|
|
</span></span><span style=display:flex><span> ) <span style=color:#f92672><<</span> std<span style=color:#f92672>::</span>endl;
|
|
</span></span></code></pre></div><p>You can also add supplementary information at the end.</p><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-cpp data-lang=cpp><span style=display:flex><span>std<span style=color:#f92672>::</span>cerr <span style=color:#f92672><<</span> toml<span style=color:#f92672>::</span>format_error(
|
|
</span></span><span style=display:flex><span> <span style=color:#e6db74>"[error] invalid range"</span>,
|
|
</span></span><span style=display:flex><span> a, <span style=color:#e6db74>"minimum value is defined here"</span>,
|
|
</span></span><span style=display:flex><span> b, <span style=color:#e6db74>"maximum value is defined here"</span>,
|
|
</span></span><span style=display:flex><span> c, <span style=color:#e6db74>"and it exceeds the range"</span>,
|
|
</span></span><span style=display:flex><span> <span style=color:#e6db74>"Hint: all the values must be in the range [a, b)"</span>
|
|
</span></span><span style=display:flex><span> ) <span style=color:#f92672><<</span> std<span style=color:#f92672>::</span>endl;
|
|
</span></span></code></pre></div><p>When passing <code>toml::value</code> or <code>toml::source_location</code>, an error message related to it must follow. If not, it will result in a very confusing compilation error.</p></article><footer class=book-footer><div class="flex flex-wrap justify-between"></div><script>(function(){function e(e){const t=window.getSelection(),n=document.createRange();n.selectNodeContents(e),t.removeAllRanges(),t.addRange(n)}document.querySelectorAll("pre code").forEach(t=>{t.addEventListener("click",function(){if(window.getSelection().toString())return;e(t.parentElement),navigator.clipboard&&navigator.clipboard.writeText(t.parentElement.textContent)})})})()</script></footer><label for=menu-control class="hidden book-menu-overlay"></label></div><aside class=book-toc><div class=book-toc-content><nav id=TableOfContents><ul><li><a href=#outputting-error-messages>Outputting Error Messages</a><ul><li><a href=#creating-error-messages-from-tomlvalue-location-information>Creating Error Messages from <code>toml::value</code> Location Information</a></li><li><a href=#adding-colors-to-strings>Adding Colors to Strings</a></li><li><a href=#changing-the-prefix-of-error-messages-from-error>Changing the Prefix of Error Messages from <code>[error]</code></a></li><li><a href=#creating-error-messages-referencing-multiple-tomlvalue>Creating Error Messages Referencing Multiple <code>toml::value</code></a></li></ul></li></ul></nav></div></aside></main></body></html> |