initializing error classes more conventionally

This commit is contained in:
HoneyryderChuck 2021-06-30 01:32:31 +03:00
parent 6275108b49
commit 08fa918537
16 changed files with 35 additions and 35 deletions

View File

@ -32,7 +32,7 @@ module HTTPX
end
def shift!(fin)
@buffer = @buffer.byteslice(fin..-1)
@buffer = @buffer.byteslice(fin..-1) || "".b
end
end
end

View File

@ -19,7 +19,7 @@ module HTTPX
end
def emit(type, *args)
callbacks(type).delete_if { |pr| :delete == pr[*args] } # rubocop:disable Style/YodaCondition
callbacks(type).delete_if { |pr| :delete == pr.call(*args) } # rubocop:disable Style/YodaCondition
end
protected

View File

@ -34,11 +34,11 @@ module HTTPX
branch(default_options).wrap(&blk)
end
def plugin(*args, **opts, &blk)
def plugin(pl, options = nil, &blk)
klass = is_a?(Session) ? self.class : Session
klass = Class.new(klass)
klass.instance_variable_set(:@default_options, klass.default_options.merge(default_options))
klass.plugin(*args, **opts, &blk).new
klass.plugin(pl, options, &blk).new
end
# deprecated

View File

@ -69,10 +69,10 @@ module HTTPX
end
@inflight = 0
@keep_alive_timeout = options.timeout[:keep_alive_timeout]
@keep_alive_timeout = @options.timeout[:keep_alive_timeout]
@keep_alive_timer = nil
self.addresses = options.addresses if options.addresses
self.addresses = @options.addresses if @options.addresses
end
# this is a semi-private method, to be used by the resolver

View File

@ -11,7 +11,7 @@ module HTTPX
MAX_CONCURRENT_REQUESTS = HTTP2Next::DEFAULT_MAX_CONCURRENT_STREAMS
Error = Class.new(Error) do
class Error < Error
def initialize(id, code)
super("stream #{id} closed with error: #{code}")
end

View File

@ -1,11 +1,11 @@
# frozen_string_literal: true
module HTTPX
Error = Class.new(StandardError)
class Error < StandardError; end
UnsupportedSchemeError = Class.new(Error)
class UnsupportedSchemeError < Error; end
TimeoutError = Class.new(Error) do
class TimeoutError < Error
attr_reader :timeout
def initialize(timeout, message)
@ -20,17 +20,17 @@ module HTTPX
end
end
TotalTimeoutError = Class.new(TimeoutError)
class TotalTimeoutError < TimeoutError; end
ConnectTimeoutError = Class.new(TimeoutError)
class ConnectTimeoutError < TimeoutError; end
SettingsTimeoutError = Class.new(TimeoutError)
class SettingsTimeoutError < TimeoutError; end
ResolveTimeoutError = Class.new(TimeoutError)
class ResolveTimeoutError < TimeoutError; end
ResolveError = Class.new(Error)
class ResolveError < Error; end
NativeResolveError = Class.new(ResolveError) do
class NativeResolveError < ResolveError
attr_reader :connection, :host
def initialize(connection, host, message = "Can't resolve #{host}")
@ -40,7 +40,7 @@ module HTTPX
end
end
HTTPError = Class.new(Error) do
class HTTPError < Error
attr_reader :response
def initialize(response)
@ -53,5 +53,5 @@ module HTTPX
end
end
MisdirectedRequestError = Class.new(HTTPError)
class MisdirectedRequestError < HTTPError; end
end

View File

@ -4,7 +4,7 @@ require "openssl"
module HTTPX
class TLS < TCP
Error = Class.new(StandardError)
class Error < StandardError; end
def initialize(_, _, options)
super

View File

@ -2,7 +2,7 @@
module HTTPX
module Parser
Error = Class.new(Error)
class Error < Error; end
class HTTP1
VERSIONS = %w[1.0 1.1].freeze

View File

@ -4,7 +4,8 @@ require "resolv"
require "ipaddr"
module HTTPX
Socks4Error = Class.new(Error)
class Socks4Error < Error; end
module Plugins
module Proxy
module Socks4

View File

@ -1,7 +1,8 @@
# frozen_string_literal: true
module HTTPX
Socks5Error = Class.new(Error)
class Socks5Error < Error; end
module Plugins
module Proxy
module Socks5

View File

@ -31,7 +31,7 @@ module HTTPX
#
module Registry
# Base Registry Error
Error = Class.new(Error)
class Error < Error; end
def self.extended(klass)
super

View File

@ -204,7 +204,7 @@ module HTTPX
@buffer = Tempfile.new("httpx", encoding: Encoding::BINARY, mode: File::RDWR)
else
@state = :memory
@buffer = StringIO.new("".b, File::RDWR)
@buffer = StringIO.new("".b)
end
when :memory
if @length > @threshold_size

View File

@ -43,9 +43,6 @@ class HTTPX::Selector
private
READ_INTERESTS = %i[r rw].freeze
WRITE_INTERESTS = %i[w rw].freeze
def select_many(interval, &block)
selectables, r, w = nil
@ -64,8 +61,8 @@ class HTTPX::Selector
selectables.each do |io|
interests = io.interests
(r ||= []) << io if READ_INTERESTS.include?(interests)
(w ||= []) << io if WRITE_INTERESTS.include?(interests)
(r ||= []) << io if READABLE.include?(interests)
(w ||= []) << io if WRITABLE.include?(interests)
end
if @selectables.empty?

View File

@ -15,8 +15,6 @@ module HTTPX
end
def wrap
return unless block_given?
begin
prev_persistent = @persistent
@persistent = true
@ -31,6 +29,8 @@ module HTTPX
end
def request(*args, **options)
raise ArgumentError, "must perform at least one request" if args.empty?
requests = args.first.is_a?(Request) ? args : build_requests(*args, options)
responses = send_requests(*requests, options)
return responses.first if responses.size == 1
@ -283,8 +283,8 @@ module HTTPX
# :nocov:
def plugins(pls)
warn ":#{__method__} is deprecated, use :plugin instead"
pls.each do |pl, *args|
plugin(pl, *args)
pls.each do |pl|
plugin(pl)
end
self
end

View File

@ -4,7 +4,7 @@ require "forwardable"
module HTTPX::Transcoder
module Body
Error = Class.new(HTTPX::Error)
class Error < HTTPX::Error; end
module_function

View File

@ -4,7 +4,8 @@ require "forwardable"
module HTTPX::Transcoder
module Chunker
Error = Class.new(HTTPX::Error)
class Error < HTTPX::Error; end
CRLF = "\r\n".b
class Encoder