Exception: ServiceStack::WebServiceException

Inherits:
StandardError
  • Object
show all
Defined in:
lib/servicestack/web_service_exception.rb

Overview

Raised for failed API Requests, containing the HTTP Status Code and the structured ResponseStatus error when the Server returned one.

begin
client.send(CreateBooking.new)
rescue ServiceStack::WebServiceException => e
puts e.status_code           # 400
puts e.error_code            # "NotEmpty"
puts e.field_error('Name')   # "'Name' must not be empty."
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, status_code: 0, status_description: nil, response_status: nil, response_body: nil, inner_exception: nil) ⇒ WebServiceException

Returns a new instance of WebServiceException.



17
18
19
20
21
22
23
24
25
# File 'lib/servicestack/web_service_exception.rb', line 17

def initialize(message = nil, status_code: 0, status_description: nil, response_status: nil,
               response_body: nil, inner_exception: nil)
  super(message || status_description || "HTTP Error #{status_code}")
  @status_code = status_code
  @status_description = status_description
  @response_status = response_status
  @response_body = response_body
  @inner_exception = inner_exception
end

Instance Attribute Details

#inner_exceptionObject (readonly)

Returns the value of attribute inner_exception.



15
16
17
# File 'lib/servicestack/web_service_exception.rb', line 15

def inner_exception
  @inner_exception
end

#response_bodyObject (readonly)

Returns the value of attribute response_body.



15
16
17
# File 'lib/servicestack/web_service_exception.rb', line 15

def response_body
  @response_body
end

#response_statusObject (readonly)

Returns the value of attribute response_status.



15
16
17
# File 'lib/servicestack/web_service_exception.rb', line 15

def response_status
  @response_status
end

#status_codeObject (readonly)

Returns the value of attribute status_code.



15
16
17
# File 'lib/servicestack/web_service_exception.rb', line 15

def status_code
  @status_code
end

#status_descriptionObject (readonly)

Returns the value of attribute status_description.



15
16
17
# File 'lib/servicestack/web_service_exception.rb', line 15

def status_description
  @status_description
end

Instance Method Details

#error_codeObject

The ErrorCode of the error, e.g. "NotFound".



28
# File 'lib/servicestack/web_service_exception.rb', line 28

def error_code = @response_status&.error_code

#error_messageObject

The error message.



31
# File 'lib/servicestack/web_service_exception.rb', line 31

def error_message = @response_status&.message || message

#field_error(field_name) ⇒ Object

The validation error message for the field, if it has one.



37
# File 'lib/servicestack/web_service_exception.rb', line 37

def field_error(field_name) = @response_status&.field_error(field_name)

#field_errorsObject

Any field validation errors.



34
# File 'lib/servicestack/web_service_exception.rb', line 34

def field_errors = @response_status&.errors || []

#forbidden?Boolean

Returns:

  • (Boolean)


40
# File 'lib/servicestack/web_service_exception.rb', line 40

def forbidden? = @status_code == 403

#not_found?Boolean

Returns:

  • (Boolean)


41
# File 'lib/servicestack/web_service_exception.rb', line 41

def not_found? = @status_code == 404

#unauthorized?Boolean

Returns:

  • (Boolean)


39
# File 'lib/servicestack/web_service_exception.rb', line 39

def unauthorized? = @status_code == 401

#validation_error?Boolean

Returns:

  • (Boolean)


42
# File 'lib/servicestack/web_service_exception.rb', line 42

def validation_error? = !field_errors.empty?