Before my code runs, I would like to evaluate if a web service is running. How would I do that in JavaScript?
To evaluate if a layer if any web services are not running, you would enter the following code:
app.map.on("layer-add-result", function (evt) { console.log(evt); var testValue = evt.layer.valueOf(); if (testValue._div == null) { /* Page Redirect */ window.location.assign("http://www.w3schools.com") } });
You may not need app for your application.
Exactly. I just want to know how to read the value of 500 from my code. Even an alert would help, as I want to exit my code if this code exists.
Chris,
Just checking the html return from that url does not show the error but if you use http://maps.decaturil.gov/arcgis/rest/services/test/StreetSignTest/FeatureServer?f=json Then you will see error code 500
I'm unsure, but could you check by pulling the first layer?
http://maps.decaturil.gov/arcgis/rest/services/test/StreetSignTest/FeatureServer/0
I am currently not receiving any errors when I pasted in the URL for the stopped web service. Any ideas why?
Here is the stopped service location: test/StreetSignTest (FeatureServer)
I'm currently not receiving any error messages when I paste in the URL for a stopped service. Any ideas?
Okay, I have my service stopped right now at: test/StreetSignTest (FeatureServer)
The URL does not provide an error by opening the page; it just shows up as blank.
Chris mentioned about checking it before his code runs - I wasn't too sure if he meant before his mapping app loaded, or before some bit of code executed in the map app. If it's the latter, it looks like Todd has a better resolution in catching the error.
Hey Chris, just some additional clarification for others that may come across your question. HTTP 404 error means not found - no page exists at a particular URL. Where an HTTP 500 error means something happened when the server tried to process the request and it failed. 500 errors can happen for a variety of reasons. Usually there is some additional indication in the 500 error message from ArcGIS Online giving a hint. One example might be you tried to add an attachment to a non-existent ObjectId in a feature service.
Just to clarify, when I'm referring to the map service throwing a 500 when it doesn't exist, this is assuming the server does exist, e.g. myMapServer/arcgis/rest/services/base_services/1234_does_not_exist/MapServer
Throws a 500 because the server page will still exist (just indicating a status of 500 and that the map service does not exist). I verified in Fiddler the 500 code is still given. You could also add a case for a 404, if needed, so if the server was completely not found:
$.ajax(serviceURL, { type: "POST", statusCode: { 404: function (response) { alert('404, not found'); }, 500: function (response) { alert('500 error'); } }, success: function () { alert('success'); }, });
A map service that doesn't exist, or is not running, should return a 500. Would something like this work for you?
$.ajax(serviceURL, { type: "POST", statusCode: { 500: function (response) { alert('500 error'); } }, success: function () { alert('success'); }, });
I suppose one could use the map's layer-add-result event to populate a boolean variable to log each layer's status. A caveat being that this would only capture any initial efforts to add the layer. If something goes south after map load, this wouldn't help.
You can try and catch the error:
<script type="text/javascript"> var gmap; function initialize() { gmap = new GMap2(document.getElementById("map_canvas")); gmap.enableScrollWheelZoom(); gmap.setCenter(new GLatLng(0, 0), 2); new esri.arcgis.gmaps.DynamicMapServiceLayer("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/Unknown_Map_Service/MapServer", null, 0.75, mapServiceLoaded); } function mapServiceLoaded(layer, error) { if (error) { // alert the error message to the user and return alert("Error " + error.code + ": " + (error.message || (error.details && error.details.join(" ")) || "Unknown error" )); return; } // add layer to the map gmap.addOverlay(layer); } </script>
Cheers,
Todd
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.