Skip to content

Commit

Permalink
Issue #6 - Handle socket errors in upload and api. Allow setting time…
Browse files Browse the repository at this point in the history
…out length externally
  • Loading branch information
TalLevAmi committed Jan 9, 2013
1 parent 04a1713 commit c46268e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/api.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions lib/uploader.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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])
Expand Down

0 comments on commit c46268e

Please sign in to comment.