Skip to content
This repository has been archived by the owner on Jun 8, 2023. It is now read-only.

Commit

Permalink
Add more error checking, and more descriptive reasons for failure
Browse files Browse the repository at this point in the history
  • Loading branch information
technicalpickles committed Nov 25, 2013
1 parent 1a31b16 commit 7ade3df
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions src/scripts/meme_generator.coffee
Expand Up @@ -35,6 +35,9 @@
# Author:
# skalnik


inspect = require('util').inspect

module.exports = (robot) ->
unless robot.brain.data.memes?
robot.brain.data.memes = [
Expand Down Expand Up @@ -196,19 +199,33 @@ memeGenerator = (msg, generatorID, imageID, text0, text1, callback) ->
text0: text0,
text1: text1
.get() (err, res, body) ->
result = JSON.parse(body)['result']
if result? and result['instanceUrl']? and result['instanceImageUrl']? and result['instanceID']?
instanceID = result['instanceID']
instanceURL = result['instanceUrl']
img = result['instanceImageUrl']
msg.http(instanceURL).get() (err, res, body) ->
# Need to hit instanceURL so that image gets generated
if preferredDimensions?
callback "http://images.memegenerator.net/instances/#{preferredDimensions}/#{instanceID}.jpg"
else
callback "http://images.memegenerator.net/instances/#{instanceID}.jpg"
else
msg.reply "Sorry, I couldn't generate that image."
if err
msg.reply "Ugh, I got an exception trying to contact memegenerator.net:", inspect(err)
return

jsonBody = JSON.parse(body)
success = jsonBody.success
errorMessage = jsonBody.errorMessage
result = jsonBody.result

if not success
msg.reply "Ugh, stupid request to memegenerator.net failed: \"#{errorMessage}.\" What does that even mean?"
return

instanceID = result?.instanceID
instanceURL = result?.instanceUrl
img = result?.instanceImageUrl

unless instanceID and instanceURL and img
msg.reply "Ugh, I got back weird results from memegenerator.net. Expected an image URL, but couldn't find it in the result. Here's what I got:", inspect(jsonBody)
return

msg.http(instanceURL).get() (err, res, body) ->
# Need to hit instanceURL so that image gets generated
if preferredDimensions?
callback "http://images.memegenerator.net/instances/#{preferredDimensions}/#{instanceID}.jpg"
else
callback "http://images.memegenerator.net/instances/#{instanceID}.jpg"

khanify = (msg) ->
msg = msg.toUpperCase()
Expand Down

0 comments on commit 7ade3df

Please sign in to comment.