QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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
121 class SERVER_EXPORT QgsBadRequestException: public QgsOgcServiceException
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  json data
164  {
165  {
166  { "code", mCode.toStdString() },
167  { "description", what().toStdString() },
168  }
169  };
170  if ( responseFormat == QStringLiteral( "application/json" ) )
171  {
172  return QByteArray::fromStdString( data.dump() );
173  }
174  else if ( responseFormat == QStringLiteral( "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 
265 {
266  public:
268  QgsServerApiImproperlyConfiguredException( const QString &message, const QString &mimeType = QStringLiteral( "application/json" ), int responseCode = 500 )
269  : QgsServerApiException( QStringLiteral( "Improperly configured error" ), message, mimeType, responseCode )
270  {
271  }
272 };
273 
274 
286 {
287  public:
289  QgsServerApiNotImplementedException( const QString &message = QStringLiteral( "Requested method is not implemented" ), const QString &mimeType = QStringLiteral( "application/json" ), int responseCode = 500 )
290  : QgsServerApiException( QStringLiteral( "Not implemented error" ), message, mimeType, responseCode )
291  {
292  }
293 };
294 
295 
306 {
307  public:
309  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 )
310  : QgsServerApiException( QStringLiteral( "Invalid mime-type" ), message, mimeType, responseCode )
311  {
312  }
313 };
314 #endif // no API exceptions for SIP
315 
316 #endif
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.
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).
Exception base class for service exceptions.
QString message() const
Returns the exception message.
Internal server error API exception.
Exception base class for API exceptions.
QgsServerApiException(const QString &code, const QString &message, const QString &mimeType=QStringLiteral("application/json"), int responseCode=200)
Construction.
this method is not yet implemented
QString version() const
Returns the exception version.
configuration error on the server prevents to serve the request, which would be valid otherwise...
QString locator() const
Returns the locator.
QgsServerApiBadRequestException(const QString &message, const QString &mimeType=QStringLiteral("application/json"), int responseCode=400)
Construction.
Exception base class for server exceptions.
Bad request error API exception.
QByteArray formatResponse(QString &responseFormat) const override
Formats the exception for sending to client.
QString code() const
Returns the exception code.
#define SIP_OUT
Definition: qgis_sip.h:58
QgsServerApiNotImplementedException(const QString &message=QStringLiteral("Requested method is not implemented"), const QString &mimeType=QStringLiteral("application/json"), int responseCode=500)
Construction.
QgsServerApiNotFoundError(const QString &message, const QString &mimeType=QStringLiteral("application/json"), int responseCode=404)
Construction.
the client sent an invalid mime type in the "Accept" header
Defines a QGIS exception class.
Definition: qgsexception.h:34
QgsServerApiInternalServerError(const QString &message=QStringLiteral("Internal server error"), const QString &mimeType=QStringLiteral("application/json"), int responseCode=500)
Construction.
QgsServerApiImproperlyConfiguredException(const QString &message, const QString &mimeType=QStringLiteral("application/json"), int responseCode=500)
Construction.