QGIS API Documentation  3.23.0-Master (eb871beae0)
qgsserverexception.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsservervexception.h
3  -------------------
4  begin : January 11, 2017
5  copyright : (C) 2017 David Marteau
6  email : david dot marteau at 3liz 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 
18 #include "qgsserverexception.h"
19 
20 #include <QDomDocument>
21 
22 // QgsServerException
23 QgsServerException::QgsServerException( const QString &message, int responseCode )
24  : QgsException( message )
25  , mResponseCode( responseCode )
26 {
27 
28 }
29 
30 QByteArray QgsServerException::formatResponse( QString &responseFormat ) const
31 {
32  QDomDocument doc;
33  QDomElement root = doc.createElement( QStringLiteral( "ServerException" ) );
34  doc.appendChild( root );
35  root.appendChild( doc.createTextNode( what() ) );
36 
37  responseFormat = QStringLiteral( "text/xml; charset=utf-8" );
38  return doc.toByteArray();
39 }
40 
41 
42 // QgsOgcServiceException
43 QgsOgcServiceException:: QgsOgcServiceException( const QString &code, const QString &message, const QString &locator,
44  int responseCode, const QString &version )
45  : QgsServerException( message, responseCode )
46  , mCode( code )
47  , mMessage( message )
48  , mLocator( locator )
49  , mVersion( version )
50 {
51 
52 }
53 
54 QByteArray QgsOgcServiceException::formatResponse( QString &responseFormat ) const
55 {
56  QDomDocument doc;
57  QDomElement root = doc.createElement( QStringLiteral( "ServiceExceptionReport" ) );
58  root.setAttribute( QStringLiteral( "version" ), mVersion );
59  root.setAttribute( QStringLiteral( "xmlns" ), QStringLiteral( "http://www.opengis.net/ogc" ) );
60  doc.appendChild( root );
61 
62  QDomElement elem = doc.createElement( QStringLiteral( "ServiceException" ) );
63  elem.setAttribute( QStringLiteral( "code" ), mCode );
64  elem.appendChild( doc.createTextNode( mMessage ) );
65  root.appendChild( elem );
66 
67  if ( ! mLocator.isEmpty() )
68  {
69  elem.setAttribute( QStringLiteral( "locator" ), mLocator );
70  }
71 
72  responseFormat = QStringLiteral( "text/xml; charset=utf-8" );
73  return doc.toByteArray();
74 }
75 
76 
77 
Defines a QGIS exception class.
Definition: qgsexception.h:35
QString what() const
Definition: qgsexception.h:48
QgsOgcServiceException(const QString &code, const QString &message, const QString &locator=QString(), int responseCode=200, const QString &version=QStringLiteral("1.3.0"))
Construction.
QByteArray formatResponse(QString &responseFormat) const override
Formats the exception for sending to client.
Exception base class for server exceptions.
virtual QByteArray formatResponse(QString &responseFormat) const
Formats the exception for sending to client.
QgsServerException(const QString &message, int responseCode=500)
Constructor.