diff --git a/src/libcalamaresui/utils/YamlUtils.cpp b/src/libcalamaresui/utils/YamlUtils.cpp index 1eb759963..4b1a8fd86 100644 --- a/src/libcalamaresui/utils/YamlUtils.cpp +++ b/src/libcalamaresui/utils/YamlUtils.cpp @@ -18,8 +18,11 @@ */ #include "YamlUtils.h" +#include "utils/Logger.h" + #include +#include #include 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 diff --git a/src/libcalamaresui/utils/YamlUtils.h b/src/libcalamaresui/utils/YamlUtils.h index a39934f51..1da36178c 100644 --- a/src/libcalamaresui/utils/YamlUtils.h +++ b/src/libcalamaresui/utils/YamlUtils.h @@ -1,6 +1,7 @@ /* === This file is part of Calamares - === * * Copyright 2014, Teo Mrnjavac + * Copyright 2017, Adriaan de Groot * * 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 #include +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 diff --git a/src/modules/locale/LocaleViewStep.cpp b/src/modules/locale/LocaleViewStep.cpp index 24719b1be..8f38e7f14 100644 --- a/src/modules/locale/LocaleViewStep.cpp +++ b/src/modules/locale/LocaleViewStep.cpp @@ -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(); diff --git a/src/modules/netinstall/NetInstallPage.cpp b/src/modules/netinstall/NetInstallPage.cpp index fe5b600ab..7bfda320c 100644 --- a/src/modules/netinstall/NetInstallPage.cpp +++ b/src/modules/netinstall/NetInstallPage.cpp @@ -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; } }