lulalala
lulalala

Reputation: 17981

Rails rescue inactive or unreachable

I have the following code in Rails routing:

    class LegacyRedirector
      InvalidUrlError = Class.new(ActionController::RoutingError)

      def call(params, request)
        URI.parse(request.path)
      rescue URI::InvalidURIError => e
        raise InvalidUrlError.new(e.message)
      end
    end

# routes.rb
match 'a', to: redirect(LegacyRedirector.new)

This is for catching invaild URLs. However when I test it in browser for curl, it would still display URI::InvalidURIError, not my new error class. It seems the rescue block was never reached, yet I am sure I am rescuing the correct type. How can that be?

URI::InvalidURIError at /twitter/typeahead-js/wikis/home[
=========================================================

> bad URI(is not URI?): "/twitter/typeahead-js/wikis/home["

lib/gitlab/routing.rb, line 31
------------------------------

``` ruby
>  31           URI.parse(request.path)
   32         rescue URI::InvalidURIError => e
   33           raise InvalidUrlError.new(e.message)
   34         end
```

Upvotes: 0

Views: 128

Answers (1)

lulalala
lulalala

Reputation: 17981

One possible cause could be better_errors.

If an error is raised in a rescue block, its cause would be the original error. better_errors displays that cause instead, meaning the backtrace will not be in the rescue block. This gives you the illusion that it is never rescued.

This was recently fixed, see https://github.com/BetterErrors/better_errors/pull/459 for more details

Upvotes: 1

Related Questions