QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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
37QgsError::QgsError( const QString &message, const QString &tag )
38{
39 append( message, tag );
40}
41
42void QgsError::append( const QString &message, const QString &tag )
43{
44 mMessageList.append( QgsErrorMessage( message, tag ) );
45}
46
48{
49 mMessageList.append( message );
50}
51
53{
54 QString str;
55
56#ifdef QGISDEBUG
57 QString srcUrl;
58#endif
59
60#if defined( QGISDEBUG ) && defined( QGS_GIT_REMOTE_URL )
61 // TODO: verify if we are not ahead to origin (remote hash does not exist)
62 // and there are no local not committed changes
63 QString hash = QString( Qgis::devVersion() );
64 QString remote = QStringLiteral( QGS_GIT_REMOTE_URL );
65 if ( !hash.isEmpty() && !remote.isEmpty() && remote.contains( "github.com"_L1 ) )
66 {
67 QString path = remote.remove( QRegularExpression( ".*github.com[:/]" ) ).remove( u".git"_s );
68 srcUrl = "https://github.com/" + path + "/blob/" + hash;
69 }
70#endif
71
72 const auto constMMessageList = mMessageList;
73 for ( const QgsErrorMessage &m : constMMessageList )
74 {
75#ifdef QGISDEBUG
76 QString file;
77#ifndef _MSC_VER
78 const int sPrefixLength = strlen( CMAKE_SOURCE_DIR ) + 1;
79 file = m.file().mid( sPrefixLength );
80#else
81 file = m.file();
82#endif
83#endif
84
85 if ( format == QgsErrorMessage::Text )
86 {
87 if ( !str.isEmpty() )
88 {
89 str += '\n'; // new message
90 }
91 if ( !m.tag().isEmpty() )
92 {
93 str += m.tag() + ' ';
94 }
95 str += m.message();
96#ifdef QGISDEBUG
97 QString where;
98 if ( !file.isEmpty() )
99 {
100 where += u"file: %1 row: %2"_s.arg( file ).arg( m.line() );
101 }
102 if ( !m.function().isEmpty() )
103 {
104 where += u"function %1:"_s.arg( m.function() );
105 }
106 if ( !where.isEmpty() )
107 {
108 str += u" (%1)"_s.arg( where );
109 }
110#endif
111 }
112 else // QgsErrorMessage::Html
113 {
114 str += "<p><b>" + m.tag() + ":</b> " + m.message();
115#ifdef QGISDEBUG
116 const QString location = u"%1 : %2 : %3"_s.arg( file ).arg( m.line() ).arg( m.function() );
117 if ( !srcUrl.isEmpty() )
118 {
119 const QString url = u"%1/%2#L%3"_s.arg( srcUrl, file ).arg( m.line() );
120 str += u"<br>(<a href='%1'>%2</a>)"_s.arg( url, location );
121 }
122 else
123 {
124 str += u"<br>(%1)"_s.arg( location );
125 }
126#endif
127 }
128 }
129 return str;
130}
131
132QString QgsError::summary() const
133{
134 // The first message in chain is usually the real error given by backend/server
135 return ( mMessageList.isEmpty() ? QString() : mMessageList.first().message() );
136}
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:52
QString summary() const
Short error description, usually the first error in chain, the real error.
Definition qgserror.cpp:132
void append(const QString &message, const QString &tag)
Append new error message.
Definition qgserror.cpp:42