QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
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
23QgsServerException::QgsServerException( const QString &message, int responseCode )
24 : QgsException( message )
25 , mResponseCode( responseCode )
26{
27}
28
29QByteArray QgsServerException::formatResponse( QString &responseFormat ) const
30{
31 QDomDocument doc;
32 const QDomNode header = doc.createProcessingInstruction( QStringLiteral( "xml" ), QStringLiteral( "version=\"1.0\" encoding=\"UTF-8\"" ) );
33 doc.appendChild( header );
34
35 QDomElement root = doc.createElement( QStringLiteral( "ServerException" ) );
36 doc.appendChild( root );
37 root.appendChild( doc.createTextNode( what() ) );
38
39 responseFormat = QStringLiteral( "text/xml; charset=utf-8" );
40 return doc.toByteArray();
41}
42
43
44// QgsOgcServiceException
45QgsOgcServiceException::QgsOgcServiceException( const QString &code, const QString &message, const QString &locator, int responseCode, const QString &version )
46 : QgsServerException( message, responseCode )
47 , mCode( code )
48 , mMessage( message )
49 , mLocator( locator )
50 , mVersion( version )
51{
52}
53
54QByteArray QgsOgcServiceException::formatResponse( QString &responseFormat ) const
55{
56 QDomDocument doc;
57 const QDomNode header = doc.createProcessingInstruction( QStringLiteral( "xml" ), QStringLiteral( "version=\"1.0\" encoding=\"UTF-8\"" ) );
58 doc.appendChild( header );
59
60 QDomElement root = doc.createElement( QStringLiteral( "ServiceExceptionReport" ) );
61 root.setAttribute( QStringLiteral( "version" ), mVersion );
62 root.setAttribute( QStringLiteral( "xmlns" ), QStringLiteral( "http://www.opengis.net/ogc" ) );
63 doc.appendChild( root );
64
65 QDomElement elem = doc.createElement( QStringLiteral( "ServiceException" ) );
66 elem.setAttribute( QStringLiteral( "code" ), mCode );
67 elem.appendChild( doc.createTextNode( mMessage ) );
68 root.appendChild( elem );
69
70 if ( !mLocator.isEmpty() )
71 {
72 elem.setAttribute( QStringLiteral( "locator" ), mLocator );
73 }
74
75 responseFormat = QStringLiteral( "text/xml; charset=utf-8" );
76 return doc.toByteArray();
77}
Defines a QGIS exception class.
QString what() const
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.