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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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
    nil # Flujo normal (Success)
  when 400
    raise BugBunny::BadRequest, format_error_message(body)
  when 404
    raise_not_found(body)
  when 406
    raise BugBunny::NotAcceptable
  when 408
    raise BugBunny::RequestTimeout
  when 409
    raise BugBunny::Conflict, format_error_message(body)
  when 422
    # Pasamos el body crudo; UnprocessableEntity lo procesará en exception.rb
    raise BugBunny::UnprocessableEntity, body
  when 500..599
    if body.is_a?(Hash) && body['bug_bunny_exception']
      exception_data = body['bug_bunny_exception']
      raise BugBunny::RemoteError.new(
        exception_data['class'],
        exception_data['message'],
        exception_data['backtrace'] || []
      )
    end
    raise BugBunny::InternalServerError, format_error_message(body)
  else
    handle_unknown_error(status, body)
  end
end