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