QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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
Exception thrown in case of malformed request.
QgsBadRequestException(const QString &code, const QString &message, const QString &locator=QString())
Constructor for QgsBadRequestException (HTTP error code 400).
Defines a QGIS exception class.
Definition: qgsexception.h:35
Exception base class for service exceptions.
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.
Bad request error API exception.
QgsServerApiBadRequestException(const QString &message, const QString &mimeType=QStringLiteral("application/json"), int responseCode=400)
Construction.
Exception base class for API exceptions.
QgsServerApiException(const QString &code, const QString &message, const QString &mimeType=QStringLiteral("application/json"), int responseCode=200)
Construction.
QByteArray formatResponse(QString &responseFormat) const override
Formats the exception for sending to client.
configuration error on the server prevents to serve the request, which would be valid otherwise.
QgsServerApiImproperlyConfiguredException(const QString &message, const QString &mimeType=QStringLiteral("application/json"), int responseCode=500)
Construction.
Internal server error API exception.
QgsServerApiInternalServerError(const QString &message=QStringLiteral("Internal server error"), const QString &mimeType=QStringLiteral("application/json"), int responseCode=500)
Construction.
the client sent an invalid mime type in the "Accept" header
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.
Not found error API exception.
QgsServerApiNotFoundError(const QString &message, const QString &mimeType=QStringLiteral("application/json"), int responseCode=404)
Construction.
this method is not yet implemented
QgsServerApiNotImplementedException(const QString &message=QStringLiteral("Requested method is not implemented"), const QString &mimeType=QStringLiteral("application/json"), int responseCode=500)
Construction.
Forbidden (permission denied) 403.
QgsServerApiPermissionDeniedException(const QString &message, const QString &mimeType=QStringLiteral("application/json"), int responseCode=403)
Construction.
Exception base class for server exceptions.
#define SIP_OUT
Definition: qgis_sip.h:58