Skip to content

Commit

Permalink
Handle 404s cleanly
Browse files Browse the repository at this point in the history
  • Loading branch information
nickpeihl committed Jan 8, 2017
1 parent dd4e268 commit 615b44c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ module.exports = function (url, opts, cb) {
try {
request(url + '?f=json', {
json: true
}, function (err, data) {
}, function (err, data, res) {
if (res.statusCode === 404) {
err = res.rawRequest.statusMessage
}
if (err) return cb(err)
if (!_isAgs(data)) {
return cb('Is not a valid ArcGIS Server URL')
}

services.push(data.services)
const harvester = _harvestFolders(url, data.folders)
parallel(harvester, limit, function (err, res) {
Expand Down
24 changes: 21 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ test('valid ags', function (t) {
res.status = 200
res.end(JSON.stringify(root))
} else if (req.url === '/arcgis/rest/services/Basemaps?f=json') {
res.status = 200
res.statusCode = 200
res.end(JSON.stringify(basemaps))
} else {
res.status = 404
Expand All @@ -67,7 +67,7 @@ test('ags with no folders', function (t) {
t.plan(3)
const server = http.createServer(function (req, res) {
t.equal(req.url, '/arcgis/rest/services?f=json', 'should return correct url')
res.status = 200
res.statusCode = 200
res.end(JSON.stringify(rootNoServices))
})
server.listen(0, function () {
Expand All @@ -84,7 +84,7 @@ test('invalid ags', function (t) {
t.plan(3)
const server = http.createServer(function(req, res) {
t.equal(req.url, '/arcgis/bad/services?f=json', 'should return url with query string')
res.status = 200
res.statusCode = 200
res.end(JSON.stringify({
'pretty': 'bad'
}))
Expand All @@ -98,3 +98,21 @@ test('invalid ags', function (t) {
})
})
})

test('404', function (t) {
t.plan(3)
const server = http.createServer(function (req, res) {
t.equal(req.url, '/arcgis/404/services?f=json')
res.statusCode = 404
res.statusMessage = 'Not found'
res.end('<body><h1>404</h1</body>')
})
server.listen(0, function () {
const port = server.address().port
agsWalk('http://localhost:' + port + '/arcgis/404/services', function (err, res) {
t.ok(err)
t.equal(err, 'Not found')
server.close()
})
})
})

0 comments on commit 615b44c

Please sign in to comment.