Module: Rapid::Server::Helpers::Error

Defined in:
lib/ury_rapid/server/helpers/error.rb

Overview

Sinatra helpers for sending error messages to the client

Instance Method Summary collapse

Instance Method Details

#client_error(message) ⇒ void

This method returns an undefined value.

Flags a client error

Parameters:

  • message (String)

    The error message.


25
26
27
# File 'lib/ury_rapid/server/helpers/error.rb', line 25

def client_error(message)
  error(400, message)
end

#error(code, message) ⇒ void

This method returns an undefined value.

Halts with an error status code and message

Parameters:

  • code (Integer)

    The HTTP status code to return.

  • message (String)

    A human-readable message to show the client.


40
41
42
# File 'lib/ury_rapid/server/helpers/error.rb', line 40

def error(code, message)
  halt(code, render_error(code, message))
end

#not_supported(exception) ⇒ Object

Halts due to an operation not being supported


30
31
32
# File 'lib/ury_rapid/server/helpers/error.rb', line 30

def not_supported(exception)
  error(405, exception.to_s)
end

#render_error(code, message) ⇒ String

Renders an error message

Parameters:

  • code (Integer)

    The HTTP status code to return.

  • message (String)

    A human-readable message to show the client.

Returns:

  • (String)

    The error message, rendered according to the client's Accept headers.


51
52
53
54
55
56
57
58
59
# File 'lib/ury_rapid/server/helpers/error.rb', line 51

def render_error(code, message)
  # This is a very hacky way of making respond_with resolve the correct
  # format for our error message, while stopping it from halting with
  # a 200 status code (we need to specify our own status code).
  catch(:halt) do
    respond_with(:error,
                 status: :error, error: message, http_code: code)
  end
end

#wrapObject

Wraps a block in various common error handlers


10
11
12
13
14
15
16
17
18
# File 'lib/ury_rapid/server/helpers/error.rb', line 10

def wrap
  yield
rescue Kankri::InsufficientPrivilegeError
  error(403, 'Forbidden.')
rescue Common::Exceptions::MissingResource
  error(404, 'Not found.')
rescue Common::Exceptions::NotSupported => e
  not_supported(e)
end