Skip to content

Commit

Permalink
Error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
frabcus committed Feb 21, 2013
1 parent 14f952e commit 28e66d3
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions scraper.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ db.run("CREATE TABLE IF NOT EXISTS bin_dates (type TEXT, next TEXT, PRIMARY KEY
// Setup events framework
var ee = new events.EventEmitter()

// Error handler
var handle_error = function(message) {
console.log("Error:", message)
request.post("https://x.scraperwiki.com/api/status", {'form': {'type':'error', 'message': "" + message} })
}
// Catch all uncaught exceptions
process.on('uncaughtException', handle_error)

// Write out the file which the Arduino's read - it sets:
// - light off
// R recycling light on
Expand All @@ -38,10 +46,10 @@ var write_bin_light_state = function(next_recycling) {
}
var fs = require('fs');
fs.writeFile("http/light.state", light_bin, function(err) {
if(err) {
console.log(err);
if (err) {
return handle_error(err)
} else {
console.log("Set light.state to", light_bin);
console.log("Set light.state to", light_bin)
}
});
}
Expand All @@ -64,16 +72,22 @@ var parse_row = function($, row, type) {
}

// Read one whole page - which has a table of recycling collections for a house
var parse_page = function (error, response, body) {
if (error || response.statusCode != 200) {
return console.log(err)
var parse_page = function (err, response, body) {
if (err) {
return handle_error(err)
}
if (response.statusCode != 200) {
return handle_error("HTTP error: " + response.statusCode)
}

// parse using jQuery via jsdom
jsdom.env({
html: body,
scripts: [ 'http://code.jquery.com/jquery-1.5.min.js' ]
}, function (err, window) {
if (err) {
return handle_error(err)
}
var $ = window.jQuery

// check header is as we expect
Expand All @@ -87,14 +101,15 @@ var parse_page = function (error, response, body) {
var next_refuse = parse_row($, $('.Refuse td'), 'refuse')
var next_recycling = parse_row($, $('.Recycling td'), 'recycling')

request.post("https://x.scraperwiki.com/api/status", {'form': {'type':'ok', 'message':'All is good'} })
ee.emit('newNextRecycling', next_recycling)
})
}

// Get the page with the data on from Liverpool Council
var get_bin_collections = function(err,data) {
if (err) {
return console.log(err)
return handle_error(err)
}
var json = JSON.parse(data)
var postcode = json['postcode']
Expand Down

0 comments on commit 28e66d3

Please sign in to comment.