Add file and line number to thrown exceptions

This commit is contained in:
Vinnie Falco
2016-11-02 16:02:43 -04:00
parent e66327ed4d
commit cfde28c3c3
12 changed files with 80 additions and 34 deletions

View File

@@ -253,7 +253,8 @@ private:
reference
dereference(C<sizeof...(Bn)> const&) const
{
throw std::logic_error("invalid iterator");
throw detail::make_exception<std::logic_error>(
"invalid iterator", __FILE__, __LINE__);
}
template<std::size_t I>
@@ -269,7 +270,8 @@ private:
void
increment(C<sizeof...(Bn)> const&)
{
throw std::logic_error("invalid iterator");
throw detail::make_exception<std::logic_error>(
"invalid iterator", __FILE__, __LINE__);
}
template<std::size_t I>
@@ -310,7 +312,8 @@ private:
--iter<I>();
return;
}
throw std::logic_error("invalid iterator");
throw detail::make_exception<std::logic_error>(
"invalid iterator", __FILE__, __LINE__);
}
template<std::size_t I>

View File

@@ -10,6 +10,8 @@
#include <tuple>
#include <type_traits>
#include <stdexcept>
#include <string>
namespace beast {
namespace detail {
@@ -78,6 +80,18 @@ struct repeat_tuple<0, T>
using type = std::tuple<>;
};
template<class Exception>
Exception
make_exception(char const* reason, char const* file, int line)
{
char const* n = file;
for(auto p = file; *p; ++p)
if(*p == '\\' || *p == '/')
n = p + 1;
return Exception{std::string(reason) + " (" +
n + ":" + std::to_string(line) + ")"};
}
} // detail
} // beast

View File

@@ -524,8 +524,8 @@ basic_streambuf<Allocator>::basic_streambuf(
, alloc_size_(alloc_size)
{
if(alloc_size <= 0)
throw std::invalid_argument(
"basic_streambuf: invalid alloc_size");
throw detail::make_exception<std::invalid_argument>(
"invalid alloc_size", __FILE__, __LINE__);
}
template<class Allocator>

View File

@@ -8,6 +8,7 @@
#ifndef BEAST_IMPL_BUFFERS_ADAPTER_IPP
#define BEAST_IMPL_BUFFERS_ADAPTER_IPP
#include <beast/core/detail/type_traits.hpp>
#include <boost/asio/buffer.hpp>
#include <algorithm>
#include <cstring>
@@ -413,8 +414,8 @@ buffers_adapter<MutableBufferSequence>::prepare(std::size_t n) ->
}
}
if(n > 0)
throw std::length_error(
"no space in buffers_adapter");
throw detail::make_exception<std::length_error>(
"no space", __FILE__, __LINE__);
return mutable_buffers_type{*this};
}

View File

@@ -8,6 +8,7 @@
#ifndef BEAST_IMPL_STATIC_STREAMBUF_IPP
#define BEAST_IMPL_STATIC_STREAMBUF_IPP
#include <beast/core/detail/type_traits.hpp>
#include <boost/asio/buffer.hpp>
#include <algorithm>
#include <cstring>
@@ -295,7 +296,8 @@ static_streambuf::prepare(std::size_t n) ->
mutable_buffers_type
{
if(n > static_cast<std::size_t>(end_ - out_))
throw std::length_error("no space in streambuf");
throw detail::make_exception<std::length_error>(
"no space in streambuf", __FILE__, __LINE__);
last_ = out_ + n;
return mutable_buffers_type{out_, n};
}

View File

@@ -8,6 +8,7 @@
#ifndef BEAST_WEBSOCKET_STATIC_STRING_HPP
#define BEAST_WEBSOCKET_STATIC_STRING_HPP
#include <beast/core/detail/type_traits.hpp>
#include <array>
#include <cstdint>
#include <iterator>
@@ -329,7 +330,8 @@ static_string<N, CharT, Traits>::
static_string(static_string<M, CharT, Traits> const& s)
{
if(s.size() > N)
throw std::length_error("static_string overflow");
throw detail::make_exception<std::length_error>(
"static_string overflow", __FILE__, __LINE__);
n_ = s.size();
Traits::copy(&s_[0], &s.s_[0], n_ + 1);
}
@@ -353,7 +355,8 @@ operator=(static_string<M, CharT, Traits> const& s) ->
static_string&
{
if(s.size() > N)
throw std::length_error("static_string overflow");
throw detail::make_exception<std::length_error>(
"static_string overflow", __FILE__, __LINE__);
n_ = s.size();
Traits::copy(&s_[0], &s.s_[0], n_ + 1);
return *this;
@@ -391,7 +394,8 @@ at(size_type pos) ->
reference
{
if(pos >= n_)
throw std::out_of_range("static_string::at");
throw detail::make_exception<std::out_of_range>(
"invalid pos", __FILE__, __LINE__);
return s_[pos];
}
@@ -402,7 +406,8 @@ at(size_type pos) const ->
const_reference
{
if(pos >= n_)
throw std::out_of_range("static_string::at");
throw detail::make_exception<std::out_of_range>(
"static_string::at", __FILE__, __LINE__);
return s_[pos];
}
@@ -412,7 +417,8 @@ static_string<N, CharT, Traits>::
resize(std::size_t n)
{
if(n > N)
throw std::length_error("static_string overflow");
throw detail::make_exception<std::length_error>(
"static_string overflow", __FILE__, __LINE__);
n_ = n;
s_[n_] = 0;
}
@@ -423,7 +429,8 @@ static_string<N, CharT, Traits>::
resize(std::size_t n, CharT c)
{
if(n > N)
throw std::length_error("static_string overflow");
throw detail::make_exception<std::length_error>(
"static_string overflow", __FILE__, __LINE__);
if(n > n_)
Traits::assign(&s_[n_], n - n_, c);
n_ = n;
@@ -462,7 +469,8 @@ assign(CharT const* s)
{
auto const n = Traits::length(s);
if(n > N)
throw std::out_of_range("too large");
throw detail::make_exception<std::out_of_range>(
"too large", __FILE__, __LINE__);
n_ = n;
Traits::copy(&s_[0], s, n_ + 1);
}

View File

@@ -159,6 +159,8 @@ void
prepare(message<isRequest, Body, Fields>& msg,
Options&&... options)
{
using beast::detail::make_exception;
// VFALCO TODO
static_assert(is_Body<Body>::value,
"Body requirements not met");
@@ -174,16 +176,16 @@ prepare(message<isRequest, Body, Fields>& msg,
std::forward<Options>(options)...);
if(msg.fields.exists("Connection"))
throw std::invalid_argument(
"prepare called with Connection field set");
throw make_exception<std::invalid_argument>(
"prepare called with Connection field set", __FILE__, __LINE__);
if(msg.fields.exists("Content-Length"))
throw std::invalid_argument(
"prepare called with Content-Length field set");
throw make_exception<std::invalid_argument>(
"prepare called with Content-Length field set", __FILE__, __LINE__);
if(token_list{msg.fields["Transfer-Encoding"]}.exists("chunked"))
throw std::invalid_argument(
"prepare called with Transfer-Encoding: chunked set");
throw make_exception<std::invalid_argument>(
"prepare called with Transfer-Encoding: chunked set", __FILE__, __LINE__);
if(pi.connection_value != connection::upgrade)
{
@@ -254,8 +256,8 @@ prepare(message<isRequest, Body, Fields>& msg,
// rfc7230 6.7.
if(msg.version < 11 && token_list{
msg.fields["Connection"]}.exists("upgrade"))
throw std::invalid_argument(
"invalid version for Connection: upgrade");
throw make_exception<std::invalid_argument>(
"invalid version for Connection: upgrade", __FILE__, __LINE__);
}
} // http

View File

@@ -20,6 +20,7 @@
#include <beast/core/prepare_buffers.hpp>
#include <beast/core/static_streambuf.hpp>
#include <beast/core/stream_concepts.hpp>
#include <beast/core/detail/type_traits.hpp>
#include <boost/assert.hpp>
#include <boost/endian/buffers.hpp>
#include <algorithm>

View File

@@ -10,6 +10,7 @@
#include <beast/websocket/rfc6455.hpp>
#include <beast/websocket/detail/decorator.hpp>
#include <beast/core/detail/type_traits.hpp>
#include <algorithm>
#include <cstdint>
#include <functional>
@@ -186,7 +187,8 @@ struct message_type
message_type(opcode op)
{
if(op != opcode::binary && op != opcode::text)
throw std::domain_error("bad opcode");
throw beast::detail::make_exception<std::invalid_argument>(
"bad opcode", __FILE__, __LINE__);
value = op;
}
};
@@ -277,6 +279,9 @@ struct read_buffer_size
read_buffer_size(std::size_t n)
: value(n)
{
if(n < 8)
throw beast::detail::make_exception<std::invalid_argument>(
"read buffer size is too small", __FILE__, __LINE__);
}
};
#endif
@@ -356,7 +361,8 @@ struct write_buffer_size
: value(n)
{
if(n < 8)
throw std::domain_error("write buffer size is too small");
throw beast::detail::make_exception<std::invalid_argument>(
"write buffer size is too small", __FILE__, __LINE__);
}
};
#endif

View File

@@ -37,6 +37,7 @@
#include <beast/zlib/zlib.hpp>
#include <beast/zlib/detail/ranges.hpp>
#include <beast/core/detail/type_traits.hpp>
#include <boost/assert.hpp>
#include <boost/optional.hpp>
#include <cstdint>
@@ -893,14 +894,19 @@ doReset(
if(windowBits == 8)
windowBits = 9;
using beast::detail::make_exception;
if(level < 0 || level > 9)
throw std::invalid_argument{"invalid level"};
throw make_exception<std::invalid_argument>(
"invalid level", __FILE__, __LINE__);
if(windowBits < 8 || windowBits > 15)
throw std::invalid_argument{"invalid windowBits"};
throw make_exception<std::invalid_argument>(
"invalid windowBits", __FILE__, __LINE__);
if(memLevel < 1 || memLevel > MAX_MEM_LEVEL)
throw std::invalid_argument{"invalid memLevel"};
throw make_exception<std::invalid_argument>(
"invalid memLevel", __FILE__, __LINE__);
w_bits_ = windowBits;

View File

@@ -40,6 +40,7 @@
#include <beast/zlib/detail/bitstream.hpp>
#include <beast/zlib/detail/ranges.hpp>
#include <beast/zlib/detail/window.hpp>
#include <beast/core/detail/type_traits.hpp>
#include <algorithm>
#include <array>
#include <cstdint>
@@ -232,7 +233,8 @@ inflate_stream::
doReset(int windowBits)
{
if(windowBits < 8 || windowBits > 15)
throw std::domain_error("windowBits out of range");
throw beast::detail::make_exception<std::domain_error>(
"windowBits out of range", __FILE__, __LINE__);
w_.reset(windowBits);
bi_.flush();
@@ -706,7 +708,8 @@ doWrite(z_params& zs, Flush flush, error_code& ec)
case SYNC:
default:
throw std::logic_error("stream error");
throw beast::detail::make_exception<std::logic_error>(
"stream error", __FILE__, __LINE__);
}
}
}
@@ -932,8 +935,8 @@ inflate_table(
auto const not_enough = []
{
throw std::logic_error(
"insufficient output size when inflating tables");
throw beast::detail::make_exception<std::logic_error>(
"insufficient output size when inflating tables", __FILE__, __LINE__);
};
// check available table space

View File

@@ -160,8 +160,8 @@ private:
auto& d = *d_;
d.ws.set_option(decorate(identity{}));
d.ws.set_option(read_message_max(64 * 1024 * 1024));
d.ws.set_option(auto_fragment{false});
d.ws.set_option(write_buffer_size{64 * 1024});
//d.ws.set_option(auto_fragment{false});
//d.ws.set_option(write_buffer_size{64 * 1024});
run();
}