1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 3<title>Custom payloads - Boost.Outcome documentation</title> 4<link rel="stylesheet" href="../../css/boost.css" type="text/css"> 5<meta name="generator" content="Hugo 0.52 with Boostdoc theme"> 6<meta name="viewport" content="width=device-width,initial-scale=1.0"/> 7 8<link rel="icon" href="../../images/favicon.ico" type="image/ico"/> 9<body><div class="spirit-nav"> 10<a accesskey="p" href="../../tutorial/advanced.html"><img src="../../images/prev.png" alt="Prev"></a> 11 <a accesskey="u" href="../../tutorial/advanced.html"><img src="../../images/up.png" alt="Up"></a> 12 <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../tutorial/advanced/payload/copy_file.html"><img src="../../images/next.png" alt="Next"></a></div><div id="content"> 13 14 <div class="titlepage"><div><div><h1 style="clear: both">Custom payloads</h1></div></div></div> 15<p>So far in this tutorial, type <code>EC</code> in <code>result<T, EC></code> has always been a 16<code>std::error_code</code> or <code>boost::system::error_code</code>. With <code>NoValuePolicy</code> 17set to <a href="../../reference/aliases/default_policy.html" class="api-reference"><code>default_policy<T, EC, EP></code></a> 18, <code>EC</code> needs 19in fact to merely satisfy the trait 20<a href="../../reference/traits/is_error_code_available.html" class="api-reference"><code>is_error_code_available<T></code></a> 21 22for <code>EC</code> to be treated as if an <code>error_code</code>. Outcome specialises that 23trait for <code>std::error_code</code> and <code>boost::system::error_code</code>, 24hence they “just work”.</p> 25 26<p>If no specialisation exists, <code>trait::is_error_code_available<EC></code> is true 27if there exists some ADL discovered free function <code>make_error_code(EC)</code>.</p> 28 29<p>Thus, we can in fact use any custom <code>EC</code> type we like, including one carrying additional 30information, or <em>payload</em>. This payload can carry anything you like, and you can tell 31Outcome to do various things with that payload under various circumstances. For example:</p> 32 33<ol> 34<li>If the user tries to observe an unsuccessful <code>result</code>, throw a custom exception 35containing the cause of failure with accompanying context from the payload.</li> 36<li>If the user ever constructs an <code>outcome</code> from a payload carrying <code>result</code>, 37set the exception ptr in the constructed <code>outcome</code> to a custom exception 38containing the cause of the failure with accompanying context from the payload.</li> 39<li>Transmit a stack backtrace specifying the exact point at which failure occurred, 40symbolising that backtrace into human readable text at the moment of conversion into human 41readable text.</li> 42<li>Upon a namespace-localised <code>result</code> from library A being copy/moved into a 43namespace-localised <code>result</code> from C bindings library B, set the C <code>errno</code> if 44the error code and category map onto the <code>errno</code> domain.</li> 45</ol> 46 47<p>There are many, many other options of course. This tutorial can only cover a 48reasonable subset. This section covers Example 1 above, throwing custom exceptions 49with payload upon observation of an unsuccessful <code>result</code>.</p> 50 51 52 53 </div><p><small>Last revised: February 08, 2019 at 22:18:08 UTC</small></p> 54<hr> 55<div class="spirit-nav"> 56<a accesskey="p" href="../../tutorial/advanced.html"><img src="../../images/prev.png" alt="Prev"></a> 57 <a accesskey="u" href="../../tutorial/advanced.html"><img src="../../images/up.png" alt="Up"></a> 58 <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../tutorial/advanced/payload/copy_file.html"><img src="../../images/next.png" alt="Next"></a></div></body> 59</html> 60