diff options
author | Jake Petroules <jake.petroules@theqtcompany.com> | 2016-03-23 17:37:49 -0700 |
---|---|---|
committer | Jake Petroules <jake.petroules@qt.io> | 2016-04-14 12:35:48 +0000 |
commit | 9eac02487ef5b8c94b08901b223b1d572642a68a (patch) | |
tree | ba51229d5779943841a697ede699d9b6617f709d /src/lib/corelib/tools/error.cpp | |
parent | a235900a7368fd2edf269454016c7577cb674323 (diff) |
Display proper file paths and line numbers for errors in more places.
Rule.outputArtifacts, Rule.prepare, and Artifact.filePath will now
display the correct file paths and line numbers for errors thrown from
those locations (including in imported JavaScript files).
Change-Id: I4e3c8e60f30791f5aa4de9e3813d4890c46c09fb
Reviewed-by: Christian Kandeler <christian.kandeler@theqtcompany.com>
Diffstat (limited to 'src/lib/corelib/tools/error.cpp')
-rw-r--r-- | src/lib/corelib/tools/error.cpp | 48 |
1 files changed, 45 insertions, 3 deletions
diff --git a/src/lib/corelib/tools/error.cpp b/src/lib/corelib/tools/error.cpp index d114fda7c..78fc5cdcd 100644 --- a/src/lib/corelib/tools/error.cpp +++ b/src/lib/corelib/tools/error.cpp @@ -30,6 +30,7 @@ #include "error.h" +#include <QRegularExpression> #include <QSharedData> #include <QStringList> @@ -40,6 +41,7 @@ class ErrorItem::ErrorItemPrivate : public QSharedData public: QString description; CodeLocation codeLocation; + bool isBacktraceItem = false; }; /*! @@ -53,11 +55,13 @@ ErrorItem::ErrorItem() : d(new ErrorItemPrivate) { } -ErrorItem::ErrorItem(const QString &description, const CodeLocation &codeLocation) +ErrorItem::ErrorItem(const QString &description, const CodeLocation &codeLocation, + bool isBacktraceItem) : d(new ErrorItemPrivate) { d->description = description; d->codeLocation = codeLocation; + d->isBacktraceItem = isBacktraceItem; } ErrorItem::ErrorItem(const ErrorItem &rhs) : d(rhs.d) @@ -84,6 +88,11 @@ CodeLocation ErrorItem::codeLocation() const return d->codeLocation; } +bool ErrorItem::isBacktraceItem() const +{ + return d->isBacktraceItem; +} + /*! * \fn const QString &ErrorData::description() const * \brief A general description of the error. @@ -138,6 +147,23 @@ ErrorInfo::ErrorInfo(const QString &description, const CodeLocation &location, b d->internalError = internalError; } +ErrorInfo::ErrorInfo(const QString &description, const QStringList &backtrace) + : d(new ErrorInfoPrivate) +{ + append(description); + for (const QString &traceLine : backtrace) { + QRegularExpression regexp( + QStringLiteral("^(?<message>.+) at (?<file>.+):(?<line>\\-?[0-9]+)$")); + QRegularExpressionMatch match = regexp.match(traceLine); + if (match.hasMatch()) { + const CodeLocation location(match.captured(QStringLiteral("file")), + match.captured(QStringLiteral("line")).toInt()); + appendBacktrace(match.captured(QStringLiteral("message")), location); + } + } +} + + ErrorInfo &ErrorInfo::operator =(const ErrorInfo &other) { d = other.d; @@ -148,6 +174,11 @@ ErrorInfo::~ErrorInfo() { } +void ErrorInfo::appendBacktrace(const QString &description, const CodeLocation &location) +{ + d->items.append(ErrorItem(description, location, true)); +} + void ErrorInfo::append(const QString &description, const CodeLocation &location) { d->items.append(ErrorItem(description, location)); @@ -181,8 +212,19 @@ void ErrorInfo::clear() QString ErrorInfo::toString() const { QStringList lines; - foreach (const ErrorItem &e, d->items) - lines.append(e.toString()); + foreach (const ErrorItem &e, d->items) { + if (e.isBacktraceItem()) { + QString line; + if (!e.description().isEmpty()) + line.append(QStringLiteral(" at %1").arg(e.description())); + if (e.codeLocation().isValid()) + line.append(QStringLiteral(" in %1").arg(e.codeLocation().toString())); + if (!line.isEmpty()) + lines.append(QStringLiteral("\t") + line); + } else { + lines.append(e.toString()); + } + } return lines.join(QLatin1Char('\n')); } |