Clear error codes idiomatically

This commit is contained in:
Vinnie Falco
2019-01-20 09:50:43 -08:00
parent 9ea70bfbe5
commit 944b5dcda7
29 changed files with 118 additions and 112 deletions

View File

@ -1,3 +1,9 @@
Version 206
* Clear error codes idiomatically
--------------------------------------------------------------------------------
Version 205
* Doc work

View File

@ -154,7 +154,7 @@ detect_ssl(
{
// This is a fast way to indicate success
// without retrieving the default category.
ec.assign(0, ec.category());
ec = {};
return result;
}

View File

@ -1094,7 +1094,7 @@ read_and_print_body(
p.get().body().size = sizeof(buf);
read(stream, buffer, p, ec);
if(ec == error::need_buffer)
ec.assign(0, ec.category());
ec = {};
if(ec)
return;
os.write(buf, sizeof(buf) - p.get().body().size);
@ -1201,7 +1201,7 @@ print_chunked_body(
else if(ec != error::end_of_chunk)
return;
else
ec.assign(0, ec.category());
ec = {};
// We got a whole chunk, print the extensions:
for(auto const& extension : ce)

View File

@ -191,7 +191,7 @@ public:
{
// Rationale:
// http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
ec.assign(0, ec.category());
ec = {};
}
if(ec)
return fail(ec, "shutdown");

View File

@ -113,7 +113,7 @@ do_session(
{
// Rationale:
// http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
ec.assign(0, ec.category());
ec = {};
}
if(ec)
return fail(ec, "shutdown");

View File

@ -111,7 +111,7 @@ int main(int argc, char** argv)
{
// Rationale:
// http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
ec.assign(0, ec.category());
ec = {};
}
if(ec)
throw beast::system_error{ec};

View File

@ -80,7 +80,7 @@ struct basic_dynamic_body
init(boost::optional<
std::uint64_t> const&, error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
template<class ConstBufferSequence>
@ -112,7 +112,7 @@ struct basic_dynamic_body
void
finish(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
};
#endif
@ -142,13 +142,13 @@ struct basic_dynamic_body
void
init(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
boost::optional<std::pair<const_buffers_type, bool>>
get(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
return {{body_.data(), false}};
}
};

View File

@ -319,7 +319,7 @@ init(error_code& ec)
// to indicate no error.
//
// We don't do anything fancy so set "no error"
ec.assign(0, ec.category());
ec = {};
}
// This function is called repeatedly by the serializer to
@ -349,7 +349,7 @@ get(error_code& ec) ->
// into the library to get the generic category because
// that saves us a possibly expensive atomic operation.
//
ec.assign(0, ec.category());
ec = {};
return boost::none;
}
@ -373,7 +373,7 @@ get(error_code& ec) ->
// we set this bool to `false` so we will not be called
// again.
//
ec.assign(0, ec.category());
ec = {};
return {{
const_buffers_type{buf_, nread}, // buffer to return.
remain_ > 0 // `true` if there are more buffers.
@ -474,7 +474,7 @@ init(
// to indicate no error.
//
// We don't do anything fancy so set "no error"
ec.assign(0, ec.category());
ec = {};
}
// This will get called one or more times with body buffers
@ -505,7 +505,7 @@ put(ConstBufferSequence const& buffers, error_code& ec)
// Indicate success
// This is required by the error_code specification
ec.assign(0, ec.category());
ec = {};
return nwritten;
}
@ -519,7 +519,7 @@ finish(error_code& ec)
{
// This has to be cleared before returning, to
// indicate no error. The specification requires it.
ec.assign(0, ec.category());
ec = {};
}
//]

View File

@ -113,7 +113,7 @@ struct buffer_body
void
init(boost::optional<std::uint64_t> const&, error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
template<class ConstBufferSequence>
@ -135,7 +135,7 @@ struct buffer_body
body_.data) + bytes_transferred;
body_.size -= bytes_transferred;
if(bytes_transferred == buffer_size(buffers))
ec.assign(0, ec.category());
ec = {};
else
ec = error::need_buffer;
return bytes_transferred;
@ -144,7 +144,7 @@ struct buffer_body
void
finish(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
};
#endif
@ -175,7 +175,7 @@ struct buffer_body
void
init(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
boost::optional<
@ -191,13 +191,13 @@ struct buffer_body
}
else
{
ec.assign(0, ec.category());
ec = {};
}
return boost::none;
}
if(body_.data)
{
ec.assign(0, ec.category());
ec = {};
toggle_ = true;
return {{const_buffers_type{
body_.data, body_.size}, body_.more}};
@ -205,7 +205,7 @@ struct buffer_body
if(body_.more)
ec = error::need_buffer;
else
ec.assign(0, ec.category());
ec = {};
return boost::none;
}
};

View File

@ -191,14 +191,14 @@ struct basic_parser_base
{
if(it == last)
{
ec.assign(0, ec.category());
ec = {};
return nullptr;
}
if(*it == '\r')
{
if(++it == last)
{
ec.assign(0, ec.category());
ec = {};
return nullptr;
}
if(*it != '\n')
@ -206,7 +206,7 @@ struct basic_parser_base
ec = error::bad_line_ending;
return nullptr;
}
ec.assign(0, ec.category());
ec = {};
return ++it;
}
// VFALCO Should we handle the legacy case

View File

@ -70,7 +70,7 @@ struct empty_body
void
init(boost::optional<std::uint64_t> const&, error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
template<class ConstBufferSequence>
@ -85,7 +85,7 @@ struct empty_body
void
finish(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
};
#endif
@ -111,13 +111,13 @@ struct empty_body
void
init(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
boost::optional<std::pair<const_buffers_type, bool>>
get(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
return boost::none;
}
};

View File

@ -100,7 +100,7 @@ put(ConstBufferSequence const& buffers,
auto const last = net::buffer_sequence_end(buffers);
if(p == last)
{
ec.assign(0, ec.category());
ec = {};
return 0;
}
if(std::next(p) == last)
@ -136,7 +136,7 @@ put(net::const_buffer const& buffer,
auto n = buffer.size();
auto const p0 = p;
auto const p1 = p0 + n;
ec.assign(0, ec.category());
ec = {};
loop:
switch(state_)
{
@ -255,7 +255,7 @@ loop:
break;
case state::complete:
ec.assign(0, ec.category());
ec = {};
goto done;
}
if(p < p1 && ! is_done() && eager())
@ -286,7 +286,7 @@ put_eof(error_code& ec)
ec = error::partial_message;
return;
}
ec.assign(0, ec.category());
ec = {};
return;
}
impl().on_finish_impl(ec);
@ -841,7 +841,7 @@ do_field(field f,
continue;
}
}
ec.assign(0, ec.category());
ec = {};
return;
}
@ -870,7 +870,7 @@ do_field(field f,
return;
}
ec.assign(0, ec.category());
ec = {};
len_ = v;
f_ |= flagContentLength;
return;
@ -893,7 +893,7 @@ do_field(field f,
return;
}
ec.assign(0, ec.category());
ec = {};
auto const v = token_list{value};
auto const p = std::find_if(v.begin(), v.end(),
[&](typename token_list::value_type const& s)
@ -912,12 +912,12 @@ do_field(field f,
// Upgrade
if(f == field::upgrade)
{
ec.assign(0, ec.category());
ec = {};
f_ |= flagUpgrade;
return;
}
ec.assign(0, ec.category());
ec = {};
}
} // http

View File

@ -387,7 +387,7 @@ do_parse(FwdIt it, FwdIt last, error_code& ec)
loop:
if(it == last)
{
ec.assign(0, ec.category());
ec = {};
return it;
}
// BWS

View File

@ -142,7 +142,7 @@ struct basic_file_body<file_win32>
beast::detail::clamp(body_.last_ - pos_));
if(n == 0)
{
ec.assign(0, ec.category());
ec = {};
return boost::none;
}
auto const nread = body_.file_.read(buf_, n, ec);
@ -150,7 +150,7 @@ struct basic_file_body<file_win32>
return boost::none;
BOOST_ASSERT(nread != 0);
pos_ += nread;
ec.assign(0, ec.category());
ec = {};
return {{
{buf_, nread}, // buffer to return.
pos_ < body_.last_}}; // `true` if there are more buffers.
@ -179,7 +179,7 @@ struct basic_file_body<file_win32>
// VFALCO We could reserve space in the file
boost::ignore_unused(content_length);
BOOST_ASSERT(body_.file_.is_open());
ec.assign(0, ec.category());
ec = {};
}
template<class ConstBufferSequence>
@ -195,14 +195,14 @@ struct basic_file_body<file_win32>
if(ec)
return nwritten;
}
ec.assign(0, ec.category());
ec = {};
return nwritten;
}
void
finish(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
};
@ -496,7 +496,7 @@ write_some(
BOOST_ASSERT(w.pos_ <= w.body_.last_);
if(w.pos_ < w.body_.last_)
{
ec.assign(0, ec.category());
ec = {};
}
else
{

View File

@ -327,7 +327,7 @@ write_some_impl(
sr.consume(f.bytes_transferred);
return f.bytes_transferred;
}
ec.assign(0, ec.category());
ec = {};
return 0;
}
@ -475,7 +475,7 @@ write_header(
}
else
{
ec.assign(0, ec.category());
ec = {};
}
return bytes_transferred;
}
@ -764,7 +764,7 @@ public:
operator()(error_code& ec,
ConstBufferSequence const& buffers) const
{
ec.assign(0, ec.category());
ec = {};
if(os_.fail())
return;
std::size_t bytes_transferred = 0;

View File

@ -347,7 +347,7 @@ private:
m_.method(method);
else
m_.method_string(method_str);
ec.assign(0, ec.category());
ec = {};
}
catch(std::bad_alloc const&)
{
@ -368,7 +368,7 @@ private:
try
{
m_.reason(reason);
ec.assign(0, ec.category());
ec = {};
}
catch(std::bad_alloc const&)
{
@ -386,7 +386,7 @@ private:
try
{
m_.insert(name, name_string, value);
ec.assign(0, ec.category());
ec = {};
}
catch(std::bad_alloc const&)
{
@ -397,7 +397,7 @@ private:
void
on_header_impl(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
void
@ -426,7 +426,7 @@ private:
{
if(cb_h_)
return cb_h_(size, extensions, ec);
ec.assign(0, ec.category());
ec = {};
}
std::size_t

View File

@ -87,7 +87,7 @@ public:
ec = error::buffer_overflow;
return;
}
ec.assign(0, ec.category());
ec = {};
}
template<class ConstBufferSequence>
@ -104,7 +104,7 @@ public:
ec = error::buffer_overflow;
return 0;
}
ec.assign(0, ec.category());
ec = {};
buffer_copy(net::buffer(
body_.data(), n), buffers);
body_ = value_type{
@ -115,7 +115,7 @@ public:
void
finish(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
};
#endif
@ -145,13 +145,13 @@ public:
void
init(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
boost::optional<std::pair<const_buffers_type, bool>>
get(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
return {{
{ body_.data(),
body_.size() * sizeof(typename

View File

@ -109,7 +109,7 @@ public:
return;
}
}
ec.assign(0, ec.category());
ec = {};
}
template<class ConstBufferSequence>
@ -130,7 +130,7 @@ public:
ec = error::buffer_overflow;
return 0;
}
ec.assign(0, ec.category());
ec = {};
CharT* dest = &body_[size];
for(auto b : beast::buffers_range_ref(buffers))
{
@ -144,7 +144,7 @@ public:
void
finish(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
};
#endif
@ -174,13 +174,13 @@ public:
void
init(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
boost::optional<std::pair<const_buffers_type, bool>>
get(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
return {{const_buffers_type{
body_.data(), body_.size()}, false}};
}

View File

@ -103,7 +103,7 @@ public:
return;
}
}
ec.assign(0, ec.category());
ec = {};
}
template<class ConstBufferSequence>
@ -124,7 +124,7 @@ public:
ec = error::buffer_overflow;
return 0;
}
ec.assign(0, ec.category());
ec = {};
return buffer_copy(net::buffer(
&body_[0] + len, n), buffers);
}
@ -132,7 +132,7 @@ public:
void
finish(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
};
#endif
@ -162,13 +162,13 @@ public:
void
init(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
boost::optional<std::pair<const_buffers_type, bool>>
get(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
return {{const_buffers_type{
body_.data(), body_.size()}, false}};
}

View File

@ -259,7 +259,7 @@ read_close(
if(n == 0)
{
cr = close_reason{};
ec.assign(0, ec.category());
ec = {};
return;
}
if(n == 1)
@ -298,7 +298,7 @@ read_close(
{
cr.reason = "";
}
ec.assign(0, ec.category());
ec = {};
}
} // detail

View File

@ -3388,7 +3388,7 @@ private:
ec = net::error::operation_aborted;
return false;
}
ec.assign(0, ec.category());
ec = {};
return true;
}

View File

@ -1006,7 +1006,7 @@ doParams(z_params& zs, int level, Strategy strategy, error_code& ec)
// Flush the last buffer:
doWrite(zs, Flush::block, ec);
if(ec == error::need_buffers && pending_ == 0)
ec.assign(0, ec.category());
ec = {};
}
if(level_ != level)
{

View File

@ -97,7 +97,7 @@ public:
p.put(buffer(s.data(), n), ec);
s.remove_prefix(used);
if(ec == error::need_more)
ec.assign(0, ec.category());
ec = {};
if(! BEAST_EXPECTS(! ec, ec.message()))
continue;
BEAST_EXPECT(! p.is_done());
@ -258,7 +258,7 @@ public:
used = p.put(b.data(), ec);
b.consume(used);
BEAST_EXPECT(ec == error::need_more);
ec.assign(0, ec.category());
ec = {};
BEAST_EXPECT(! p.is_done());
ostream(b) <<
"\r\n"; // final crlf to end message
@ -302,7 +302,7 @@ public:
used = p.put(b.data(), ec);
BEAST_EXPECTS(ec == error::need_more, ec.message());
b.consume(used);
ec.assign(0, ec.category());
ec = {};
ostream(b) <<
"User-Agent: test\r\n"
"\r\n";
@ -323,7 +323,7 @@ public:
BEAST_EXPECT(ec == error::need_more);
BEAST_EXPECT(! p.got_some());
BEAST_EXPECT(used == 0);
ec.assign(0, ec.category());
ec = {};
used = p.put(buf("G"), ec);
BEAST_EXPECT(ec == error::need_more);
BEAST_EXPECT(p.got_some());

View File

@ -66,7 +66,7 @@ public:
if(fc_)
fc_->fail(ec);
else
ec.assign(0, ec.category());
ec = {};
}
void
@ -82,7 +82,7 @@ public:
if(fc_)
fc_->fail(ec);
else
ec.assign(0, ec.category());
ec = {};
}
void
@ -93,7 +93,7 @@ public:
if(fc_)
fc_->fail(ec);
else
ec.assign(0, ec.category());
ec = {};
fields[name.to_string()] = value.to_string();
}
@ -104,7 +104,7 @@ public:
if(fc_)
fc_->fail(ec);
else
ec.assign(0, ec.category());
ec = {};
}
void
@ -118,7 +118,7 @@ public:
if(fc_)
fc_->fail(ec);
else
ec.assign(0, ec.category());
ec = {};
}
std::size_t
@ -129,7 +129,7 @@ public:
if(fc_)
fc_->fail(ec);
else
ec.assign(0, ec.category());
ec = {};
return s.size();
}
@ -143,7 +143,7 @@ public:
if(fc_)
fc_->fail(ec);
else
ec.assign(0, ec.category());
ec = {};
}
std::size_t
@ -156,7 +156,7 @@ public:
if(fc_)
fc_->fail(ec);
else
ec.assign(0, ec.category());
ec = {};
return s.size();
}
@ -168,7 +168,7 @@ public:
if(fc_)
fc_->fail(ec);
else
ec.assign(0, ec.category());
ec = {};
}
};

View File

@ -60,13 +60,13 @@ public:
void
init(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
boost::optional<std::pair<const_buffers_type, bool>>
get(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
return {{const_buffers_type{
body_.data(), body_.size()}, false}};
}
@ -105,13 +105,13 @@ public:
void
init(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
boost::optional<std::pair<const_buffers_type, bool>>
get(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
body_.read = true;
return get(
std::integral_constant<bool, isSplit>{},
@ -916,13 +916,13 @@ public:
void
init(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
boost::optional<std::pair<const_buffers_type, bool>>
get(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
return {{const_buffers_type{"", 0}, false}};
}
};
@ -947,13 +947,13 @@ public:
void
init(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
boost::optional<std::pair<const_buffers_type, bool>>
get(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
return {{const_buffers_type{"", 0}, false}};
}
};

View File

@ -164,7 +164,7 @@ public:
on_request_impl(verb, string_view,
string_view, int, error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
void
@ -172,20 +172,20 @@ public:
string_view,
int, error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
void
on_field_impl(field,
string_view, string_view, error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
void
on_header_impl(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
void
@ -193,13 +193,13 @@ public:
boost::optional<std::uint64_t> const&,
error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
std::size_t
on_body_impl(string_view s, error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
return s.size();
}
@ -207,21 +207,21 @@ public:
on_chunk_header_impl(std::uint64_t,
string_view, error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
std::size_t
on_chunk_body_impl(std::uint64_t,
string_view s, error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
return s.size();
}
void
on_finish_impl(error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
};

View File

@ -572,7 +572,7 @@ private:
// Transfer-Encoding, see if we can reserve the buffer.
//
// r_.reserve(content_length)
ec.assign(0, ec.category());
ec = {};
}
bool
@ -625,7 +625,7 @@ private:
void
on_body(void const*, std::size_t, error_code& ec)
{
ec.assign(0, ec.category());
ec = {};
}
void

View File

@ -76,7 +76,7 @@ public:
init(error_code& ec)
{
// The specification requires this to indicate "no error"
ec.assign(0, ec.category());
ec = {};
}
/** Returns the next buffer in the body.
@ -101,7 +101,7 @@ public:
get(error_code& ec)
{
// The specification requires this to indicate "no error"
ec.assign(0, ec.category());
ec = {};
return boost::none; // for exposition only
}
@ -144,7 +144,7 @@ struct BodyReader
boost::ignore_unused(content_length);
// The specification requires this to indicate "no error"
ec.assign(0, ec.category());
ec = {};
}
/** Store buffers.

View File

@ -365,7 +365,7 @@ print_cxx14(message<isRequest, Body, Fields> const& m)
sr.next(ec,
[&sr](error_code& ec, auto const& buffer)
{
ec.assign(0, ec.category());
ec = {};
std::cout << buffers(buffer);
sr.consume(net::buffer_size(buffer));
});
@ -392,7 +392,7 @@ struct lambda
template<class ConstBufferSequence>
void operator()(error_code& ec, ConstBufferSequence const& buffer) const
{
ec.assign(0, ec.category());
ec = {};
std::cout << buffers(buffer);
sr.consume(net::buffer_size(buffer));
}
@ -433,7 +433,7 @@ split_print_cxx14(message<isRequest, Body, Fields> const& m)
sr.next(ec,
[&sr](error_code& ec, auto const& buffer)
{
ec.assign(0, ec.category());
ec = {};
std::cout << buffers(buffer);
sr.consume(net::buffer_size(buffer));
});
@ -447,7 +447,7 @@ split_print_cxx14(message<isRequest, Body, Fields> const& m)
sr.next(ec,
[&sr](error_code& ec, auto const& buffer)
{
ec.assign(0, ec.category());
ec = {};
std::cout << buffers(buffer);
sr.consume(net::buffer_size(buffer));
});