Exception: WeChat::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/we_chat/error.rb

Overview

Custom error class for rescuing from all WeChat errors

Constant Summary collapse

ClientError =

Raised when WeChat returns a 4xx HTTP status code

Class.new(self)
BadRequest =

Raised when WeChat returns the HTTP status code 400

Class.new(ClientError)
Unauthorized =

Raised when WeChat returns the HTTP status code 401

Class.new(ClientError)
Forbidden =

Raised when WeChat returns the HTTP status code 403

Class.new(ClientError)
AlreadyFavorited =

Raised when a Tweet has already been favorited

Class.new(Forbidden)
AlreadyRetweeted =

Raised when a Tweet has already been retweeted

Class.new(Forbidden)
DuplicateStatus =

Raised when a Tweet has already been posted

Class.new(Forbidden)
NotFound =

Raised when WeChat returns the HTTP status code 404

Class.new(ClientError)
NotAcceptable =

Raised when WeChat returns the HTTP status code 406

Class.new(ClientError)
UnprocessableEntity =

Raised when WeChat returns the HTTP status code 422

Class.new(ClientError)
TooManyRequests =

Raised when WeChat returns the HTTP status code 429

Class.new(ClientError)
ServerError =

Raised when WeChat returns a 5xx HTTP status code

Class.new(self)
InternalServerError =

Raised when WeChat returns the HTTP status code 500

Class.new(ServerError)
BadGateway =

Raised when WeChat returns the HTTP status code 502

Class.new(ServerError)
ServiceUnavailable =

Raised when WeChat returns the HTTP status code 503

Class.new(ServerError)
GatewayTimeout =

Raised when WeChat returns the HTTP status code 504

Class.new(ServerError)
ERRORS =
{
  400 => WeChat::Error::BadRequest,
  401 => WeChat::Error::Unauthorized,
  403 => WeChat::Error::Forbidden,
  404 => WeChat::Error::NotFound,
  406 => WeChat::Error::NotAcceptable,
  422 => WeChat::Error::UnprocessableEntity,
  429 => WeChat::Error::TooManyRequests,
  500 => WeChat::Error::InternalServerError,
  502 => WeChat::Error::BadGateway,
  503 => WeChat::Error::ServiceUnavailable,
  504 => WeChat::Error::GatewayTimeout,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = '', headers = {}, code = nil) ⇒ WeChat::Error

Initializes a new Error object

Parameters:

  • message (Exception, String) (defaults to: '')
  • rate_limit (Hash)
  • code (Integer) (defaults to: nil)


100
101
102
103
# File 'lib/we_chat/error.rb', line 100

def initialize(message = '', headers = {}, code = nil)
  super(message)
  @code = code
end

Instance Attribute Details

#codeInteger (readonly)

Returns:

  • (Integer)


5
6
7
# File 'lib/we_chat/error.rb', line 5

def code
  @code
end

Class Method Details

.from_response(body, headers) ⇒ WeChat::Error

Create a new error from an HTTP response

Parameters:

  • body (String)
  • headers (Hash)

Returns:



76
77
78
79
# File 'lib/we_chat/error.rb', line 76

def from_response(body, headers)
  message, code = parse_error(body)
  new(message, headers, code)
end