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"
43 #include "qqmlglobal_p.h"
45 #include <QtCore/qdebug.h>
46 #include <QtCore/qfile.h>
47 #include <QtCore/qstringlist.h>
55 \brief The QQmlError class encapsulates a QML error.
57 QQmlError includes a textual description of the error, as well
58 as location information (the file, line, and column). The toString()
59 method creates a single-line, human-readable string containing all of
60 this information, for example:
62 file:///home/user/test.qml:7:8: Invalid property assignment: double expected
65 You can use qDebug() or qWarning() to output errors to the console. This method
66 will attempt to open the file indicated by the error
67 and include additional contextual information.
69 file:///home/user/test.qml:7:8: Invalid property assignment: double expected
74 Note that the QtQuick 1 version is named QDeclarativeError
76 \sa QQuickView::errors(), QQmlComponent::errors()
78 class QQmlErrorPrivate
89 QQmlErrorPrivate::QQmlErrorPrivate()
95 Creates an empty error object.
97 QQmlError::QQmlError()
103 Creates a copy of \a other.
105 QQmlError::QQmlError(const QQmlError &other)
112 Assigns \a other to this error object.
114 QQmlError &QQmlError::operator=(const QQmlError &other)
120 if (!d) d = new QQmlErrorPrivate;
121 d->url = other.d->url;
122 d->description = other.d->description;
123 d->line = other.d->line;
124 d->column = other.d->column;
132 QQmlError::~QQmlError()
138 Returns true if this error is valid, otherwise false.
140 bool QQmlError::isValid() const
146 Returns the url for the file that caused this error.
148 QUrl QQmlError::url() const
150 if (d) return d->url;
155 Sets the \a url for the file that caused this error.
157 void QQmlError::setUrl(const QUrl &url)
159 if (!d) d = new QQmlErrorPrivate;
164 Returns the error description.
166 QString QQmlError::description() const
168 if (d) return d->description;
169 else return QString();
173 Sets the error \a description.
175 void QQmlError::setDescription(const QString &description)
177 if (!d) d = new QQmlErrorPrivate;
178 d->description = description;
182 Returns the error line number.
184 int QQmlError::line() const
186 if (d) return qmlSourceCoordinate(d->line);
191 Sets the error \a line number.
193 void QQmlError::setLine(int line)
195 if (!d) d = new QQmlErrorPrivate;
196 d->line = qmlSourceCoordinate(line);
200 Returns the error column number.
202 int QQmlError::column() const
204 if (d) return qmlSourceCoordinate(d->column);
209 Sets the error \a column number.
211 void QQmlError::setColumn(int column)
213 if (!d) d = new QQmlErrorPrivate;
214 d->column = qmlSourceCoordinate(column);
218 Returns the error as a human readable string.
220 QString QQmlError::toString() const
228 rv = QLatin1String("<Unknown File>");
229 } else if (l != -1) {
230 rv = u.toString() + QLatin1Char(':') + QString::number(l);
234 rv += QLatin1Char(':') + QString::number(c);
239 rv += QLatin1String(": ") + description();
246 \fn QDebug operator<<(QDebug debug, const QQmlError &error)
248 Outputs a human readable version of \a error to \a debug.
251 QDebug operator<<(QDebug debug, const QQmlError &error)
253 debug << qPrintable(error.toString());
255 QUrl url = error.url();
257 if (error.line() > 0 && url.scheme() == QLatin1String("file")) {
258 QString file = url.toLocalFile();
260 if (f.open(QIODevice::ReadOnly)) {
261 QByteArray data = f.readAll();
262 QTextStream stream(data, QIODevice::ReadOnly);
263 #ifndef QT_NO_TEXTCODEC
264 stream.setCodec("UTF-8");
266 const QString code = stream.readAll();
267 const QStringList lines = code.split(QLatin1Char('\n'));
269 if (lines.count() >= error.line()) {
270 const QString &line = lines.at(error.line() - 1);
271 debug << "\n " << qPrintable(line);
273 if(error.column() > 0) {
274 int column = qMax(0, error.column() - 1);
275 column = qMin(column, line.length());
279 for (int i = 0; i < column; ++i) {
280 const QChar ch = line.at(i);
282 ind.append(ch.unicode());
287 debug << "\n " << ind.constData();