Sha256: 3f5dfa137721d62f731a0e15061d6e2f45a02246f3ae6d607fdcb9f27d2f23dc

Contents?: true

Size: 1.07 KB

Versions: 49

Compression:

Stored size: 1.07 KB

Contents

package_require("ree_errors/error")
package_require("ree_mapper/errors/type_error")

class ReeRoda::App < Roda
  include Ree::LinkDSL

  link :logger, from: :ree_logger
  link :status_from_error
  link :to_json, from: :ree_json

  plugin :error_handler
  plugin :json_parser
  plugin :type_routing, default_type: :json

  error do |e|
    response["Content-Type"] = "application/json"

    if e.is_a?(ReeErrors::Error)
      body = {
        code: e.code,
        message: e.message,
        type: e.type,
      }

      response.status = status_from_error(e.type)
      response.write(to_json(body))
      response.finish
    elsif e.is_a?(ReeMapper::TypeError) || e.is_a?(ReeMapper::CoercionError)
      body = {
        code: "param",
        message: e.message,
        type: :invalid_param,
      }

      response.status = 400
      response.write(to_json(body))
      response.finish
    else
      logger.error(e.message, {}, e)
      response["Content-Type"] = "text/plain"
      response.status = 500
      response.write("unhandled server error")
      response.finish
    end
  end
end

Version data entries

49 entries across 49 versions & 1 rubygems

Version Path
ree_lib-1.0.85 lib/ree_lib/packages/ree_roda/package/ree_roda/app.rb
ree_lib-1.0.84 lib/ree_lib/packages/ree_roda/package/ree_roda/app.rb
ree_lib-1.0.83 lib/ree_lib/packages/ree_roda/package/ree_roda/app.rb
ree_lib-1.0.82 lib/ree_lib/packages/ree_roda/package/ree_roda/app.rb
ree_lib-1.0.81 lib/ree_lib/packages/ree_roda/package/ree_roda/app.rb
ree_lib-1.0.80 lib/ree_lib/packages/ree_roda/package/ree_roda/app.rb
ree_lib-1.0.79 lib/ree_lib/packages/ree_roda/package/ree_roda/app.rb
ree_lib-1.0.78 lib/ree_lib/packages/ree_roda/package/ree_roda/app.rb
ree_lib-1.0.77 lib/ree_lib/packages/ree_roda/package/ree_roda/app.rb
ree_lib-1.0.76 lib/ree_lib/packages/ree_roda/package/ree_roda/app.rb
ree_lib-1.0.75 lib/ree_lib/packages/ree_roda/package/ree_roda/app.rb
ree_lib-1.0.74 lib/ree_lib/packages/ree_roda/package/ree_roda/app.rb
ree_lib-1.0.73 lib/ree_lib/packages/ree_roda/package/ree_roda/app.rb
ree_lib-1.0.72 lib/ree_lib/packages/ree_roda/package/ree_roda/app.rb
ree_lib-1.0.71 lib/ree_lib/packages/ree_roda/package/ree_roda/app.rb
ree_lib-1.0.70 lib/ree_lib/packages/ree_roda/package/ree_roda/app.rb
ree_lib-1.0.69 lib/ree_lib/packages/ree_roda/package/ree_roda/app.rb
ree_lib-1.0.68 lib/ree_lib/packages/ree_roda/package/ree_roda/app.rb
ree_lib-1.0.67 lib/ree_lib/packages/ree_roda/package/ree_roda/app.rb
ree_lib-1.0.66 lib/ree_lib/packages/ree_roda/package/ree_roda/app.rb