Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlerror.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
5#include "qqmlerror.h"
6#include "qqmlfile.h"
7#include <private/qqmljsdiagnosticmessage_p.h>
8
9#include <QtCore/qdebug.h>
10#include <QtCore/qfile.h>
11#include <QtCore/qstringlist.h>
12#include <QtCore/qvector.h>
13
14#include <QtCore/qobject.h>
15#include <QtCore/qpointer.h>
16
18
19/*!
20 \class QQmlError
21 \since 5.0
22 \inmodule QtQml
23 \brief The QQmlError class encapsulates a QML error.
24
25 QQmlError includes a textual description of the error, as well
26 as location information (the file, line, and column). The toString()
27 method creates a single-line, human-readable string containing all of
28 this information, for example:
29 \code
30 file:///home/user/test.qml:7:8: Invalid property assignment: double expected
31 \endcode
32
33 You can use qDebug(), qInfo(), or qWarning() to output errors to the console.
34 This method will attempt to open the file indicated by the error
35 and include additional contextual information.
36 \code
37 file:///home/user/test.qml:7:8: Invalid property assignment: double expected
38 y: "hello"
39 ^
40 \endcode
41
42 \sa QQuickView::errors(), QQmlComponent::errors()
43*/
45{
46public:
47 QUrl url;
49 QString message;
51 int line = -1;
52 int column = -1;
53
54 friend bool operator==(const QQmlErrorPrivate &a, const QQmlErrorPrivate &b)
55 {
56 return a.url == b.url && a.object == b.object && a.message == b.message
57 && a.type == b.type && a.line == b.line && a.column == b.column;
58 }
59};
60
61/*!
62 Creates an empty error object.
63*/
64QQmlError::QQmlError()
65: d(nullptr)
66{
67}
68
69/*!
70 Creates a copy of \a other.
71*/
72QQmlError::QQmlError(const QQmlError &other)
73: d(nullptr)
74{
75 *this = other;
76}
77
78/*!
79 Assigns \a other to this error object.
80*/
81QQmlError &QQmlError::operator=(const QQmlError &other)
82{
83 if (!other.d) {
84 delete d;
85 d = nullptr;
86 } else {
87 if (!d)
88 d = new QQmlErrorPrivate;
89 d->url = other.d->url;
90 d->message = other.d->message;
91 d->line = other.d->line;
92 d->column = other.d->column;
93 d->object = other.d->object;
94 d->type = other.d->type;
95 }
96 return *this;
97}
98
99/*!
100 \internal
101*/
102QQmlError::~QQmlError()
103{
104 delete d; d = nullptr;
105}
106
107/*!
108 Returns true if this error is valid, otherwise false.
109*/
110bool QQmlError::isValid() const
111{
112 return d != nullptr;
113}
114
115/*!
116 Returns the url for the file that caused this error.
117*/
118QUrl QQmlError::url() const
119{
120 if (d)
121 return d->url;
122 return QUrl();
123}
124
125/*!
126 Sets the \a url for the file that caused this error.
127*/
128void QQmlError::setUrl(const QUrl &url)
129{
130 if (!d)
131 d = new QQmlErrorPrivate;
132 d->url = url;
133}
134
135/*!
136 Returns the error description.
137*/
138QString QQmlError::description() const
139{
140 if (d)
141 return d->message;
142 return QString();
143}
144
145/*!
146 Sets the error \a description.
147*/
148void QQmlError::setDescription(const QString &description)
149{
150 if (!d)
151 d = new QQmlErrorPrivate;
152 d->message = description;
153}
154
155/*!
156 Returns the error line number.
157*/
158int QQmlError::line() const
159{
160 if (d)
161 return d->line;
162 return -1;
163}
164
165/*!
166 Sets the error \a line number.
167*/
168void QQmlError::setLine(int line)
169{
170 if (!d)
171 d = new QQmlErrorPrivate;
172 d->line = line;
173}
174
175/*!
176 Returns the error column number.
177*/
178int QQmlError::column() const
179{
180 if (d)
181 return d->column;
182 return -1;
183}
184
185/*!
186 Sets the error \a column number.
187*/
188void QQmlError::setColumn(int column)
189{
190 if (!d)
191 d = new QQmlErrorPrivate;
192 d->column = column;
193}
194
195/*!
196 Returns the nearest object where this error occurred.
197 Exceptions in bound property expressions set this to the object
198 to which the property belongs. It will be 0 for all
199 other exceptions.
200 */
201QObject *QQmlError::object() const
202{
203 if (d)
204 return d->object;
205 return nullptr;
206}
207
208/*!
209 Sets the nearest \a object where this error occurred.
210 */
211void QQmlError::setObject(QObject *object)
212{
213 if (!d)
214 d = new QQmlErrorPrivate;
215 d->object = object;
216}
217
218/*!
219 \since 5.9
220
221 Returns the message type.
222 */
223QtMsgType QQmlError::messageType() const
224{
225 if (d)
226 return d->type;
227 return QtMsgType::QtWarningMsg;
228}
229
230/*!
231 \since 5.9
232
233 Sets the \a messageType for this message. The message type determines which
234 QDebug handlers are responsible for receiving the message.
235 */
236void QQmlError::setMessageType(QtMsgType messageType)
237{
238 if (!d)
239 d = new QQmlErrorPrivate;
240 d->type = messageType;
241}
242
243/*!
244 Returns the error as a human readable string.
245*/
246QString QQmlError::toString() const
247{
248 QString rv;
249
250 QUrl u(url());
251 int l(line());
252
253 if (u.isEmpty() || (u.isLocalFile() && u.path().isEmpty()))
254 rv += QLatin1String("<Unknown File>");
255 else
256 rv += u.toString();
257
258 if (l != -1) {
259 rv += QLatin1Char(':') + QString::number(l);
260
261 int c(column());
262 if (c != -1)
263 rv += QLatin1Char(':') + QString::number(c);
264 }
265
266 rv += QLatin1String(": ") + description();
267
268 return rv;
269}
270
271bool operator==(const QQmlError &a, const QQmlError &b)
272{
273 return a.d == b.d || (a.d && b.d && *a.d == *b.d);
274}
275
276/*!
277 \relates QQmlError
278 \fn QDebug operator<<(QDebug debug, const QQmlError &error)
279
280 Outputs a human readable version of \a error to \a debug.
281*/
282
283QDebug operator<<(QDebug debug, const QQmlError &error)
284{
285 debug << qPrintable(error.toString());
286
287 QUrl url = error.url();
288
289 if (error.line() > 0 && (url.scheme() == QLatin1String("file") || url.scheme() == QLatin1String("qrc"))) {
290 QString file = QQmlFile::urlToLocalFileOrQrc(url);
291 QFile f(file);
292 if (f.open(QIODevice::ReadOnly)) {
293 QByteArray data = f.readAll();
294 QTextStream stream(data, QIODevice::ReadOnly);
295 const QString code = stream.readAll();
296 const auto lines = QStringView{code}.split(QLatin1Char('\n'));
297
298 if (lines.size() >= error.line()) {
299 const QStringView &line = lines.at(error.line() - 1);
300 debug << "\n " << line.toLocal8Bit().constData();
301
302 if(error.column() > 0) {
303 int column = qMax(0, error.column() - 1);
304 column = qMin(column, line.size());
305
306 QByteArray ind;
307 ind.reserve(column);
308 for (int i = 0; i < column; ++i) {
309 const QChar ch = line.at(i);
310 if (ch.isSpace())
311 ind.append(ch.unicode());
312 else
313 ind.append(' ');
314 }
315 ind.append('^');
316 debug << "\n " << ind.constData();
317 }
318 }
319 }
320 }
321 return debug;
322}
323
324QT_END_NAMESPACE
friend bool operator==(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
Returns true if lhs and rhs are equal, otherwise returns false.
Definition qbytearray.h:807
friend bool operator==(const QQmlErrorPrivate &a, const QQmlErrorPrivate &b)
Definition qqmlerror.cpp:54
QPointer< QObject > object
Definition qqmlerror.cpp:48
Combined button and popup list for selecting options.