Skip to content

Commit

Permalink
Add an enum for exception codes
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Mar 28, 2019
1 parent d7f12ea commit 60b308e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/server/services/wms/CMakeLists.txt
Expand Up @@ -25,6 +25,7 @@ SET (wms_SRCS

SET (wms_MOC_HDRS
qgswmsparameters.h
qgswmsserviceexception.h
)

########################################################
Expand Down
50 changes: 50 additions & 0 deletions src/server/services/wms/qgswmsserviceexception.h
Expand Up @@ -19,6 +19,7 @@
#define QGSWMSSERVICEEXCEPTION_H

#include <QString>
#include <QMetaEnum>

#include "qgsserverexception.h"

Expand All @@ -40,7 +41,27 @@ namespace QgsWms
*/
class QgsServiceException : public QgsOgcServiceException
{
Q_GADGET

public:
enum ExceptionCode
{
OGC_INVALID_FORMAT,
OGC_INVALID_SRS,
OGC_LAYER_NOT_DEFINED,
OGC_STYLE_NOT_DEFINED,
OGC_LAYER_NOT_QUERYABLE,
OGC_CURRENT_UPDATE_SEQUENCE,
OGC_INVALID_UPDATE_SEQUENCE,
OGC_MISSING_DIMENSION_VALUE,
OGC_INVALID_DIMENSION_VALUE,
OGC_INVALID_CRS, // new in WMS 1.3.0
OGC_OPERATION_NOT_SUPPORTED, // new in WMS 1.3.0
QGIS_MISSING_PARAMETER_VALUE,
QGIS_INVALID_PARAMETER_VALUE,
QGIS_ERROR
};
Q_ENUM( ExceptionCode )

/**
* Constructor for QgsServiceException.
Expand All @@ -64,6 +85,31 @@ namespace QgsWms
: QgsOgcServiceException( code, message, QString(), responseCode, QStringLiteral( "1.3.0" ) )
{}

QgsServiceException( ExceptionCode code, const QString &message, int responseCode )
: QgsServiceException( formatCode( code ), message, QString(), responseCode )
{}

private:
static QString formatCode( ExceptionCode code )
{
// get key as a string from enum
const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServiceException::ExceptionCode>() );
QString key = metaEnum.valueToKey( code );

// remove prefix
key.replace( QStringLiteral( "OGC_" ), QString() );
key.replace( QStringLiteral( "QGIS_" ), QString() );

// build the exception name
QString formattedCode;
for ( auto &part : key.split( '_' ) )
{
part = part.toLower().replace( 0, 1, part[0].toUpper() );
formattedCode = QString( "%1%2" ).arg( formattedCode ).arg( part );
}

return formattedCode;
}
};

/**
Expand Down Expand Up @@ -106,6 +152,10 @@ namespace QgsWms
QgsBadRequestException( const QString &code, const QString &message, const QString &locator = QString() )
: QgsServiceException( code, message, locator, 400 )
{}

QgsBadRequestException( ExceptionCode code, const QString &message )
: QgsServiceException( code, message, 400 )
{}
};
} // namespace QgsWms

Expand Down

0 comments on commit 60b308e

Please sign in to comment.