lib/geonames_api/error.rb in geonames_api-0.1.1 vs lib/geonames_api/error.rb in geonames_api-0.1.2
- old
+ new
@@ -1,28 +1,52 @@
module GeoNamesAPI
class Error < StandardError
# See http://www.geonames.org/export/webservice-exception.html
def self.from_status(status)
- val = status['value'].to_i
- error_type = case val
- when 12, 13, 22
- Timeout
- when 14
- InvalidParameter
- when 21
- InvalidInput
- else
- Error
+ error_code = status['value'].to_i
+ error_class = for_error_code(error_code) || self
+ raise error_class, "#{status['message']} (#{error_code})"
+ end
+
+ def self.for_error_code(error_code)
+ @lookup ||= subclasses.reduce({}) do |h, subclass|
+ subclass::ERROR_CODES.each { |ea| h[ea] = subclass }
+ h
end
- raise error_type, "#{status['message']} (#{val})"
+ @lookup[error_code]
end
end
+ class AuthorizationException < Error
+ ERROR_CODES = [10]
+ end
+ class RecordDoesNotExist < Error
+ ERROR_CODES = [11]
+ end
class Timeout < Error
+ ERROR_CODES = [13, 22]
end
-
class InvalidParameter < Error
+ ERROR_CODES = [14]
end
-
- class InvalidInput < Error
+ class NoResultFound < Error
+ ERROR_CODES = [15]
+ end
+ class DuplicateException < Error
+ ERROR_CODES = [16]
+ end
+ class PostalCodeNotFound < Error
+ ERROR_CODES = [17]
+ end
+ class DailyLimitExceeded < Error
+ ERROR_CODES = [18]
+ end
+ class HourlyLimitExceeded < Error
+ ERROR_CODES = [19]
+ end
+ class WeeklyLimitExceeded < Error
+ ERROR_CODES = [20]
+ end
+ class ServiceNotImplemented < Error
+ ERROR_CODES = [23]
end
end