lib/douban_api/error.rb in douban_api-0.1.5 vs lib/douban_api/error.rb in douban_api-0.1.6
- old
+ new
@@ -2,13 +2,33 @@
module Douban
# Custom error class for rescuing from all Douban errors
class Error < StandardError; end
# Raised when Douban returns the HTTP status code 400
- class BadRequest < Error; end
+ class BadRequest < Error
+ attr_reader :code, :msg
+ def initialize(body)
+ @code, @msg = 0, ""
+ parse_error_body(body)
+ end
+
+ private
+ def parse_error_body(body)
+ # body gets passed as a string, not sure if it is passed as something else from other spots?
+ if not body.nil? and not body.empty? and body.kind_of?(String)
+ # removed multi_json thanks to wesnolte's commit
+ body = ::JSON.parse(body)
+
+ if body['code'] and body['msg']
+ @code, @msg = body['code'], body['msg']
+ end
+ end
+ end
+ end
+
# Raised when Douban returns the HTTP status code 404
- class NotFound < Error; end
+ class NotFound < BadRequest; end
# Raised when Douban returns the HTTP status code 500
class InternalServerError < Error; end
# Raised when Douban returns the HTTP status code 503