Skip to content

Commit

Permalink
Improving the error-system
Browse files Browse the repository at this point in the history
  • Loading branch information
judofyr committed Jun 27, 2008
1 parent 8be532d commit a283dfa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
20 changes: 17 additions & 3 deletions lib/oembed/errors.rb
@@ -1,5 +1,19 @@
module OEmbed
NotFound = Class.new(StandardError)
UnknownFormat = Class.new(StandardError)
UnknownResponse = Class.new(StandardError)
class NotFound < StandardError
def to_s
"No embeddable content at '#{super}'"
end
end

class UnknownFormat < StandardError
def to_s
"The provider doesn't support the '#{super}' format"
end
end

class UnknownResponse < StandardError
def to_s
"Got unkown response (#{super}) from server"
end
end
end
13 changes: 8 additions & 5 deletions lib/oembed/provider.rb
Expand Up @@ -16,7 +16,7 @@ def <<(url)
end

def build(url, options = {})
raise OEmbed::NotFound, "No embeddable content at '#{url}'" unless include?(url)
raise OEmbed::NotFound, url unless include?(url)
query = options.merge({:url => url})
endpoint = @endpoint.clone

Expand All @@ -31,7 +31,10 @@ def build(url, options = {})
"#{key}=#{value}&#{memo}"
end.chop

URI.parse(endpoint + query_string)
URI.parse(endpoint + query_string).instance_eval do
@format = format; def format; @format; end
self
end
end

def raw(url, options = {})
Expand All @@ -43,13 +46,13 @@ def raw(url, options = {})

case res
when Net::HTTPNotImplemented
raise OEmbed::UnknownFormat, "The provider doesn't support the '#{format}' format"
raise OEmbed::UnknownFormat, uri.format
when Net::HTTPNotFound
raise OEmbed::NotFound, "No embeddable content at '#{url}'"
raise OEmbed::NotFound, url
when Net::HTTPOK
res.body
else
raise OEmbed::UnknownResponse, "Got unkown response (#{res.code}) from server"
raise OEmbed::UnknownResponse, res.code
end
end

Expand Down

0 comments on commit a283dfa

Please sign in to comment.