YAML: refactor YAML-exception reporting

- both NetInstall (group data) and Locale (GeoIP) use network
   data returned as a source of YAML data. Try to explain
   parsing errors for both.

FIXES #786
This commit is contained in:
Adriaan de Groot 2017-09-06 07:51:22 -04:00 committed by Philip
parent c85ecce1e7
commit 362b5f44a2
4 changed files with 77 additions and 47 deletions

View File

@ -18,8 +18,11 @@
*/
#include "YamlUtils.h"
#include "utils/Logger.h"
#include <yaml-cpp/yaml.h>
#include <QByteArray>
#include <QRegExp>
void
@ -105,4 +108,42 @@ yamlMapToVariant( const YAML::Node& mapNode )
}
void
explainYamlException( const YAML::Exception& e, const QByteArray& yamlData, const char *label )
{
cDebug() << "WARNING: YAML error " << e.what() << "in" << label << '.';
if ( ( e.mark.line >= 0 ) && ( e.mark.column >= 0 ) )
{
// Try to show the line where it happened.
int linestart = 0;
for ( int linecount = 0; linecount < e.mark.line; ++linecount )
{
linestart = yamlData.indexOf( '\n', linestart );
// No more \ns found, weird
if ( linestart < 0 )
break;
linestart += 1; // Skip that \n
}
int lineend = linestart;
if ( linestart >= 0 )
{
lineend = yamlData.indexOf( '\n', linestart );
if ( lineend < 0 )
lineend = yamlData.length();
}
int rangestart = linestart;
int rangeend = lineend;
// Adjust range (linestart..lineend) so it's not too long
if ( ( linestart >= 0 ) && ( e.mark.column > 30 ) )
rangestart += ( e.mark.column - 30 );
if ( ( linestart >= 0 ) && ( rangeend - rangestart > 40 ) )
rangeend = rangestart + 40;
if ( linestart >= 0 )
cDebug() << "WARNING: offending YAML data:" << yamlData.mid( rangestart, rangeend-rangestart ).constData();
}
}
} // namespace

View File

@ -1,6 +1,7 @@
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Copyright 2014, Teo Mrnjavac <teo@kde.org>
* Copyright 2017, Adriaan de Groot <groot@kde.org>
*
* Calamares is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -22,9 +23,12 @@
#include <QStringList>
#include <QVariant>
class QByteArray;
namespace YAML
{
class Node;
class Exception;
}
void operator>>( const YAML::Node& node, QStringList& v );
@ -37,6 +41,13 @@ QVariant yamlScalarToVariant( const YAML::Node& scalarNode );
QVariant yamlSequenceToVariant( const YAML::Node& sequenceNode );
QVariant yamlMapToVariant( const YAML::Node& mapNode );
/**
* Given an exception from the YAML parser library, explain
* what is going on in terms of the data passed to the parser.
* Uses @p label when labeling the data source (e.g. "netinstall data")
*/
void explainYamlException( const YAML::Exception& e, const QByteArray& data, const char *label );
} //ns
#endif // YAMLUTILS_H

View File

@ -115,6 +115,10 @@ LocaleViewStep::fetchGeoIpTimezone()
[=]( QNetworkReply* reply )
{
if ( reply->error() == QNetworkReply::NoError )
{
QByteArray data = reply->readAll();
try
{
YAML::Node doc = YAML::Load( reply->readAll() );
@ -139,6 +143,11 @@ LocaleViewStep::fetchGeoIpTimezone()
}
}
}
catch ( YAML::Exception& e )
{
CalamaresUtils::explainYamlException( e, data, "GeoIP data");
}
}
reply->deleteLater();
manager->deleteLater();

View File

@ -83,38 +83,7 @@ NetInstallPage::readGroups( const QByteArray& yamlData )
}
catch ( YAML::Exception& e )
{
cDebug() << "WARNING: YAML error " << e.what() << "in netinstall groups data.";
if ( ( e.mark.line >= 0 ) && ( e.mark.column >= 0 ) )
{
// Try to show the line where it happened.
int linestart = 0;
for ( int linecount = 0; linecount < e.mark.line; ++linecount )
{
linestart = yamlData.indexOf( '\n', linestart );
// No more \ns found, weird
if ( linestart < 0 )
break;
linestart += 1; // Skip that \n
}
int lineend = linestart;
if ( linestart >= 0 )
{
lineend = yamlData.indexOf( '\n', linestart );
if ( lineend < 0 )
lineend = yamlData.length();
}
int rangestart = linestart;
int rangeend = lineend;
// Adjust range (linestart..lineend) so it's not too long
if ( ( linestart >= 0 ) && ( e.mark.column > 30 ) )
rangestart += ( e.mark.column - 30 );
if ( ( linestart >= 0 ) && ( rangeend - rangestart > 40 ) )
rangeend = rangestart + 40;
if ( linestart >= 0 )
cDebug() << "WARNING: offending YAML data:" << yamlData.mid( rangestart, rangeend-rangestart ).constData();
}
CalamaresUtils::explainYamlException( e, yamlData, "netinstall groups data" );
return false;
}
}