From a283dfae5fba6f83ee7712b43fd65843e17bfb5e Mon Sep 17 00:00:00 2001 From: Magnus Holm Date: Fri, 27 Jun 2008 20:58:09 +0200 Subject: [PATCH] Improving the error-system --- lib/oembed/errors.rb | 20 +++++++++++++++++--- lib/oembed/provider.rb | 13 ++++++++----- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/lib/oembed/errors.rb b/lib/oembed/errors.rb index d6df9e6..84647c9 100644 --- a/lib/oembed/errors.rb +++ b/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 \ No newline at end of file diff --git a/lib/oembed/provider.rb b/lib/oembed/provider.rb index e6c38b1..255eabc 100644 --- a/lib/oembed/provider.rb +++ b/lib/oembed/provider.rb @@ -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 @@ -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 = {}) @@ -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