def handle_exception(response, to_send = "code", send_json = false)
      msg = "HTTP Code: #{response.code}, Response Body: #{response.body}"
      @logger.debug msg
      case response.code.to_i
      # As of Jenkins version 1.519, the job builds return a 201 status code
      # with a Location HTTP header with the pointing the URL of the item in
      # the queue.
      when 200, 201, 302
        if to_send == "body" && send_json
          return JSON.parse(response.body)
        elsif to_send == "body"
          return response.body
        elsif to_send == "code"
          return response.code
        elsif to_send == "raw"
          return response
        end
      when 400
        matched = response.body.match(/<p>(.*)<\/p>/)
        api_message = matched[1] unless matched.nil?
        @logger.debug "API message: #{api_message}"
        case api_message
        when /A job already exists with the name/
          raise Exceptions::JobAlreadyExists.new(@logger, api_message)
        when /A view already exists with the name/
          raise Exceptions::ViewAlreadyExists.new(@logger, api_message)
        when /Slave called .* already exists/
          raise Exceptions::NodeAlreadyExists.new(@logger, api_message)
        when /Nothing is submitted/
          raise Exceptions::NothingSubmitted.new(@logger, api_message)
        else
          raise Exceptions::ApiException.new(@logger, api_message)
        end
      when 401
        raise Exceptions::Unauthorized.new @logger
      when 403
        raise Exceptions::Forbidden.new @logger
      when 404
        raise Exceptions::NotFound.new @logger
      when 500
        matched = response.body.match(/Exception: (.*)<br>/)
        api_message = matched[1] unless matched.nil?
        @logger.debug "API message: #{api_message}"
        raise Exceptions::InternalServerError.new(@logger, api_message)
      when 503
        raise Exceptions::ServiceUnavailable.new @logger
      else
        raise Exceptions::ApiException.new(
          @logger,
          "Error code #{response.code}"
        )
      end
    end
def handle_exception(response, to_send = "code", send_json = false)
      msg = "HTTP Code: #{response.code}, Response Body: #{response.body}"
      @logger.debug msg
      case response.code.to_i
      # As of Jenkins version 1.519, the job builds return a 201 status code
      # with a Location HTTP header with the pointing the URL of the item in
      # the queue.
      when 200, 201, 302
        if to_send == "body" && send_json
          return JSON.parse(response.body)
        elsif to_send == "body"
          return response.body
        elsif to_send == "code"
          return response.code
        elsif to_send == "raw"
          return response
        end
      when 400
        matched = response.body.match(/<p>(.*)<\/p>/)
        api_message = matched[1] unless matched.nil?
        @logger.debug "API message: #{api_message}"
        case api_message
        when /A job already exists with the name/
          raise Exceptions::JobAlreadyExists.new(@logger, api_message)
        when /A view already exists with the name/
          raise Exceptions::ViewAlreadyExists.new(@logger, api_message)
        when /Slave called .* already exists/
          raise Exceptions::NodeAlreadyExists.new(@logger, api_message)
        when /Nothing is submitted/
          raise Exceptions::NothingSubmitted.new(@logger, api_message)
        else
          raise Exceptions::ApiException.new(@logger, api_message)
        end
      when 401
        raise Exceptions::Unauthorized.new @logger
      when 403
        raise Exceptions::Forbidden.new @logger
      when 404
        raise Exceptions::NotFound.new @logger
      when 500
        matched = response.body.match(/Exception: (.*)<br>/)
        api_message = matched[1] unless matched.nil?
        @logger.debug "API message: #{api_message}"
        raise Exceptions::InternalServerError.new(@logger, api_message)
      when 503
        raise Exceptions::ServiceUnavailable.new @logger
      else
        raise Exceptions::ApiException.new(
          @logger,
          "Error code #{response.code}"
        )
      end
    end
Private method that handles the exception and raises with proper error
 message with the type of exception and returns the required values if no
 exceptions are raised.

 @param [Net::HTTP::Response] response Response from Jenkins
 @param [String] to_send What should be returned as a response. Allowed
   values: "code", "body", and "raw".
 @param [Boolean] send_json Boolean value used to determine whether to
   load the JSON or send the response as is.

 @return [String, Hash] Response returned whether loaded JSON or raw
   string

 @raise [Exceptions::Unauthorized] When invalid credentials are
   provided to connect to Jenkins
 @raise [Exceptions::NotFound] When the requested page on Jenkins is not
   found
 @raise [Exceptions::InternalServerError] When Jenkins returns a 500
   Internal Server Error
 @raise [Exceptions::ApiException] Any other exception returned from
   Jenkins that are not categorized in the API Client.