QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsserverexception.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgserverexception.h
3  ------------------------
4  begin : January 11, 2017
5  copyright : (C) 2017 by 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 #ifndef QGSSERVEREXCEPTION_H
19 #define QGSSERVEREXCEPTION_H
20 
21 
22 #include <QString>
23 #include <QByteArray>
24 
25 #include "qgsexception.h"
26 #include "qgis_server.h"
27 #include "qgis_sip.h"
28 #include "nlohmann/json.hpp"
29 
30 #ifndef SIP_RUN
31 using namespace nlohmann;
32 #endif
33 
34 
41 #ifndef SIP_RUN
42 class SERVER_EXPORT QgsServerException : public QgsException
43 {
44 #else
45 class SERVER_EXPORT QgsServerException
46 {
47 #endif
48  public:
50  QgsServerException( const QString &message, int responseCode = 500 );
51 
55  int responseCode() const { return mResponseCode; }
56 
65  virtual QByteArray formatResponse( QString &responseFormat SIP_OUT ) const;
66 
67  private:
68  int mResponseCode;
69 };
70 
81 #ifndef SIP_RUN
82 class SERVER_EXPORT QgsOgcServiceException : public QgsServerException
83 {
84 #else
85 class SERVER_EXPORT QgsOgcServiceException
86 {
87 #endif
88  public:
90  QgsOgcServiceException( const QString &code, const QString &message, const QString &locator = QString(),
91  int responseCode = 200, const QString &version = QStringLiteral( "1.3.0" ) );
92 
94  QString message() const { return mMessage; }
95 
97  QString code() const { return mCode; }
98 
100  QString locator() const { return mLocator; }
101 
103  QString version() const { return mVersion; }
104 
105  QByteArray formatResponse( QString &responseFormat SIP_OUT ) const override;
106 
107  private:
108  QString mCode;
109  QString mMessage;
110  QString mLocator;
111  QString mVersion;
112 };
113 
120 #ifndef SIP_RUN
122 {
123  public:
124 
131  QgsBadRequestException( const QString &code, const QString &message, const QString &locator = QString() )
132  : QgsOgcServiceException( code, message, locator, 400 )
133  {}
134 };
135 #endif
136 
137 #ifndef SIP_RUN // No API exceptions for SIP, see python/server/qgsserverexception.sip
138 
149 class SERVER_EXPORT QgsServerApiException: public QgsServerException
150 {
151  public:
153  QgsServerApiException( const QString &code, const QString &message, const QString &mimeType = QStringLiteral( "application/json" ), int responseCode = 200 )
154  : QgsServerException( message, responseCode )
155  , mCode( code )
156  , mMimeType( mimeType )
157  {
158  }
159 
160  QByteArray formatResponse( QString &responseFormat SIP_OUT ) const override
161  {
162  responseFormat = mMimeType;
163  const json data
164  {
165  {
166  { "code", mCode.toStdString() },
167  { "description", what().toStdString() },
168  }
169  };
170  if ( responseFormat == QLatin1String( "application/json" ) )
171  {
172  return QByteArray::fromStdString( data.dump() );
173  }
174  else if ( responseFormat == QLatin1String( "text/html" ) )
175  {
176  // TODO: template
177  return QByteArray::fromStdString( data.dump() );
178  }
179  else
180  {
181  // TODO: template
182  return QByteArray::fromStdString( data.dump() );
183  }
184  }
185 
186  private:
187  QString mCode;
188  QString mMimeType;
189 };
190 
191 
203 {
204  public:
206  QgsServerApiInternalServerError( const QString &message = QStringLiteral( "Internal server error" ), const QString &mimeType = QStringLiteral( "application/json" ), int responseCode = 500 )
207  : QgsServerApiException( QStringLiteral( "Internal server error" ), message, mimeType, responseCode )
208  {
209  }
210 };
211 
212 
224 {
225  public:
227  QgsServerApiNotFoundError( const QString &message, const QString &mimeType = QStringLiteral( "application/json" ), int responseCode = 404 )
228  : QgsServerApiException( QStringLiteral( "API not found error" ), message, mimeType, responseCode )
229  {
230  }
231 };
232 
233 
245 {
246  public:
248  QgsServerApiBadRequestException( const QString &message, const QString &mimeType = QStringLiteral( "application/json" ), int responseCode = 400 )
249  : QgsServerApiException( QStringLiteral( "Bad request error" ), message, mimeType, responseCode )
250  {
251  }
252 };
253 
254 
266 {
267  public:
269  QgsServerApiPermissionDeniedException( const QString &message, const QString &mimeType = QStringLiteral( "application/json" ), int responseCode = 403 )
270  : QgsServerApiException( QStringLiteral( "Forbidden" ), message, mimeType, responseCode )
271  {
272  }
273 };
274 
286 {
287  public:
289  QgsServerApiImproperlyConfiguredException( const QString &message, const QString &mimeType = QStringLiteral( "application/json" ), int responseCode = 500 )
290  : QgsServerApiException( QStringLiteral( "Improperly configured error" ), message, mimeType, responseCode )
291  {
292  }
293 };
294 
295 
307 {
308  public:
310  QgsServerApiNotImplementedException( const QString &message = QStringLiteral( "Requested method is not implemented" ), const QString &mimeType = QStringLiteral( "application/json" ), int responseCode = 500 )
311  : QgsServerApiException( QStringLiteral( "Not implemented error" ), message, mimeType, responseCode )
312  {
313  }
314 };
315 
316 
327 {
328  public:
330  QgsServerApiInvalidMimeTypeException( const QString &message = QStringLiteral( "The Accept header submitted in the request did not support any of the media types supported by the server for the requested resource" ), const QString &mimeType = QStringLiteral( "application/json" ), int responseCode = 406 )
331  : QgsServerApiException( QStringLiteral( "Invalid mime-type" ), message, mimeType, responseCode )
332  {
333  }
334 };
335 #endif // no API exceptions for SIP
336 
337 #endif
QgsServerApiImproperlyConfiguredException
configuration error on the server prevents to serve the request, which would be valid otherwise.
Definition: qgsserverexception.h:285
QgsException
Defines a QGIS exception class.
Definition: qgsexception.h:34
QgsServerApiNotImplementedException
this method is not yet implemented
Definition: qgsserverexception.h:306
QgsOgcServiceException::locator
QString locator() const
Returns the locator.
Definition: qgsserverexception.h:100
SIP_OUT
#define SIP_OUT
Definition: qgis_sip.h:58
QgsServerApiNotFoundError
Not found error API exception.
Definition: qgsserverexception.h:223
QgsOgcServiceException::code
QString code() const
Returns the exception code.
Definition: qgsserverexception.h:97
QgsServerApiInternalServerError
Internal server error API exception.
Definition: qgsserverexception.h:202
QgsServerApiException
Exception base class for API exceptions.
Definition: qgsserverexception.h:149
QgsServerApiException::QgsServerApiException
QgsServerApiException(const QString &code, const QString &message, const QString &mimeType=QStringLiteral("application/json"), int responseCode=200)
Construction.
Definition: qgsserverexception.h:153
QgsServerApiBadRequestException::QgsServerApiBadRequestException
QgsServerApiBadRequestException(const QString &message, const QString &mimeType=QStringLiteral("application/json"), int responseCode=400)
Construction.
Definition: qgsserverexception.h:248
QgsServerException::responseCode
int responseCode() const
Definition: qgsserverexception.h:55
qgis_sip.h
QgsServerApiNotFoundError::QgsServerApiNotFoundError
QgsServerApiNotFoundError(const QString &message, const QString &mimeType=QStringLiteral("application/json"), int responseCode=404)
Construction.
Definition: qgsserverexception.h:227
QgsServerApiInternalServerError::QgsServerApiInternalServerError
QgsServerApiInternalServerError(const QString &message=QStringLiteral("Internal server error"), const QString &mimeType=QStringLiteral("application/json"), int responseCode=500)
Construction.
Definition: qgsserverexception.h:206
QgsOgcServiceException
Exception base class for service exceptions.
Definition: qgsserverexception.h:82
QgsBadRequestException::QgsBadRequestException
QgsBadRequestException(const QString &code, const QString &message, const QString &locator=QString())
Constructor for QgsBadRequestException (HTTP error code 400).
Definition: qgsserverexception.h:131
QgsServerApiException::formatResponse
QByteArray formatResponse(QString &responseFormat) const override
Formats the exception for sending to client.
Definition: qgsserverexception.h:160
QgsOgcServiceException::message
QString message() const
Returns the exception message.
Definition: qgsserverexception.h:94
QgsServerException
Exception base class for server exceptions.
Definition: qgsserverexception.h:42
QgsServerApiPermissionDeniedException::QgsServerApiPermissionDeniedException
QgsServerApiPermissionDeniedException(const QString &message, const QString &mimeType=QStringLiteral("application/json"), int responseCode=403)
Construction.
Definition: qgsserverexception.h:269
QgsServerApiInvalidMimeTypeException::QgsServerApiInvalidMimeTypeException
QgsServerApiInvalidMimeTypeException(const QString &message=QStringLiteral("The Accept header submitted in the request did not support any of the media types supported by the server for the requested resource"), const QString &mimeType=QStringLiteral("application/json"), int responseCode=406)
Construction.
Definition: qgsserverexception.h:330
qgsexception.h
QgsServerApiNotImplementedException::QgsServerApiNotImplementedException
QgsServerApiNotImplementedException(const QString &message=QStringLiteral("Requested method is not implemented"), const QString &mimeType=QStringLiteral("application/json"), int responseCode=500)
Construction.
Definition: qgsserverexception.h:310
QgsServerApiInvalidMimeTypeException
the client sent an invalid mime type in the "Accept" header
Definition: qgsserverexception.h:326
QgsBadRequestException
Exception thrown in case of malformed request.
Definition: qgsserverexception.h:121
QgsOgcServiceException::version
QString version() const
Returns the exception version.
Definition: qgsserverexception.h:103
QgsServerApiBadRequestException
Bad request error API exception.
Definition: qgsserverexception.h:244
QgsServerApiImproperlyConfiguredException::QgsServerApiImproperlyConfiguredException
QgsServerApiImproperlyConfiguredException(const QString &message, const QString &mimeType=QStringLiteral("application/json"), int responseCode=500)
Construction.
Definition: qgsserverexception.h:289
QgsServerApiPermissionDeniedException
Forbidden (permission denied) 403.
Definition: qgsserverexception.h:265