1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qdeclarativeerror.h"
44 #include <QtCore/qdebug.h>
45 #include <QtCore/qfile.h>
46 #include <QtCore/qstringlist.h>
51 \class QDeclarativeError
53 \brief The QDeclarativeError class encapsulates a QML error.
55 QDeclarativeError includes a textual description of the error, as well
56 as location information (the file, line, and column). The toString()
57 method creates a single-line, human-readable string containing all of
58 this information, for example:
60 file:///home/user/test.qml:7:8: Invalid property assignment: double expected
63 You can use qDebug() or qWarning() to output errors to the console. This method
64 will attempt to open the file indicated by the error
65 and include additional contextual information.
67 file:///home/user/test.qml:7:8: Invalid property assignment: double expected
72 \sa QDeclarativeView::errors(), QDeclarativeComponent::errors()
74 class QDeclarativeErrorPrivate
77 QDeclarativeErrorPrivate();
85 QDeclarativeErrorPrivate::QDeclarativeErrorPrivate()
86 : line(-1), column(-1)
91 Creates an empty error object.
93 QDeclarativeError::QDeclarativeError()
99 Creates a copy of \a other.
101 QDeclarativeError::QDeclarativeError(const QDeclarativeError &other)
108 Assigns \a other to this error object.
110 QDeclarativeError &QDeclarativeError::operator=(const QDeclarativeError &other)
116 if (!d) d = new QDeclarativeErrorPrivate;
117 d->url = other.d->url;
118 d->description = other.d->description;
119 d->line = other.d->line;
120 d->column = other.d->column;
128 QDeclarativeError::~QDeclarativeError()
134 Returns true if this error is valid, otherwise false.
136 bool QDeclarativeError::isValid() const
142 Returns the url for the file that caused this error.
144 QUrl QDeclarativeError::url() const
146 if (d) return d->url;
151 Sets the \a url for the file that caused this error.
153 void QDeclarativeError::setUrl(const QUrl &url)
155 if (!d) d = new QDeclarativeErrorPrivate;
160 Returns the error description.
162 QString QDeclarativeError::description() const
164 if (d) return d->description;
165 else return QString();
169 Sets the error \a description.
171 void QDeclarativeError::setDescription(const QString &description)
173 if (!d) d = new QDeclarativeErrorPrivate;
174 d->description = description;
178 Returns the error line number.
180 int QDeclarativeError::line() const
182 if (d) return d->line;
187 Sets the error \a line number.
189 void QDeclarativeError::setLine(int line)
191 if (!d) d = new QDeclarativeErrorPrivate;
196 Returns the error column number.
198 int QDeclarativeError::column() const
200 if (d) return d->column;
205 Sets the error \a column number.
207 void QDeclarativeError::setColumn(int column)
209 if (!d) d = new QDeclarativeErrorPrivate;
214 Returns the error as a human readable string.
216 QString QDeclarativeError::toString() const
219 if (url().isEmpty()) {
220 rv = QLatin1String("<Unknown File>");
221 } else if (line() != -1) {
222 rv = url().toString() + QLatin1Char(':') + QString::number(line());
224 rv += QLatin1Char(':') + QString::number(column());
226 rv = url().toString();
229 rv += QLatin1String(": ") + description();
235 \relates QDeclarativeError
236 \fn QDebug operator<<(QDebug debug, const QDeclarativeError &error)
238 Outputs a human readable version of \a error to \a debug.
241 QDebug operator<<(QDebug debug, const QDeclarativeError &error)
243 debug << qPrintable(error.toString());
245 QUrl url = error.url();
247 if (error.line() > 0 && url.scheme() == QLatin1String("file")) {
248 QString file = url.toLocalFile();
250 if (f.open(QIODevice::ReadOnly)) {
251 QByteArray data = f.readAll();
252 QTextStream stream(data, QIODevice::ReadOnly);
253 #ifndef QT_NO_TEXTCODEC
254 stream.setCodec("UTF-8");
256 const QString code = stream.readAll();
257 const QStringList lines = code.split(QLatin1Char('\n'));
259 if (lines.count() >= error.line()) {
260 const QString &line = lines.at(error.line() - 1);
261 debug << "\n " << qPrintable(line);
263 if(error.column() > 0) {
264 int column = qMax(0, error.column() - 1);
265 column = qMin(column, line.length());
269 for (int i = 0; i < column; ++i) {
270 const QChar ch = line.at(i);
272 ind.append(ch.unicode());
277 debug << "\n " << ind.constData();