QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgserror.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgserror.cpp - Error container
3 -------------------
4 begin : October 2012
5 copyright : (C) 2012 Radim Blazek
6 email : radim dot blazek at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17#include "qgsconfig.h"
18#include "qgserror.h"
19
20#include "qgis.h"
21#include "qgslogger.h"
22#include "qgsversion.h"
23
24#include <QRegularExpression>
25#include <QString>
26
27using namespace Qt::StringLiterals;
28
29QgsErrorMessage::QgsErrorMessage( const QString &message, const QString &tag, const QString &file, const QString &function, int line )
30 : mMessage( message )
31 , mTag( tag )
32 , mFile( file )
33 , mFunction( function )
34 , mLine( line )
35{
36}
37
38QgsError::QgsError( const QString &message, const QString &tag )
39{
40 append( message, tag );
41}
42
43void QgsError::append( const QString &message, const QString &tag )
44{
45 mMessageList.append( QgsErrorMessage( message, tag ) );
46}
47
49{
50 mMessageList.append( message );
51}
52
54{
55 QString str;
56
57#ifdef QGISDEBUG
58 QString srcUrl;
59#endif
60
61#if defined(QGISDEBUG) && defined(QGS_GIT_REMOTE_URL)
62 // TODO: verify if we are not ahead to origin (remote hash does not exist)
63 // and there are no local not committed changes
64 QString hash = QString( Qgis::devVersion() );
65 QString remote = QStringLiteral( QGS_GIT_REMOTE_URL );
66 if ( !hash.isEmpty() && !remote.isEmpty() && remote.contains( "github.com"_L1 ) )
67 {
68 QString path = remote.remove( QRegularExpression( ".*github.com[:/]" ) ).remove( u".git"_s );
69 srcUrl = "https://github.com/" + path + "/blob/" + hash;
70 }
71#endif
72
73 const auto constMMessageList = mMessageList;
74 for ( const QgsErrorMessage &m : constMMessageList )
75 {
76#ifdef QGISDEBUG
77 QString file;
78#ifndef _MSC_VER
79 const int sPrefixLength = strlen( CMAKE_SOURCE_DIR ) + 1;
80 file = m.file().mid( sPrefixLength );
81#else
82 file = m.file();
83#endif
84#endif
85
86 if ( format == QgsErrorMessage::Text )
87 {
88 if ( !str.isEmpty() )
89 {
90 str += '\n'; // new message
91 }
92 if ( !m.tag().isEmpty() )
93 {
94 str += m.tag() + ' ';
95 }
96 str += m.message();
97#ifdef QGISDEBUG
98 QString where;
99 if ( !file.isEmpty() )
100 {
101 where += u"file: %1 row: %2"_s.arg( file ).arg( m.line() );
102 }
103 if ( !m.function().isEmpty() )
104 {
105 where += u"function %1:"_s.arg( m.function() );
106 }
107 if ( !where.isEmpty() )
108 {
109 str += u" (%1)"_s.arg( where );
110 }
111#endif
112 }
113 else // QgsErrorMessage::Html
114 {
115 str += "<p><b>" + m.tag() + ":</b> " + m.message();
116#ifdef QGISDEBUG
117 const QString location = u"%1 : %2 : %3"_s.arg( file ).arg( m.line() ).arg( m.function() );
118 if ( !srcUrl.isEmpty() )
119 {
120 const QString url = u"%1/%2#L%3"_s.arg( srcUrl, file ).arg( m.line() );
121 str += u"<br>(<a href='%1'>%2</a>)"_s.arg( url, location );
122 }
123 else
124 {
125 str += u"<br>(%1)"_s.arg( location );
126 }
127#endif
128 }
129 }
130 return str;
131}
132
133QString QgsError::summary() const
134{
135 // The first message in chain is usually the real error given by backend/server
136 return ( mMessageList.isEmpty() ? QString() : mMessageList.first().message() );
137}
static QString devVersion()
The development version.
Definition qgis.cpp:699
Represents a single error message.
Definition qgserror.h:35
QString tag() const
Definition qgserror.h:57
QString file() const
Definition qgserror.h:58
QString function() const
Definition qgserror.h:59
int line() const
Definition qgserror.h:60
QgsErrorMessage()=default
Format
Format.
Definition qgserror.h:39
QString message() const
Definition qgserror.h:56
QgsError()=default
QString message(QgsErrorMessage::Format format=QgsErrorMessage::Html) const
Full error messages description.
Definition qgserror.cpp:53
QString summary() const
Short error description, usually the first error in chain, the real error.
Definition qgserror.cpp:133
void append(const QString &message, const QString &tag)
Append new error message.
Definition qgserror.cpp:43