qmlerror.cpp 6.8 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29 30 31

#include "qmlerror.h"

32

33 34 35
#include <QtCore/qdebug.h>
#include <QtCore/qfile.h>
#include <QtCore/qstringlist.h>
36 37 38 39 40

QT_BEGIN_NAMESPACE

/*!
    \class QmlError
41 42
    \since 5.0
    \inmodule QtQml
Roberto Raggi's avatar
Roberto Raggi committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    \brief The QmlError class encapsulates a QML error.

    QmlError includes a textual description of the error, as well
    as location information (the file, line, and column). The toString()
    method creates a single-line, human-readable string containing all of
    this information, for example:
    \code
    file:///home/user/test.qml:7:8: Invalid property assignment: double expected
    \endcode

    You can use qDebug() or qWarning() to output errors to the console. This method
    will attempt to open the file indicated by the error
    and include additional contextual information.
    \code
    file:///home/user/test.qml:7:8: Invalid property assignment: double expected
            y: "hello"
               ^
    \endcode

Leena Miettinen's avatar
Leena Miettinen committed
62
    \note The QtQuick 1 version is named QDeclarativeError.
63 64

    \sa QQuickView::errors(), QmlComponent::errors()
65
*/
66 67 68 69 70 71

static quint16 qmlSourceCoordinate(int n)
{
    return (n > 0 && n <= static_cast<int>(USHRT_MAX)) ? static_cast<quint16>(n) : 0;
}

72 73 74 75 76 77 78
class QmlErrorPrivate
{
public:
    QmlErrorPrivate();

    QUrl url;
    QString description;
79 80
    quint16 line;
    quint16 column;
81 82 83
};

QmlErrorPrivate::QmlErrorPrivate()
84
: line(0), column(0)
85 86 87 88
{
}

/*!
Roberto Raggi's avatar
Roberto Raggi committed
89
    Creates an empty error object.
90 91 92 93 94 95 96
*/
QmlError::QmlError()
: d(0)
{
}

/*!
Roberto Raggi's avatar
Roberto Raggi committed
97
    Creates a copy of \a other.
98 99 100 101 102 103 104 105
*/
QmlError::QmlError(const QmlError &other)
: d(0)
{
    *this = other;
}

/*!
Roberto Raggi's avatar
Roberto Raggi committed
106
    Assigns \a other to this error object.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
*/
QmlError &QmlError::operator=(const QmlError &other)
{
    if (!other.d) {
        delete d;
        d = 0;
    } else {
        if (!d) d = new QmlErrorPrivate;
        d->url = other.d->url;
        d->description = other.d->description;
        d->line = other.d->line;
        d->column = other.d->column;
    }
    return *this;
}

/*!
124
    \internal 
125 126 127 128 129 130 131
*/
QmlError::~QmlError()
{
    delete d; d = 0;
}

/*!
Roberto Raggi's avatar
Roberto Raggi committed
132
    Returns true if this error is valid, otherwise false.
133 134 135 136 137 138 139
*/
bool QmlError::isValid() const
{
    return d != 0;
}

/*!
Leena Miettinen's avatar
Leena Miettinen committed
140
    Returns the URL for the file that caused this error.
141 142 143 144 145 146 147 148
*/
QUrl QmlError::url() const
{
    if (d) return d->url;
    else return QUrl();
}

/*!
Roberto Raggi's avatar
Roberto Raggi committed
149
    Sets the \a url for the file that caused this error.
150 151 152 153 154 155 156 157
*/
void QmlError::setUrl(const QUrl &url)
{
    if (!d) d = new QmlErrorPrivate;
    d->url = url;
}

/*!
Roberto Raggi's avatar
Roberto Raggi committed
158
    Returns the error description.
159 160 161 162 163 164 165 166
*/
QString QmlError::description() const
{
    if (d) return d->description;
    else return QString();
}

/*!
Roberto Raggi's avatar
Roberto Raggi committed
167
    Sets the error \a description.
168 169 170 171 172 173 174 175
*/
void QmlError::setDescription(const QString &description)
{
    if (!d) d = new QmlErrorPrivate;
    d->description = description;
}

/*!
Roberto Raggi's avatar
Roberto Raggi committed
176
    Returns the error line number.
177 178 179
*/
int QmlError::line() const
{
180
    if (d) return qmlSourceCoordinate(d->line);
181 182 183 184
    else return -1;
}

/*!
Roberto Raggi's avatar
Roberto Raggi committed
185
    Sets the error \a line number.
186 187 188 189
*/
void QmlError::setLine(int line)
{
    if (!d) d = new QmlErrorPrivate;
190
    d->line = qmlSourceCoordinate(line);
191 192 193
}

/*!
Roberto Raggi's avatar
Roberto Raggi committed
194
    Returns the error column number.
195 196 197
*/
int QmlError::column() const
{
198
    if (d) return qmlSourceCoordinate(d->column);
199 200 201 202
    else return -1;
}

/*!
Roberto Raggi's avatar
Roberto Raggi committed
203
    Sets the error \a column number.
204 205 206 207
*/
void QmlError::setColumn(int column)
{
    if (!d) d = new QmlErrorPrivate;
208
    d->column = qmlSourceCoordinate(column);
209 210 211
}

/*!
Roberto Raggi's avatar
Roberto Raggi committed
212
    Returns the error as a human readable string.
213 214 215 216
*/
QString QmlError::toString() const
{
    QString rv;
217 218 219 220 221

    QUrl u(url());
    int l(line());

    if (u.isEmpty()) {
Roberto Raggi's avatar
Roberto Raggi committed
222
        rv = QLatin1String("<Unknown File>");
223 224 225 226 227 228
    } else if (l != -1) {
        rv = u.toString() + QLatin1Char(':') + QString::number(l);

        int c(column());
        if (c != -1)
            rv += QLatin1Char(':') + QString::number(c);
Roberto Raggi's avatar
Roberto Raggi committed
229
    } else {
230
        rv = u.toString();
Roberto Raggi's avatar
Roberto Raggi committed
231
    }
232 233 234 235 236 237 238 239 240

    rv += QLatin1String(": ") + description();

    return rv;
}

/*!
    \relates QmlError

Roberto Raggi's avatar
Roberto Raggi committed
241
    Outputs a human readable version of \a error to \a debug.
242 243 244 245 246 247 248 249 250 251 252 253 254 255
*/

QDebug operator<<(QDebug debug, const QmlError &error)
{
    debug << qPrintable(error.toString());

    QUrl url = error.url();

    if (error.line() > 0 && url.scheme() == QLatin1String("file")) {
        QString file = url.toLocalFile();
        QFile f(file);
        if (f.open(QIODevice::ReadOnly)) {
            QByteArray data = f.readAll();
            QTextStream stream(data, QIODevice::ReadOnly);
Roberto Raggi's avatar
Roberto Raggi committed
256
#ifndef QT_NO_TEXTCODEC
257
            stream.setCodec("UTF-8");
Roberto Raggi's avatar
Roberto Raggi committed
258
#endif
259 260 261 262 263 264 265
            const QString code = stream.readAll();
            const QStringList lines = code.split(QLatin1Char('\n'));

            if (lines.count() >= error.line()) {
                const QString &line = lines.at(error.line() - 1);
                debug << "\n    " << qPrintable(line);

266
                if(error.column() > 0) {
267
                    int column = qMax(0, error.column() - 1);
268
                    column = qMin(column, line.length()); 
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288

                    QByteArray ind;
                    ind.reserve(column);
                    for (int i = 0; i < column; ++i) {
                        const QChar ch = line.at(i);
                        if (ch.isSpace())
                            ind.append(ch.unicode());
                        else
                            ind.append(' ');
                    }
                    ind.append('^');
                    debug << "\n    " << ind.constData();
                }
            }
        }
    }
    return debug;
}

QT_END_NAMESPACE