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