Skip to content

Commit

Permalink
Some more exception work.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefansundin committed Jul 3, 2015
1 parent 1012539 commit 6e77dcb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
54 changes: 35 additions & 19 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,27 @@
require "erb"
require "active_support/core_ext/string"

class YoutubeError < StandardError; end
class FacebookError < StandardError; end
class InstagramError < StandardError; end
class PartyError < StandardError
def initialize(request)
@request = request
end

def request
@request
end

def data
@request.parsed_response
end

def message
@request.to_s
end
end

class YoutubeError < PartyError; end
class FacebookError < PartyError; end
class InstagramError < PartyError; end

def httparty_error(r)
"#{r.request.path.to_s}: #{r.code} #{r.message}: #{r.body}. #{r.headers.to_h.to_json}"
Expand Down Expand Up @@ -53,19 +71,17 @@ def httparty_error(r)

if user
response = HTTParty.get("https://www.googleapis.com/youtube/v3/channels?part=id&forUsername=#{user}&key=#{ENV["GOOGLE_API_KEY"]}")
if !response.success?
raise YoutubeError, response
end
raise YoutubeError.new(response) if !response.success?

if response.parsed_response["items"].length > 0
channel_id = response.parsed_response["items"][0]["id"]
end
end

if video_id
response = HTTParty.get("https://www.googleapis.com/youtube/v3/videos?part=snippet&id=#{video_id}&key=#{ENV["GOOGLE_API_KEY"]}")
if !response.success?
raise YoutubeError, response
end
raise YoutubeError.new(response) if !response.success?

if response.parsed_response["items"].length > 0
channel_id = response.parsed_response["items"][0]["snippet"]["channelId"]
end
Expand All @@ -91,7 +107,7 @@ def httparty_error(r)

response = HTTParty.get("https://graph.facebook.com/v2.3/#{name}?access_token=#{ENV["FACEBOOK_APP_ID"]}|#{ENV["FACEBOOK_APP_SECRET"]}")
return "Can't find a page with that name. Sorry." if response.code == 404
raise FacebookError, response if !response.success?
raise FacebookError.new(response) if !response.success?

data = response.parsed_response
redirect "/facebook/#{data["id"]}/#{data["username"] || data["name"]}"
Expand All @@ -101,7 +117,7 @@ def httparty_error(r)
@id = id

response = HTTParty.get("https://graph.facebook.com/v2.3/#{id}/posts?access_token=#{ENV["FACEBOOK_APP_ID"]}|#{ENV["FACEBOOK_APP_SECRET"]}")
raise FacebookError, response if !response.success?
raise FacebookError.new(response) if !response.success?

@data = response.parsed_response["data"]
@user = @data[0]["from"]["name"] rescue username
Expand All @@ -121,7 +137,7 @@ def httparty_error(r)
redirect_uri: request.base_url+request.path_info,
code: params[:code]
})
raise InstagramError, httparty_error(response) if !response.success?
raise InstagramError.new(response) if !response.success?
headers "Content-Type" => "text/plain"
"heroku config:set INSTAGRAM_ACCESS_TOKEN=#{response.parsed_response["access_token"]}"
else
Expand All @@ -145,7 +161,7 @@ def httparty_error(r)

if name
response = HTTParty.get("https://api.instagram.com/v1/users/search?q=#{name}&access_token=#{ENV["INSTAGRAM_ACCESS_TOKEN"]}")
raise InstagramError, response if !response.success?
raise InstagramError.new(response) if !response.success?
user = response.parsed_response["data"].find { |user| user["username"] == name }
end

Expand All @@ -166,7 +182,7 @@ def httparty_error(r)
headers "Content-Type" => "application/atom+xml;charset=utf-8"
return erb :instagram_error
end
raise InstagramError, response if !response.success?
raise InstagramError.new(response) if !response.success?

@data = response.parsed_response["data"]
@user = @data[0]["user"]["username"] rescue username
Expand Down Expand Up @@ -199,22 +215,22 @@ def httparty_error(r)
end


error do
error do |e|
status 500
"Sorry, a nasty error occurred: #{env["sinatra.error"].message}"
"Sorry, a nasty error occurred: #{e}"
end

error YoutubeError do
error YoutubeError do |e|
status 503
"There was a problem talking to YouTube."
end

error FacebookError do
error FacebookError do |e|
status 503
"There was a problem talking to Facebook."
end

error InstagramError do
error InstagramError do |e|
status 503
"There was a problem talking to Instagram."
end
3 changes: 3 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

set :erb, trim: "-"

# uncomment to get production error pages in development
# set :environment, :production

# development specific
configure :development do
use BetterErrors::Middleware
Expand Down

0 comments on commit 6e77dcb

Please sign in to comment.