QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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  const auto constMMessageList = mMessageList;
70  for ( const QgsErrorMessage &m : constMMessageList )
71  {
72 #ifdef QGISDEBUG
73  QString file;
74 #ifndef _MSC_VER
75  int sPrefixLength = strlen( CMAKE_SOURCE_DIR ) + 1;
76  file = m.file().mid( sPrefixLength );
77 #else
78  file = m.file();
79 #endif
80 #endif
81 
82  if ( format == QgsErrorMessage::Text )
83  {
84  if ( !str.isEmpty() )
85  {
86  str += '\n'; // new message
87  }
88  if ( !m.tag().isEmpty() )
89  {
90  str += m.tag() + ' ';
91  }
92  str += m.message();
93 #ifdef QGISDEBUG
94  QString where;
95  if ( !file.isEmpty() )
96  {
97  where += QStringLiteral( "file: %1 row: %2" ).arg( file ).arg( m.line() );
98  }
99  if ( !m.function().isEmpty() )
100  {
101  where += QStringLiteral( "function %1:" ).arg( m.function() );
102  }
103  if ( !where.isEmpty() )
104  {
105  str += QStringLiteral( " (%1)" ).arg( where );
106  }
107 #endif
108  }
109  else // QgsErrorMessage::Html
110  {
111  str += "<p><b>" + m.tag() + ":</b> " + m.message();
112 #ifdef QGISDEBUG
113  QString location = QStringLiteral( "%1 : %2 : %3" ).arg( file ).arg( m.line() ).arg( m.function() );
114  if ( !srcUrl.isEmpty() )
115  {
116  QString url = QStringLiteral( "%1/%2#L%3" ).arg( srcUrl, file ).arg( m.line() );
117  str += QStringLiteral( "<br>(<a href='%1'>%2</a>)" ).arg( url, location );
118  }
119  else
120  {
121  str += QStringLiteral( "<br>(%1)" ).arg( location );
122  }
123 #endif
124  }
125  }
126  return str;
127 }
128 
129 QString QgsError::summary() const
130 {
131  // The first message in chain is usually the real error given by backend/server
132  return mMessageList.first().message();
133 }
QString message() const
Definition: qgserror.h:55
static const char * QGIS_DEV_VERSION
The development version.
Definition: qgis.h:57
Format
Format.
Definition: qgserror.h:36
QString summary() const
Short error description, usually the first error in chain, the real error.
Definition: qgserror.cpp:129
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
QString message(QgsErrorMessage::Format format=QgsErrorMessage::Html) const
Full error messages description.
Definition: qgserror.cpp:49
QString tag() const
Definition: qgserror.h:56
QgsErrorMessage()=default
Constructor for QgsErrorMessage.
QString file() const
Definition: qgserror.h:57