```ruby
module Pliny::Middleware
  class RescueErrors
    def initialize(app, options = {})
      @app = app
      @raise = options[:raise]
    end

    def call(env)
      error = nil
      @app.call(env)
    rescue Pliny::Errors::Error => e
      error = e
      Pliny::Errors::Error.render(e)
    rescue => e
      error = e
      raise if @raise

      Pliny::ErrorReporters.notify(e, rack_env: env)
      Pliny::Errors::Error.render(Pliny::Errors::InternalServerError.new)
    ensure
      # Leave this in env for CanonicalLogLine.
      env["pliny.error"] = e
    end
  end
end
```

Refactor to better modularize setting error in env
user
```ruby
module Pliny::Middleware
  class RescueErrors
    def initialize(app, options = {})
      @app = app
      @raise = options[:raise]
    end

    def call(env)
      @app.call(env)
    rescue Pliny::Errors::Error => e
      set_error_in_env(env, e)
      Pliny::Errors::Error.render(e)
    rescue => e
      set_error_in_env(env, e)
      raise if @raise

      Pliny::ErrorReporters.notify(e, rack_env: env)
      Pliny::Errors::Error.render(Pliny::Errors::InternalServerError.new)
    end

    # Sets the error in a predefined env key for use by the upstream
    # CanonicalLogLine middleware.
    def set_error_in_env(env, e)
      env["pliny.error"] = e
    end
  end
end
```
assistant