• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>Mapping the HTTP library into the Application &lt;sup&gt;1&lt;/sup&gt;&frasl;&lt;sub&gt;2&lt;/sub&gt; - 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/interop/app.html"><img src="../../../images/prev.png" alt="Prev"></a>
11    <a accesskey="u" href="../../../tutorial/advanced/interop.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/interop/app-map-httplib2.html"><img src="../../../images/next.png" alt="Next"></a></div><div id="content">
13  <div class="titlepage"><div><div><h1 style="clear: both">Mapping the HTTP library into the Application <code>1/2</code></h1></div></div></div>
14  <p>Firstly, remember that we are the application writer who has the problem of
15integrating three third party libraries into our application&rsquo;s Outcome-based
16failure handling mechanism. We cannot modify those third party library
17sources; we must be <em>non-intrusive</em>.</p>
18
19<p>We start by dealing with the HTTP library. We will integrate this
20into our application by wrapping up <code>httplib::failure</code> into a custom
21STL exception type. We then type erase it into an <code>exception_ptr</code>
22instance. Please note that this code is exclusively defined in the <code>app</code> namespace:</p>
23
24<div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="k">namespace</span> <span class="n">app</span>
25<span class="p">{</span>
26  <span class="c1">// Specialise an exception type for httplib errors
27</span><span class="c1"></span>  <span class="k">struct</span> <span class="nl">httplib_error</span> <span class="p">:</span> <span class="n">std</span><span class="o">::</span><span class="n">runtime_error</span>
28  <span class="p">{</span>
29    <span class="c1">// passthrough
30</span><span class="c1"></span>    <span class="k">using</span> <span class="n">std</span><span class="o">::</span><span class="n">runtime_error</span><span class="o">::</span><span class="n">runtime_error</span><span class="p">;</span>
31    <span class="n">httplib_error</span><span class="p">(</span><span class="n">httplib</span><span class="o">::</span><span class="n">failure</span> <span class="n">_failure</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">msg</span><span class="p">)</span>
32        <span class="o">:</span> <span class="n">std</span><span class="o">::</span><span class="n">runtime_error</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">msg</span><span class="p">))</span>
33        <span class="p">,</span> <span class="n">failure</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">_failure</span><span class="p">))</span>
34    <span class="p">{</span>
35    <span class="p">}</span>
36
37    <span class="c1">// the original failure
38</span><span class="c1"></span>    <span class="n">httplib</span><span class="o">::</span><span class="n">failure</span> <span class="n">failure</span><span class="p">;</span>
39  <span class="p">};</span>
40
41  <span class="c1">// Type erase httplib::result&lt;U&gt; into a httplib_error exception ptr
42</span><span class="c1"></span>  <span class="k">template</span> <span class="o">&lt;</span><span class="k">class</span><span class="err"> </span><span class="nc">U</span><span class="o">&gt;</span>  <span class="c1">//
43</span><span class="c1"></span>  <span class="kr">inline</span> <span class="n">std</span><span class="o">::</span><span class="n">exception_ptr</span> <span class="n">make_httplib_exception</span><span class="p">(</span><span class="k">const</span> <span class="n">httplib</span><span class="o">::</span><span class="n">result</span><span class="o">&lt;</span><span class="n">U</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="n">src</span><span class="p">)</span>
44  <span class="p">{</span>
45    <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">str</span><span class="p">(</span><span class="s">&#34;httplib failed with error &#34;</span><span class="p">);</span>
46    <span class="k">switch</span><span class="p">(</span><span class="n">src</span><span class="p">.</span><span class="n">error</span><span class="p">().</span><span class="n">status</span><span class="p">)</span>
47    <span class="p">{</span>
48    <span class="k">case</span> <span class="n">httplib</span><span class="o">::</span><span class="n">status_code</span><span class="o">::</span><span class="nl">success</span><span class="p">:</span>
49      <span class="n">str</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#34;success&#34;</span><span class="p">);</span>
50      <span class="k">break</span><span class="p">;</span>
51    <span class="k">case</span> <span class="n">httplib</span><span class="o">::</span><span class="n">status_code</span><span class="o">::</span><span class="nl">bad_request</span><span class="p">:</span>
52      <span class="n">str</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#34;bad request&#34;</span><span class="p">);</span>
53      <span class="k">break</span><span class="p">;</span>
54    <span class="k">case</span> <span class="n">httplib</span><span class="o">::</span><span class="n">status_code</span><span class="o">::</span><span class="nl">access_denied</span><span class="p">:</span>
55      <span class="n">str</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#34;access denied&#34;</span><span class="p">);</span>
56      <span class="k">break</span><span class="p">;</span>
57    <span class="k">case</span> <span class="n">httplib</span><span class="o">::</span><span class="n">status_code</span><span class="o">::</span><span class="nl">logon_failed</span><span class="p">:</span>
58      <span class="n">str</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#34;logon failed&#34;</span><span class="p">);</span>
59      <span class="k">break</span><span class="p">;</span>
60    <span class="k">case</span> <span class="n">httplib</span><span class="o">::</span><span class="n">status_code</span><span class="o">::</span><span class="nl">forbidden</span><span class="p">:</span>
61      <span class="n">str</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#34;forbidden&#34;</span><span class="p">);</span>
62      <span class="k">break</span><span class="p">;</span>
63    <span class="k">case</span> <span class="n">httplib</span><span class="o">::</span><span class="n">status_code</span><span class="o">::</span><span class="nl">not_found</span><span class="p">:</span>
64      <span class="n">str</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#34;not found&#34;</span><span class="p">);</span>
65      <span class="k">break</span><span class="p">;</span>
66    <span class="k">case</span> <span class="n">httplib</span><span class="o">::</span><span class="n">status_code</span><span class="o">::</span><span class="nl">internal_error</span><span class="p">:</span>
67      <span class="n">str</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#34;internal error&#34;</span><span class="p">);</span>
68      <span class="k">break</span><span class="p">;</span>
69    <span class="p">}</span>
70    <span class="n">str</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#34; [url was &#34;</span><span class="p">);</span>
71    <span class="n">str</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="n">src</span><span class="p">.</span><span class="n">error</span><span class="p">().</span><span class="n">url</span><span class="p">);</span>
72    <span class="n">str</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#34;]&#34;</span><span class="p">);</span>
73    <span class="k">return</span> <span class="n">std</span><span class="o">::</span><span class="n">make_exception_ptr</span><span class="p">(</span><span class="n">httplib_error</span><span class="p">(</span><span class="n">src</span><span class="p">.</span><span class="n">error</span><span class="p">(),</span> <span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">str</span><span class="p">)));</span>
74  <span class="p">}</span>
75<span class="p">}</span>  <span class="c1">// namespace app
76</span><span class="c1"></span></code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/finale.cpp#L202" class="code-snippet-url" target="_blank">View this code on Github</a></div>
77
78
79<p>Most of the complexity in this code fragment is driven by the need to create
80some sort of descriptive string for <code>std::runtime_error</code>
81so its <code>.what()</code> returns a useful summary of the original failure. This
82is the main purpose of the <code>app::make_httplib_exception()</code> function.</p>
83
84<p>(Note that if you have Reflection in your C++ compiler, it may be possible to script
85the conversion of enum values to string representations)</p>
86
87<p>The only real thing to note about <code>app::httplib_error</code> is that it squirrels away
88the original <code>httplib::failure</code> in case that is ever needed.</p>
89
90
91        </div><p><small>Last revised: February 11, 2019 at 13:38:04 UTC</small></p>
92<hr>
93<div class="spirit-nav">
94<a accesskey="p" href="../../../tutorial/advanced/interop/app.html"><img src="../../../images/prev.png" alt="Prev"></a>
95    <a accesskey="u" href="../../../tutorial/advanced/interop.html"><img src="../../../images/up.png" alt="Up"></a>
96    <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/advanced/interop/app-map-httplib2.html"><img src="../../../images/next.png" alt="Next"></a></div></body>
97</html>
98