From c46268e94b8250529e275059f4e8965242c9c2c9 Mon Sep 17 00:00:00 2001 From: Tal Lev-Ami Date: Wed, 9 Jan 2013 13:52:14 +0200 Subject: [PATCH] Issue #6 - Handle socket errors in upload and api. Allow setting timeout length externally --- lib/api.coffee | 3 ++- lib/uploader.coffee | 7 ++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/api.coffee b/lib/api.coffee index bf81ea9c..fb93505e 100644 --- a/lib/api.coffee +++ b/lib/api.coffee @@ -112,7 +112,8 @@ call_api = (method, uri, params, callback, options) -> callback(error: {message: "Server returned unexpected status code - #{res.statusCode}", http_code: res.statusCode}) request = https.request(request_options, handle_response) - request.setTimeout 60 + request.on "error", (e) -> callback(error: e) + request.setTimeout options["timeout"] ? 60 if method != "get" request.write(query_params) diff --git a/lib/uploader.coffee b/lib/uploader.coffee index 5342aaef..6555365e 100644 --- a/lib/uploader.coffee +++ b/lib/uploader.coffee @@ -152,10 +152,10 @@ call_api = (action, callback, options, get_params) -> callback(error: {message: "Server returned unexpected status code - #{res.statusCode}"}) post_data = (new Buffer(EncodeFieldPart(boundary, key, value), 'ascii') for key, value of params when utils.present(value)) - post api_url, post_data, boundary, file, handle_response + post api_url, post_data, boundary, file, handle_response, options true -post = (url, post_data, boundary, file, callback) -> +post = (url, post_data, boundary, file, callback, options) -> finish_buffer = new Buffer("--" + boundary + "--", 'ascii') length = 0 for i in [0..post_data.length-1] @@ -176,7 +176,8 @@ post = (url, post_data, boundary, file, callback) -> 'Content-Length': length post_request = https.request(post_options, callback) - post_request.setTimeout 60 + post_request.on "error", (e) -> callback(error: e) + post_request.setTimeout options["timeout"] ? 60 for i in [0..post_data.length-1] post_request.write(post_data[i])