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
main
Adriaan de Groot 7 years ago
parent 09decf8e06
commit 7e25909e18

@ -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

@ -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

@ -116,28 +116,37 @@ LocaleViewStep::fetchGeoIpTimezone()
{
if ( reply->error() == QNetworkReply::NoError )
{
YAML::Node doc = YAML::Load( reply->readAll() );
QByteArray data = reply->readAll();
QVariant var = CalamaresUtils::yamlToVariant( doc );
if ( !var.isNull() &&
var.isValid() &&
var.type() == QVariant::Map )
try
{
QVariantMap map = var.toMap();
if ( map.contains( "time_zone" ) &&
!map.value( "time_zone" ).toString().isEmpty() )
YAML::Node doc = YAML::Load( reply->readAll() );
QVariant var = CalamaresUtils::yamlToVariant( doc );
if ( !var.isNull() &&
var.isValid() &&
var.type() == QVariant::Map )
{
QString timezoneString = map.value( "time_zone" ).toString();
QStringList timezone = timezoneString.split( '/', QString::SkipEmptyParts );
if ( timezone.size() >= 2 )
QVariantMap map = var.toMap();
if ( map.contains( "time_zone" ) &&
!map.value( "time_zone" ).toString().isEmpty() )
{
cDebug() << "GeoIP reporting" << timezoneString;
QString region = timezone.takeFirst();
QString zone = timezone.join( '/' );
m_startingTimezone = qMakePair( region, zone );
QString timezoneString = map.value( "time_zone" ).toString();
QStringList timezone = timezoneString.split( '/', QString::SkipEmptyParts );
if ( timezone.size() >= 2 )
{
cDebug() << "GeoIP reporting" << timezoneString;
QString region = timezone.takeFirst();
QString zone = timezone.join( '/' );
m_startingTimezone = qMakePair( region, zone );
}
}
}
}
catch ( YAML::Exception& e )
{
CalamaresUtils::explainYamlException( e, data, "GeoIP data");
}
}
reply->deleteLater();

@ -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;
}
}

Loading…
Cancel
Save