Permalink
Browse files

add error handlers

  • Loading branch information...
1 parent d417e65 commit ed76a93c82fa2660630a92fe710e627332a03414 @podviaznikov podviaznikov committed Oct 15, 2012
Showing with 6 additions and 2 deletions.
  1. +1 −1 README.md
  2. +5 −1 lib/files.coffee
View
2 README.md
@@ -46,7 +46,7 @@ We appreciate feedback!
(The MIT License)
-Copyright (c) 2011 CircuitHub., https://circuithub.com/
+Copyright (c) 2011-2012 CircuitHub., https://circuithub.com/
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
View
6 lib/files.coffee
@@ -19,6 +19,8 @@ exports.fetchFromUrl = (url, path, callback) ->
req = request url, (err, response, body) ->
if err or response.statusCode != 200
callback {code: "fileNotFound", message: "File wasn't found on URL '#{url}'"}
+ req.on "error", (e) ->
+ callback {code: "errorFetchingFile", message: "File wasn't fetch using URL '#{url}'", error: e}
req.on "end", () ->
callback undefined, path # success callback
req.pipe fs.createWriteStream(path)
@@ -38,14 +40,16 @@ exports.fetchFromUrlToHash = (url, path, extension, callback) ->
callback {code: "fileNotFound", message: "File wasn't found on URL '#{url}'"} # error callback
tempId = uuid.v1()
fileStream = fs.createWriteStream(path + tempId)
- req.pipe fileStream
md5sum = crypto.createHash("md5")
+ req.on "error", (e) ->
+ callback {code: "errorFetchingFile", message: "File wasn't fetch using URL '#{url}'", error: e}
req.on "data", (d) ->
md5sum.update(d)
req.on "end", (d) ->
newPath = path + md5sum.digest("hex") + extension
fs.rename path + tempId, newPath, ->
callback undefined, newPath # success callback
+ req.pipe fileStream
catch error
callback {code: "invalidURL", message: "'#{url}' is invalid file URL"} # error callback

0 comments on commit ed76a93

Please sign in to comment.