1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qqmlerror.h"
44 #include <QtCore/qdebug.h>
45 #include <QtCore/qfile.h>
46 #include <QtCore/qstringlist.h>
54 \brief The QQmlError class encapsulates a QML error.
56 QQmlError includes a textual description of the error, as well
57 as location information (the file, line, and column). The toString()
58 method creates a single-line, human-readable string containing all of
59 this information, for example:
61 file:///home/user/test.qml:7:8: Invalid property assignment: double expected
64 You can use qDebug() or qWarning() to output errors to the console. This method
65 will attempt to open the file indicated by the error
66 and include additional contextual information.
68 file:///home/user/test.qml:7:8: Invalid property assignment: double expected
73 Note that the QtQuick 1 version is named QDeclarativeError
75 \sa QQuickView::errors(), QQmlComponent::errors()
77 class QQmlErrorPrivate
88 QQmlErrorPrivate::QQmlErrorPrivate()
89 : line(-1), column(-1)
94 Creates an empty error object.
96 QQmlError::QQmlError()
102 Creates a copy of \a other.
104 QQmlError::QQmlError(const QQmlError &other)
111 Assigns \a other to this error object.
113 QQmlError &QQmlError::operator=(const QQmlError &other)
119 if (!d) d = new QQmlErrorPrivate;
120 d->url = other.d->url;
121 d->description = other.d->description;
122 d->line = other.d->line;
123 d->column = other.d->column;
131 QQmlError::~QQmlError()
137 Returns true if this error is valid, otherwise false.
139 bool QQmlError::isValid() const
145 Returns the url for the file that caused this error.
147 QUrl QQmlError::url() const
149 if (d) return d->url;
154 Sets the \a url for the file that caused this error.
156 void QQmlError::setUrl(const QUrl &url)
158 if (!d) d = new QQmlErrorPrivate;
163 Returns the error description.
165 QString QQmlError::description() const
167 if (d) return d->description;
168 else return QString();
172 Sets the error \a description.
174 void QQmlError::setDescription(const QString &description)
176 if (!d) d = new QQmlErrorPrivate;
177 d->description = description;
181 Returns the error line number.
183 int QQmlError::line() const
185 if (d) return d->line;
190 Sets the error \a line number.
192 void QQmlError::setLine(int line)
194 if (!d) d = new QQmlErrorPrivate;
199 Returns the error column number.
201 int QQmlError::column() const
203 if (d) return d->column;
208 Sets the error \a column number.
210 void QQmlError::setColumn(int column)
212 if (!d) d = new QQmlErrorPrivate;
217 Returns the error as a human readable string.
219 QString QQmlError::toString() const
222 if (url().isEmpty()) {
223 rv = QLatin1String("<Unknown File>");
224 } else if (line() != -1) {
225 rv = url().toString() + QLatin1Char(':') + QString::number(line());
227 rv += QLatin1Char(':') + QString::number(column());
229 rv = url().toString();
232 rv += QLatin1String(": ") + description();
239 \fn QDebug operator<<(QDebug debug, const QQmlError &error)
241 Outputs a human readable version of \a error to \a debug.
244 QDebug operator<<(QDebug debug, const QQmlError &error)
246 debug << qPrintable(error.toString());
248 QUrl url = error.url();
250 if (error.line() > 0 && url.scheme() == QLatin1String("file")) {
251 QString file = url.toLocalFile();
253 if (f.open(QIODevice::ReadOnly)) {
254 QByteArray data = f.readAll();
255 QTextStream stream(data, QIODevice::ReadOnly);
256 #ifndef QT_NO_TEXTCODEC
257 stream.setCodec("UTF-8");
259 const QString code = stream.readAll();
260 const QStringList lines = code.split(QLatin1Char('\n'));
262 if (lines.count() >= error.line()) {
263 const QString &line = lines.at(error.line() - 1);
264 debug << "\n " << qPrintable(line);
266 if(error.column() > 0) {
267 int column = qMax(0, error.column() - 1);
268 column = qMin(column, line.length());
272 for (int i = 0; i < column; ++i) {
273 const QChar ch = line.at(i);
275 ind.append(ch.unicode());
280 debug << "\n " << ind.constData();