Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Auto format message for missing parameter
  • Loading branch information
pblottiere committed Mar 28, 2019
1 parent c5817cb commit f5171b8
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/server/services/wms/qgswms.cpp
Expand Up @@ -21,6 +21,7 @@

#include "qgsmodule.h"
#include "qgsdxfwriter.h"
#include "qgswmsserviceexception.h"
#include "qgswmsgetcapabilities.h"
#include "qgswmsgetmap.h"
#include "qgswmsgetstyles.h"
Expand Down
1 change: 1 addition & 0 deletions src/server/services/wms/qgswmsgetlegendgraphics.cpp
Expand Up @@ -19,6 +19,7 @@
* *
***************************************************************************/
#include "qgswmsutils.h"
#include "qgswmsserviceexception.h"
#include "qgswmsgetlegendgraphics.h"
#include "qgswmsrenderer.h"

Expand Down
1 change: 1 addition & 0 deletions src/server/services/wms/qgswmsgetmap.cpp
Expand Up @@ -21,6 +21,7 @@
#include "qgswmsutils.h"
#include "qgswmsgetmap.h"
#include "qgswmsrenderer.h"
#include "qgswmsserviceexception.h"

#include <QImage>

Expand Down
1 change: 1 addition & 0 deletions src/server/services/wms/qgswmsgetprint.cpp
Expand Up @@ -21,6 +21,7 @@
#include "qgswmsutils.h"
#include "qgswmsgetprint.h"
#include "qgswmsrenderer.h"
#include "qgswmsserviceexception.h"

namespace QgsWms
{
Expand Down
1 change: 1 addition & 0 deletions src/server/services/wms/qgswmsparameters.cpp
Expand Up @@ -18,6 +18,7 @@
#include "qgswmsparameters.h"
#include "qgsdatasourceuri.h"
#include "qgsmessagelog.h"
#include "qgswmsserviceexception.h"

const QString EXTERNAL_LAYER_PREFIX = QStringLiteral( "EXTERNAL_WMS:" );

Expand Down
1 change: 0 additions & 1 deletion src/server/services/wms/qgswmsparameters.h
Expand Up @@ -23,7 +23,6 @@
#include <QColor>

#include "qgsrectangle.h"
#include "qgswmsserviceexception.h"
#include "qgslegendsettings.h"
#include "qgsprojectversion.h"
#include "qgsogcutils.h"
Expand Down
24 changes: 14 additions & 10 deletions src/server/services/wms/qgswmsrenderer.cpp
Expand Up @@ -131,12 +131,12 @@ namespace QgsWms
{
// check parameters
if ( mWmsParameters.allLayersNickname().isEmpty() )
throw QgsBadRequestException( QStringLiteral( "LayerNotSpecified" ),
QStringLiteral( "LAYER is mandatory for GetLegendGraphic operation" ) );
throw QgsBadRequestException( QgsServiceException::QGIS_MISSING_PARAMETER_VALUE,
QgsWmsParameter::LAYER );

if ( mWmsParameters.format() == QgsWmsParameters::Format::NONE )
throw QgsBadRequestException( QStringLiteral( "FormatNotSpecified" ),
QStringLiteral( "FORMAT is mandatory for GetLegendGraphic operation" ) );
throw QgsBadRequestException( QgsServiceException::QGIS_MISSING_PARAMETER_VALUE,
QgsWmsParameter::FORMAT );

// get layers
std::unique_ptr<QgsLayerRestorer> restorer;
Expand Down Expand Up @@ -269,8 +269,8 @@ namespace QgsWms
const QString templateName = mWmsParameters.composerTemplate();
if ( templateName.isEmpty() )
{
throw QgsBadRequestException( QStringLiteral( "ParameterMissing" ),
QStringLiteral( "The TEMPLATE parameter is required for the GetPrint request" ) );
throw QgsBadRequestException( QgsServiceException::QGIS_MISSING_PARAMETER_VALUE,
QgsWmsParameter::TEMPLATE );
}

// check template
Expand Down Expand Up @@ -854,8 +854,8 @@ namespace QgsWms
// The QUERY_LAYERS parameter is Mandatory
if ( mWmsParameters.queryLayersNickname().isEmpty() )
{
QString msg = QObject::tr( "QUERY_LAYERS parameter is required for GetFeatureInfo" );
throw QgsBadRequestException( QStringLiteral( "LayerNotDefined" ), msg );
throw QgsBadRequestException( QgsServiceException::QGIS_MISSING_PARAMETER_VALUE,
QgsWmsParameter::QUERY_LAYERS );
}

// The I/J parameters are Mandatory if they are not replaced by X/Y or FILTER or FILTER_GEOM
Expand All @@ -866,8 +866,12 @@ namespace QgsWms

if ( !ijDefined && !xyDefined && !filtersDefined && !filterGeomDefined )
{
throw QgsBadRequestException( QStringLiteral( "ParameterMissing" ),
QStringLiteral( "I/J parameters are required for GetFeatureInfo" ) );
QgsWmsParameter::Name name = QgsWmsParameter::I;

if ( mWmsParameters.j().isEmpty() )
name = QgsWmsParameter::J;

throw QgsBadRequestException( QgsServiceException::QGIS_MISSING_PARAMETER_VALUE, name );
}

const QgsWmsParameters::Format infoFormat = mWmsParameters.infoFormat();
Expand Down
43 changes: 43 additions & 0 deletions src/server/services/wms/qgswmsserviceexception.h
Expand Up @@ -22,6 +22,7 @@
#include <QMetaEnum>

#include "qgsserverexception.h"
#include "qgswmsparameters.h"

namespace QgsWms
{
Expand Down Expand Up @@ -89,7 +90,45 @@ namespace QgsWms
: QgsServiceException( formatCode( code ), message, QString(), responseCode )
{}

QgsServiceException( ExceptionCode code, QgsWmsParameter::Name name, int responseCode )
: QgsServiceException( formatCode( code ), formatMessage( code, name ), QString(), responseCode )
{}

private:
static QString formatMessage( ExceptionCode code, QgsWmsParameter::Name parameter )
{
QString message;

const QString name = QgsWmsParameter::name( parameter );

switch ( code )
{
case QgsServiceException::QGIS_MISSING_PARAMETER_VALUE:
{
message = QStringLiteral( "The %1 parameter is missing." ).arg( name );
break;
}
case OGC_INVALID_FORMAT:
case OGC_INVALID_SRS:
case OGC_LAYER_NOT_DEFINED:
case OGC_STYLE_NOT_DEFINED:
case OGC_LAYER_NOT_QUERYABLE:
case OGC_CURRENT_UPDATE_SEQUENCE:
case OGC_INVALID_UPDATE_SEQUENCE:
case OGC_MISSING_DIMENSION_VALUE:
case OGC_INVALID_DIMENSION_VALUE:
case OGC_INVALID_CRS:
case OGC_OPERATION_NOT_SUPPORTED:
case QGIS_INVALID_PARAMETER_VALUE:
case QGIS_ERROR:
{
break;
}
}

return message;
}

static QString formatCode( ExceptionCode code )
{
// get key as a string from enum
Expand Down Expand Up @@ -156,6 +195,10 @@ namespace QgsWms
QgsBadRequestException( ExceptionCode code, const QString &message )
: QgsServiceException( code, message, 400 )
{}

QgsBadRequestException( ExceptionCode code, QgsWmsParameter::Name parameter )
: QgsServiceException( code, parameter, 400 )
{}
};
} // namespace QgsWms

Expand Down

0 comments on commit f5171b8

Please sign in to comment.