Skip to content

Commit

Permalink
Improve error reporting
Browse files Browse the repository at this point in the history
-JSON syntax errors say they are syntax errors, to help people understand how to resolve them.
-The line and character offset of JSON errors has been made explicit, instead of requiring you to guess what the numbers mean.
-The C++ source file names/etc were made more explicit as well, which should hopefully reduce people asking "I can't find this .cpp file on my game, where is it"
  • Loading branch information
RenechCDDA committed Jan 27, 2025
1 parent d3be85f commit a4eeeb0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
10 changes: 5 additions & 5 deletions src/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,11 @@ static void debug_error_prompt(

std::string formatted_report =
string_format( // developer-facing error report. INTENTIONALLY UNTRANSLATED!
" DEBUG : %s\n\n"
" FUNCTION : %s\n"
" FILE : %s\n"
" LINE : %s\n"
" VERSION : %s\n",
" DEBUG : %s\n\n"
" REPORTING FUNCTION : %s\n"
" C++ SOURCE FILE : %s\n"
" LINE : %s\n"
" VERSION : %s\n",
text, funcname, filename, line, getVersionString()
);

Expand Down
28 changes: 14 additions & 14 deletions src/flexbuffer_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,23 +143,23 @@ std::string Json::str() const
bool JsonValue::read( bool &b, bool throw_on_error ) const
{
if( !test_bool() ) {
return error_or_false( throw_on_error, "Expected bool" );
return error_or_false( throw_on_error, "Syntax error. Expected bool" );
}
b = get_bool();
return true;
}
bool JsonValue::read( char &c, bool throw_on_error ) const
{
if( !test_number() ) {
return error_or_false( throw_on_error, "Expected number" );
return error_or_false( throw_on_error, "Syntax error. Expected number" );
}
c = get_int();
return true;
}
bool JsonValue::read( signed char &c, bool throw_on_error ) const
{
if( !test_number() ) {
return error_or_false( throw_on_error, "Expected number" );
return error_or_false( throw_on_error, "Syntax error. Expected number" );
}
// TODO: test for overflow
c = get_int();
Expand All @@ -168,7 +168,7 @@ bool JsonValue::read( signed char &c, bool throw_on_error ) const
bool JsonValue::read( unsigned char &c, bool throw_on_error ) const
{
if( !test_number() ) {
return error_or_false( throw_on_error, "Expected number" );
return error_or_false( throw_on_error, "Syntax error. Expected number" );
}
// TODO: test for overflow
c = get_int();
Expand All @@ -177,7 +177,7 @@ bool JsonValue::read( unsigned char &c, bool throw_on_error ) const
bool JsonValue::read( short unsigned int &s, bool throw_on_error ) const
{
if( !test_number() ) {
return error_or_false( throw_on_error, "Expected number" );
return error_or_false( throw_on_error, "Syntax error. Expected number" );
}
// TODO: test for overflow
s = get_int();
Expand All @@ -186,7 +186,7 @@ bool JsonValue::read( short unsigned int &s, bool throw_on_error ) const
bool JsonValue::read( short int &s, bool throw_on_error ) const
{
if( !test_number() ) {
return error_or_false( throw_on_error, "Expected number" );
return error_or_false( throw_on_error, "Syntax error. Expected number" );
}
// TODO: test for overflow
s = get_int();
Expand All @@ -195,55 +195,55 @@ bool JsonValue::read( short int &s, bool throw_on_error ) const
bool JsonValue::read( int &i, bool throw_on_error ) const
{
if( !test_number() ) {
return error_or_false( throw_on_error, "Expected number" );
return error_or_false( throw_on_error, "Syntax error. Expected number" );
}
i = get_int();
return true;
}
bool JsonValue::read( int64_t &i, bool throw_on_error ) const
{
if( !test_number() ) {
return error_or_false( throw_on_error, "Expected number" );
return error_or_false( throw_on_error, "Syntax error. Expected number" );
}
i = get_int64();
return true;
}
bool JsonValue::read( uint64_t &i, bool throw_on_error ) const
{
if( !test_number() ) {
return error_or_false( throw_on_error, "Expected number" );
return error_or_false( throw_on_error, "Syntax error. Expected number" );
}
i = get_uint64();
return true;
}
bool JsonValue::read( unsigned int &u, bool throw_on_error ) const
{
if( !test_number() ) {
return error_or_false( throw_on_error, "Expected number" );
return error_or_false( throw_on_error, "Syntax error. Expected number" );
}
u = get_uint();
return true;
}
bool JsonValue::read( float &f, bool throw_on_error ) const
{
if( !test_number() ) {
return error_or_false( throw_on_error, "Expected number" );
return error_or_false( throw_on_error, "Syntax error. Expected number" );
}
f = get_float();
return true;
}
bool JsonValue::read( double &d, bool throw_on_error ) const
{
if( !test_number() ) {
return error_or_false( throw_on_error, "Expected number" );
return error_or_false( throw_on_error, "Syntax error. Expected number" );
}
d = get_float();
return true;
}
bool JsonValue::read( std::string &s, bool throw_on_error ) const
{
if( !test_string() ) {
return error_or_false( throw_on_error, "Expected string" );
return error_or_false( throw_on_error, "Syntax error. Expected string" );
}
s = get_string();
return true;
Expand Down Expand Up @@ -340,7 +340,7 @@ void JsonObject::error_skipped_members( const std::vector<size_t> &skipped_membe
"\"//~\" should be within a text object and contain comments for translators." );
} else {
jo.throw_error_at( name.c_str(),
string_format( "Invalid or misplaced field name \"%s\" in JSON data",
string_format( "Unread data. Invalid or misplaced field name \"%s\" in JSON data",
name.c_str() ) );
}
} catch( const JsonError &e ) {
Expand Down
12 changes: 8 additions & 4 deletions src/generic_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -565,9 +565,11 @@ inline void mandatory( const JsonObject &jo, const bool was_loaded, const std::s
if( !jo.read( name, member ) ) {
if( !was_loaded ) {
if( jo.has_member( name ) ) {
jo.throw_error( str_cat( "failed to read mandatory member \"", name, "\"" ) );
jo.throw_error( str_cat( "object beginning at this line failed to read mandatory member \"", name,
"\"" ) );
} else {
jo.throw_error( str_cat( "missing mandatory member \"", name, "\"" ) );
jo.throw_error( str_cat( "object beginning at this line missing mandatory member \"", name,
"\"" ) );
}
}
}
Expand All @@ -579,9 +581,11 @@ inline void mandatory( const JsonObject &jo, const bool was_loaded, const std::s
if( !reader( jo, name, member, was_loaded ) ) {
if( !was_loaded ) {
if( jo.has_member( name ) ) {
jo.throw_error( str_cat( "failed to read mandatory member \"", name, "\"" ) );
jo.throw_error( str_cat( "object beginning at this line failed to read mandatory member \"", name,
"\"" ) );
} else {
jo.throw_error( str_cat( "missing mandatory member \"", name, "\"" ) );
jo.throw_error( str_cat( "object beginning at this line missing mandatory member \"", name,
"\"" ) );
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1889,7 +1889,7 @@ std::string TextJsonIn::line_number( int offset_modifier )
std::stringstream ret;
switch( error_log_format ) {
case error_log_format_t::human_readable:
ret << name << ":" << line << ":" << offset;
ret << "file " << name << ", at line " << line << ", character " << offset;
break;
case error_log_format_t::github_action:
ret.imbue( std::locale::classic() );
Expand Down Expand Up @@ -2012,7 +2012,7 @@ void TextJsonIn::error( int offset, const std::string &message )
std::ostringstream err_header;
switch( error_log_format ) {
case error_log_format_t::human_readable:
err_header << "Json error: " << line_number( offset ) << ": ";
err_header << "Json error: " << line_number( offset ) << ":\n";
break;
case error_log_format_t::github_action:
err_header << "::error " << line_number( offset ) << "::";
Expand Down

0 comments on commit a4eeeb0

Please sign in to comment.