Skip to content

Commit

Permalink
Merge pull request #186 from x3ro/octokit-error-map
Browse files Browse the repository at this point in the history
Replace case statement with error-code-to-error-class map
  • Loading branch information
pengwynn committed Dec 3, 2012
2 parents 8645161 + 3794ea6 commit 3274e65
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
37 changes: 15 additions & 22 deletions lib/faraday/response/raise_octokit_error.rb
Expand Up @@ -4,29 +4,22 @@
# @api private
module Faraday
class Response::RaiseOctokitError < Response::Middleware
ERROR_MAP = {
400 => Octokit::BadRequest,
401 => Octokit::Unauthorized,
403 => Octokit::Forbidden,
404 => Octokit::NotFound,
406 => Octokit::NotAcceptable,
422 => Octokit::UnprocessableEntity,
500 => Octokit::InternalServerError,
501 => Octokit::NotImplemented,
502 => Octokit::BadGateway,
503 => Octokit::ServiceUnavailable
}

def on_complete(response)
case response[:status].to_i
when 400
raise Octokit::BadRequest, error_message(response)
when 401
raise Octokit::Unauthorized, error_message(response)
when 403
raise Octokit::Forbidden, error_message(response)
when 404
raise Octokit::NotFound, error_message(response)
when 406
raise Octokit::NotAcceptable, error_message(response)
when 422
raise Octokit::UnprocessableEntity, error_message(response)
when 500
raise Octokit::InternalServerError, error_message(response)
when 501
raise Octokit::NotImplemented, error_message(response)
when 502
raise Octokit::BadGateway, error_message(response)
when 503
raise Octokit::ServiceUnavailable, error_message(response)
end
key = response[:status].to_i
raise ERROR_MAP[key], error_message(response) if ERROR_MAP.has_key? key
end

def error_message(response)
Expand Down
2 changes: 1 addition & 1 deletion lib/octokit.rb
@@ -1,6 +1,6 @@
require 'octokit/configuration'
require 'octokit/client'
require 'octokit/error'
require 'octokit/client'

module Octokit
extend Configuration
Expand Down

0 comments on commit 3274e65

Please sign in to comment.