Exception: Poodle::NetworkError

Inherits:
Error
  • Object
show all
Defined in:
lib/poodle/errors/network_error.rb

Overview

Exception raised when network or HTTP errors occur

Examples:

Handling network errors

begin
  client.send_email(email)
rescue Poodle::NetworkError => e
  puts "Network error: #{e.message}"
  puts "Original error: #{e.original_error}" if e.original_error
end

Instance Attribute Summary collapse

Attributes inherited from Error

#context, #status_code

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Error

#message, #to_s

Constructor Details

#initialize(message = "Network error occurred", original_error: nil, context: {}, status_code: nil) ⇒ NetworkError

Initialize a new NetworkError

Parameters:

  • message (String) (defaults to: "Network error occurred")

    the error message

  • original_error (Exception, nil) (defaults to: nil)

    the original exception

  • context (Hash) (defaults to: {})

    additional context information

  • status_code (Integer, nil) (defaults to: nil)

    HTTP status code



25
26
27
28
# File 'lib/poodle/errors/network_error.rb', line 25

def initialize(message = "Network error occurred", original_error: nil, context: {}, status_code: nil)
  @original_error = original_error
  super(message, context: context, status_code: status_code)
end

Instance Attribute Details

#original_errorException? (readonly)

Returns the original exception that caused this error.

Returns:

  • (Exception, nil)

    the original exception that caused this error



17
18
19
# File 'lib/poodle/errors/network_error.rb', line 17

def original_error
  @original_error
end

Class Method Details

.connection_failed(url, original_error: nil) ⇒ NetworkError

Create a NetworkError for connection failure

Parameters:

  • url (String)

    the URL that failed to connect

  • original_error (Exception, nil) (defaults to: nil)

    the original exception

Returns:



47
48
49
50
51
52
53
# File 'lib/poodle/errors/network_error.rb', line 47

def self.connection_failed(url, original_error: nil)
  new(
    "Failed to connect to #{url}",
    original_error: original_error,
    context: { url: url, error_type: "connection_failed" }
  )
end

.connection_timeout(timeout) ⇒ NetworkError

Create a NetworkError for connection timeout

Parameters:

  • timeout (Integer)

    the timeout duration

Returns:



34
35
36
37
38
39
40
# File 'lib/poodle/errors/network_error.rb', line 34

def self.connection_timeout(timeout)
  new(
    "Connection timeout after #{timeout} seconds",
    context: { timeout: timeout, error_type: "connection_timeout" },
    status_code: 408
  )
end

.dns_resolution_failed(host) ⇒ NetworkError

Create a NetworkError for DNS resolution failure

Parameters:

  • host (String)

    the host that failed to resolve

Returns:



59
60
61
62
63
64
# File 'lib/poodle/errors/network_error.rb', line 59

def self.dns_resolution_failed(host)
  new(
    "DNS resolution failed for host: #{host}",
    context: { host: host, error_type: "dns_resolution_failed" }
  )
end

.http_error(status_code, message = "") ⇒ NetworkError

Create a NetworkError for HTTP errors

Parameters:

  • status_code (Integer)

    the HTTP status code

  • message (String) (defaults to: "")

    the error message

Returns:



82
83
84
85
86
87
88
89
90
91
# File 'lib/poodle/errors/network_error.rb', line 82

def self.http_error(status_code, message = "")
  default_message = "HTTP error occurred with status code: #{status_code}"
  final_message = message.empty? ? default_message : message

  new(
    final_message,
    context: { error_type: "http_error" },
    status_code: status_code
  )
end

.malformed_response(response = "") ⇒ NetworkError

Create a NetworkError for malformed response

Parameters:

  • response (String) (defaults to: "")

    the malformed response

Returns:



97
98
99
100
101
102
# File 'lib/poodle/errors/network_error.rb', line 97

def self.malformed_response(response = "")
  new(
    "Received malformed response from server",
    context: { response: response, error_type: "malformed_response" }
  )
end

.ssl_error(message) ⇒ NetworkError

Create a NetworkError for SSL/TLS errors

Parameters:

  • message (String)

    the SSL error message

Returns:



70
71
72
73
74
75
# File 'lib/poodle/errors/network_error.rb', line 70

def self.ssl_error(message)
  new(
    "SSL/TLS error: #{message}",
    context: { error_type: "ssl_error" }
  )
end