QGIS API Documentation 3.99.0-Master (d270888f95f)
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#include <QString>
22
23using namespace Qt::StringLiterals;
24
25// QgsServerException
27 : QgsException( message )
28 , mResponseCode( responseCode )
29{
30}
31
32QByteArray QgsServerException::formatResponse( QString &responseFormat ) const
33{
34 QDomDocument doc;
35 const QDomNode header = doc.createProcessingInstruction( u"xml"_s, u"version=\"1.0\" encoding=\"UTF-8\""_s );
36 doc.appendChild( header );
37
38 QDomElement root = doc.createElement( u"ServerException"_s );
39 doc.appendChild( root );
40 root.appendChild( doc.createTextNode( what() ) );
41
42 responseFormat = u"text/xml; charset=utf-8"_s;
43 return doc.toByteArray();
44}
45
46
47// QgsOgcServiceException
48QgsOgcServiceException::QgsOgcServiceException( const QString &code, const QString &message, const QString &locator, int responseCode, const QString &version )
50 , mCode( code )
51 , mMessage( message )
52 , mLocator( locator )
53 , mVersion( version )
54{
55}
56
57QByteArray QgsOgcServiceException::formatResponse( QString &responseFormat ) const
58{
59 QDomDocument doc;
60 const QDomNode header = doc.createProcessingInstruction( u"xml"_s, u"version=\"1.0\" encoding=\"UTF-8\""_s );
61 doc.appendChild( header );
62
63 QDomElement root = doc.createElement( u"ServiceExceptionReport"_s );
64 root.setAttribute( u"version"_s, mVersion );
65 root.setAttribute( u"xmlns"_s, u"http://www.opengis.net/ogc"_s );
66 doc.appendChild( root );
67
68 QDomElement elem = doc.createElement( u"ServiceException"_s );
69 elem.setAttribute( u"code"_s, mCode );
70 elem.appendChild( doc.createTextNode( mMessage ) );
71 root.appendChild( elem );
72
73 if ( !mLocator.isEmpty() )
74 {
75 elem.setAttribute( u"locator"_s, mLocator );
76 }
77
78 responseFormat = u"text/xml; charset=utf-8"_s;
79 return doc.toByteArray();
80}
QString what() const
QgsException(const QString &message)
Constructor for QgsException, with the specified error message.
QString locator() const
Returns the locator.
QString message() const
Returns the exception message.
QString code() const
Returns the exception code.
QString version() const
Returns the exception version.
QByteArray formatResponse(QString &responseFormat) const override
Formats the exception for sending to client.
QgsOgcServiceException(const QString &code, const QString &message, const QString &locator=QString(), int responseCode=200, const QString &version=u"1.3.0"_s)
Construction.
int responseCode() const
Returns the return HTTP response code associated with this exception.
virtual QByteArray formatResponse(QString &responseFormat) const
Formats the exception for sending to client.
QgsServerException(const QString &message, int responseCode=500)
Constructor.