iangreenleaf / githubot

Github API access, tailored for Hubot
MIT License
106 stars 38 forks source link

Undefined body values #27

Closed lukerollans closed 3 years ago

lukerollans commented 9 years ago

Hey there,

Having a weird amount of trouble accessing any properties assigned in res.body within handleErrors.

EG (this is working, just providing context. I'm trying to expand on it but am getting an undefined issue that makes no sense to me):

# Handle errors
gh.handleErrors (res) ->

  # Handle 404 specifically
  if res.statusCode is 404
    msg.send "You haven't forked that repo"

  # Otherwise, send back the same friendly error message github generated
  msg.send "You twollop"

This is the output from console.log res.body

{"message":"Validation Failed","documentation_url":"https://developer.github.com/v3/pulls/#create-a-pull-request","errors":[{"resource":"PullRequest","code":"custom","message":"A pull request already exists for lukerollans:master."}]}

So, msg.send "You twollop. #{res.body.errors[0].message}" should read "You twollop. A pull request already exists for lukerollans:master", but I just get undefined on anything under res.body.. even console.log res.body.message

What am I missing?

lukerollans commented 9 years ago

So I was assuming that res.body was already JSONified.. it wasn't

body = JSON.parse res.body

Cheers

iangreenleaf commented 9 years ago

Could you share the code where you encountered this? When possible, githubot ought to do the JSON parsing for you, so this sounds like something I might want to fix.

lukerollans commented 9 years ago

@iangreenleaf encountered when creating a PR via the API

# Handle errors
gh.handleErrors (res) ->

  # JSONify the response from Github
  body = JSON.parse res.body

  # Process error
  if res.statusCode is 404
    msg.send "You haven't forked that repo, _sir_"
  else
    err = body.errors[body.errors.length - 1].message or "No error defined"
    msg.send "You twollop. #{err}"

gh.post "repos/#{process.env.HUBOT_GITHUB_ORG}/#{repo}/pulls", payload, (pr) ->

Without body = JSON.parse res.body, res.body is just a standard string

iangreenleaf commented 9 years ago

Cool, thanks. This is probably something that should be parsed automatically, since we know that it's coming back as JSON. I'm gonna reopen and see if I can get this fixed.

May be related to #22.