Skip to content
This repository has been archived by the owner on Dec 27, 2018. It is now read-only.

Commit

Permalink
Better logging
Browse files Browse the repository at this point in the history
  • Loading branch information
emmenko committed Feb 23, 2014
1 parent f8fbf75 commit e657269
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/coffee/oauth2.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class OAuth2
timeout: opts.timeout or 20000
rejectUnauthorized: rejectUnauthorized

@logger.debug @_options, 'OAuth constructor initialized.'
@logger.debug @_options, 'New OAuth object'
return

getAccessToken: (callback) ->
Expand Down
14 changes: 5 additions & 9 deletions src/coffee/rest.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -34,42 +34,39 @@ class Rest
if @_options.access_token
@_options.headers['Authorization'] = "Bearer #{@_options.access_token}"

@logger.debug @_options, 'Rest constructor initialized.'
@logger.debug @_options, 'New Rest object'
return

GET: (resource, callback) ->
@logger.info 'GET', resource
@logger.info "Project '#{@_options.config.project_key}'", 'GET', resource
params =
resource: resource
method: 'GET'
@_preRequest(params, callback)

POST: (resource, payload, callback) ->
@logger.info 'POST', resource, payload
@logger.info "Project '#{@_options.config.project_key}'", 'POST', resource, payload
params =
resource: resource
method: 'POST'
body: payload
@_preRequest(params, callback)

DELETE: (resource, callback) ->
@logger.info 'DELETE', resource
@logger.info "Project '#{@_options.config.project_key}'", 'DELETE', resource
params =
resource: resource
method: 'DELETE'
@_preRequest(params, callback)

PUT: ->
@logger.warn 'PUT', 'Method not implemented yet'
throw new Error 'Not implemented yet'
PUT: -> throw new Error 'Not implemented yet'

_preRequest: (params, callback) ->
_req = (retry) =>
unless @_options.access_token
@_oauth.getAccessToken (error, response, body) =>
if error
if retry is 10
@logger.warn error, 'Error on retrieving access_token after 10 attempts.'
throw new Error 'Error on retrieving access_token after 10 attempts.\n' +
"Error: #{error}\n"
else
Expand All @@ -78,7 +75,6 @@ class Rest
if response.statusCode isnt 200
# try again to get an access token
if retry is 10
@logger.warn body, 'Error on retrieving access_token after 10 attempts.'
throw new Error 'Could not retrieve access_token after 10 attempts.\n' +
"Status code: #{response.statusCode}\n" +
"Body: #{body}\n"
Expand Down

0 comments on commit e657269

Please sign in to comment.