Class: BugBunny::Middleware::RaiseError

Inherits:
Base
  • Object
show all
Defined in:
lib/bug_bunny/middleware/raise_error.rb

Overview

Middleware que inspecciona el status de la respuesta y lanza excepciones si se encuentran errores (4xx o 5xx).

Extrae inteligentemente el mensaje de error del cuerpo de la respuesta para que las excepciones tengan trazas claras y legibles, evitando el output crudo de Hashes en Ruby (‘{ “error” => … }`).

See Also:

Instance Attribute Summary

Attributes inherited from Base

#app

Instance Method Summary collapse

Methods inherited from Base

#call, #initialize

Constructor Details

This class inherits a constructor from BugBunny::Middleware::Base

Instance Method Details

#on_complete(response) ⇒ void

This method returns an undefined value.

Hook de ciclo de vida: Ejecutado después de recibir la respuesta.

Verifica el código de estado (status) de la respuesta. Si cae en el rango de éxito (2xx), permite que el flujo continúe. Si es un error, lo formatea y lanza la excepción semántica correspondiente.

Parameters:

  • response (Hash)

    El hash de respuesta conteniendo ‘status’ y ‘body’.

Raises:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/bug_bunny/middleware/raise_error.rb', line 32

def on_complete(response)
  status = response['status'].to_i
  body = response['body']

  case status
  when 200..299 then nil # Flujo normal (Success)
  when 400 then raise_typed(BugBunny::BadRequest.new(format_error_message(body)), status, body)
  when 404 then raise_not_found(status, body)
  when 406 then raise_typed(BugBunny::NotAcceptable.new, status, body)
  when 408 then raise_typed(BugBunny::RequestTimeout.new, status, body)
  when 409 then raise_typed(BugBunny::Conflict.new(format_error_message(body)), status, body)
  # Pasamos el body crudo; UnprocessableEntity lo procesará en exception.rb
  when 422 then raise_typed(BugBunny::UnprocessableEntity.new(body), status, body)
  when 500..599 then raise_server_error(status, body)
  else handle_unknown_error(status, body)
  end
end