Skip to content

Commit 043a41c

Browse files
committed
Improve exception handling.
1 parent 7f37826 commit 043a41c

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

include/tao/json/internal/action.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ namespace tao
138138
static void apply( const Input& in, number_state& result )
139139
{
140140
if( in.size() > ( 1 << 20 ) ) {
141-
throw json_pegtl::parse_error( "JSON number with 1 megabyte digits", in );
141+
throw std::runtime_error( "JSON number with 1 megabyte digits" );
142142
}
143143
const auto c = std::min( in.size(), max_mantissa_digits );
144144
std::memcpy( result.mantissa, in.begin(), c );
@@ -202,7 +202,7 @@ namespace tao
202202
++b;
203203
}
204204
if( ( in.end() - b ) > 9 ) {
205-
throw json_pegtl::parse_error( "JSON exponent has more than 9 significant digits", in );
205+
throw std::runtime_error( "JSON exponent has more than 9 significant digits" );
206206
}
207207
int exponent10 = 0;
208208

include/tao/json/internal/control.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ namespace tao
2626
};
2727

2828
template<>
29-
struct control< rules::number > : json_pegtl::change_state< rules::number, number_state, json_pegtl::normal >
29+
struct control< rules::number > : json_pegtl::change_state< rules::number, number_state, errors >
3030
{
3131
};
3232

3333
template<>
34-
struct control< rules::string::content > : json_pegtl::change_state_and_action< rules::string::content, string_state, unescape_action, json_pegtl::normal >
34+
struct control< rules::string::content > : json_pegtl::change_state_and_action< rules::string::content, string_state, unescape_action, errors >
3535
{
3636
};
3737

3838
template<>
39-
struct control< rules::key::content > : json_pegtl::change_state_and_action< rules::key::content, key_state, unescape_action, json_pegtl::normal >
39+
struct control< rules::key::content > : json_pegtl::change_state_and_action< rules::key::content, key_state, unescape_action, errors >
4040
{
4141
};
4242

include/tao/json/internal/errors.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,36 @@ namespace tao
3636
{
3737
throw json_pegtl::parse_error( error_message, in );
3838
}
39+
40+
template< template< typename... > class Action, typename Input, typename... States >
41+
static void apply0( const Input& in, States&&... st )
42+
{
43+
try {
44+
Action< Rule >::apply0( st... );
45+
}
46+
catch( const json_pegtl::parse_error& ) {
47+
throw;
48+
}
49+
catch( const std::exception& e ) {
50+
throw json_pegtl::parse_error( e.what(), in );
51+
}
52+
}
53+
54+
template< template< typename... > class Action, typename Iterator, typename Input, typename... States >
55+
static void apply( const Iterator begin, const Iterator end, const Input& in, States&&... st )
56+
{
57+
try {
58+
using action_t = typename Input::action_t;
59+
const action_t action_input( begin, end, in.source() );
60+
Action< Rule >::apply( action_input, st... );
61+
}
62+
catch( const json_pegtl::parse_error& ) {
63+
throw;
64+
}
65+
catch( const std::exception& e ) {
66+
throw json_pegtl::parse_error( e.what(), in );
67+
}
68+
}
3969
};
4070

4171
// clang-format off

0 commit comments

Comments
 (0)