Add file and line number to thrown exceptions

This commit is contained in:
Vinnie Falco
2017-07-20 08:12:14 -07:00
parent e66327ed4d
commit cfde28c3c3
12 changed files with 80 additions and 34 deletions
+6 -3
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>
+14
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
+2 -2
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>
+3 -2
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};
}
+3 -1
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};
}
+15 -7
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);
}