HEX
Server: Apache
System: Linux hp3-stn-1011028.hostingp3.local 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64
User: csh958633 (951153)
PHP: 8.3.30
Disabled: shell_exec,exec,system,popen,set_time_limit
Upload Files
File: //usr/share/ruby/vendor_ruby/puppet/forge/errors.rb
require 'json'
require 'puppet/error'
require 'puppet/forge'

# Puppet::Forge specific exceptions
module Puppet::Forge::Errors

  # This exception is the parent for all Forge API errors
  class ForgeError < Puppet::Error
    # This is normally set by the child class, but if it is not this will
    # fall back to displaying the message as a multiline.
    #
    # @return [String] the multiline version of the error message
    def multiline
      self.message
    end
  end

  # This exception is raised when there is an SSL verification error when
  # communicating with the forge.
  class SSLVerifyError < ForgeError
    # @option options [String] :uri The URI that failed
    # @option options [String] :original the original exception
    def initialize(options)
      @uri     = options[:uri]
      original = options[:original]

      super("Unable to verify the SSL certificate at #{@uri}", original)
    end

    # Return a multiline version of the error message
    #
    # @return [String] the multiline version of the error message
    def multiline
      <<-EOS.chomp
Could not connect via HTTPS to #{@uri}
  Unable to verify the SSL certificate
    The certificate may not be signed by a valid CA
    The CA bundle included with OpenSSL may not be valid or up to date
      EOS
    end
  end

  # This exception is raised when there is a communication error when connecting
  # to the forge
  class CommunicationError < ForgeError
    # @option options [String] :uri The URI that failed
    # @option options [String] :original the original exception
    def initialize(options)
      @uri     = options[:uri]
      original = options[:original]
      @detail  = original.message

      message = "Unable to connect to the server at #{@uri}. Detail: #{@detail}."
      super(message, original)
    end

    # Return a multiline version of the error message
    #
    # @return [String] the multiline version of the error message
    def multiline
      <<-EOS.chomp
Could not connect to #{@uri}
  There was a network communications problem
    The error we caught said '#{@detail}'
    Check your network connection and try again
      EOS
    end
  end

  # This exception is raised when there is a bad HTTP response from the forge
  # and optionally a message in the response.
  class ResponseError < ForgeError
    # @option options [String] :uri The URI that failed
    # @option options [String] :input The user's input (e.g. module name)
    # @option options [String] :message Error from the API response (optional)
    # @option options [Net::HTTPResponse] :response The original HTTP response
    def initialize(options)
      @uri     = options[:uri]
      @input   = options[:input]
      @message = options[:message]
      response = options[:response]
      @response = "#{response.code} #{response.message.strip}"

      begin
        body = JSON.parse(response.body)
        if body['message']
          @message ||= body['message'].strip
        end
      rescue JSON::ParserError
      end

      message = "Could not execute operation for '#{@input}'. Detail: "
      message << @message << " / " if @message
      message << @response << "."
      super(message, original)
    end

    # Return a multiline version of the error message
    #
    # @return [String] the multiline version of the error message
    def multiline
      message = <<-EOS
Could not execute operation for '#{@input}'
  The server being queried was #{@uri}
  The HTTP response we received was '#{@response}'
      EOS
      message << "  The message we received said '#{@message}'\n" if @message
      message << "    Check the author and module names are correct."
    end
  end

end