[OpenLayers-Users] Catching WMS errors when using OpenLayers.Layer.WMS... can it be done?

Paul Edwards pjfingers at gmail.com
Wed May 2 17:05:50 EDT 2012


I posed this question a while ago, and having never received an answer I
came up with my own hackadelic solution. I post it now in case it helps
someone in the future. Prior to adding a layer to the map I generate an
AJAX call to retrieve a single tile (via a server-side proxy). I make sure
all the specific unique params that are going to be present in the
OpenLayers call are present in my test call. I then simply check the
result. If I get an image back I know the layer is good. If I get error XML
then I can review the error text and respond accordingly. If I get a server
error I know the layer itself is configured incorrectly and again I can
respond accordingly.

Here's the Layer code to perform the validation as I wrote it, and while
much of this is specific to our application the key bits are there if you
want them...


    /**
     *The goal here is to determine if an externally sourced WMS layer is
functional. Disfunctionality
     *could take the form of a down server, an invalid config, or a bogus
SLD.
     */
    validateExternalLayer: function(){

        //some trickery to get the URL to test against
        var op = this.getOpenLayersLayer(); //a fully instantiated
OpenLayers.Layer.WMS object
        op.imageSize = new OpenLayers.Size( 256, 256 );
        op.map = this.getOpenLayersMap();
        var url = op.getURL( new OpenLayers.Bounds(-1,-1,1,1) );
        //back em out, otherwise it will never be able to be added to the
map
        op.imageSize = null;
        op.map = null;

        var isValid = false;
        //for use within the ajax handlers
        var exLayer = this;

        $.ajax({
            async: false,
            timeout: 5000,
            url: '/assets/php/requestProxy',
            type: "POST",
            data: {
                url: url
            },
            complete: function( xhr, status ){
                var statusCode = xhr.status;
                var statusText = xhr.statusText;
                var contentType = xhr.getResponseHeader( "Content-Type" );
                var respText = xhr.responseText;
                var layerName = exLayer.getLayerInfo().name;
                if( statusCode != 200 ){
                    Ext.Msg.alert("Layer Error", "The <b>" + layerName  +
"</b> layer cannot be properly loaded due to a server error. An error code
was returned of:<br>"+ statusCode + " - " + statusText + "<br><br>This
layer will be disabled.");
                }else{
                    if( contentType == "text/html" ){
                        //This content type is returned when there is a PHP
error which is always a Server SLD configuration error
                        Ext.Msg.alert("Layer Error", "The <b>" + layerName
 + "</b> layer cannot be properly loaded due to a configuration
error.<br><br>This layer will be disabled.");
                    }else if( contentType.indexOf( "vnd.ogc.se_xml" ) != -1
){
                        //This content type is what the WMS server returns
error messages as
                        if( exLayer.getOpenLayersLayer().params.SLD ){
                            //WMS generated errors are fairly random and
non-specific. The only way to
                            //Conclusively determine  if it's an SLD
problem vs something else is
                            //to try removing the custom SLD and validate
again... this usually fixes it
                            delete exLayer.getOpenLayersLayer().params.SLD
                            delete
exLayer.getOpenLayersLayer().params.LAYER_SLD

                            if( exLayer.validateExternalLayer() ){
                                //The issue is with the locally defined
SLD...
                                Ext.Msg.alert("Layer Error", "The <b>" +
layerName  + "</b> layer styles cannot be properly loaded due to a WMS
error. The WMS server returned:<br>'"+respText+"'<br><br>This custom
styling for this layer will be disabled.");
                                //Default back to the WMS provided legend
                                exLayer.contextMenu = null;
                                exLayer.getLayerInfo().useExternalLegend =
true;
                                isValid = true;
                            }//There is no 'else'. Since this segment
features a nested call the
                            //error message will be displayed in that inner
call
                        }else{
                            Ext.Msg.alert("Layer Error", "The <b>" +
layerName  + "</b> layer cannot be properly loaded due to a WMS error. The
WMS server returned:<br>'"+respText+"'<br><br>This layer will be
disabled.");
                        }
                    }else{
                        isValid = true;
                    }
                }
            }
        });

        return isValid;
    }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20120502/5c699db2/attachment.html


More information about the Users mailing list