Mange / roadie

Making HTML emails comfortable for the Ruby rockstars

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

URI::InvalidComponentError in MailPreview

PikachuEXE opened this issue · comments

I am using mail_preview for email template preview
But sometimes (not always) it gives this error:

URI::InvalidComponentError - bad component(expected scheme component): http://:
  /usr/local/rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/uri/generic.rb:330:in `check_scheme'
  /usr/local/rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/uri/generic.rb:371:in `scheme='
  /usr/local/rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/uri/generic.rb:193:in `initialize'
  /usr/local/rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/uri/generic.rb:141:in `build'
  (gem) roadie-2.3.4/lib/roadie/inliner.rb:192:in `absolute_url_base'
  (gem) roadie-2.3.4/lib/roadie/inliner.rb:179:in `ensure_absolute_url'
  (gem) roadie-2.3.4/lib/roadie/inliner.rb:167:in `block in make_image_urls_absolute'
  (gem) nokogiri-1.5.9/lib/nokogiri/xml/node_set.rb:239:in `block in each'
  (gem) nokogiri-1.5.9/lib/nokogiri/xml/node_set.rb:238:in `each'
  (gem) roadie-2.3.4/lib/roadie/inliner.rb:166:in `make_image_urls_absolute'
  (gem) roadie-2.3.4/lib/roadie/inliner.rb:55:in `block in execute'
  (gem) roadie-2.3.4/lib/roadie/inliner.rb:77:in `block in adjust_html'
  (gem) roadie-2.3.4/lib/roadie/inliner.rb:76:in `adjust_html'
  (gem) roadie-2.3.4/lib/roadie/inliner.rb:49:in `execute'
  (gem) roadie-2.3.4/lib/roadie.rb:6:in `inline_css'
  (gem) roadie-2.3.4/lib/roadie/action_mailer_extensions.rb:46:in `inline_style_response'
  (gem) roadie-2.3.4/lib/roadie/action_mailer_extensions.rb:33:in `block in collect_responses_and_parts_order_with_inline_styles'
  (gem) roadie-2.3.4/lib/roadie/action_mailer_extensions.rb:33:in `collect_responses_and_parts_order_with_inline_styles'
  (gem) actionmailer-3.2.12/lib/action_mailer/base.rb:648:in `mail'
  (gem) roadie-2.3.4/lib/roadie/action_mailer_extensions.rb:25:in `mail_with_inline_styles'
  app/mailers/employer_mailer.rb:10:in `welcome'
  (gem) actionpack-3.2.12/lib/abstract_controller/base.rb:167:in `process_action'
  (gem) actionpack-3.2.12/lib/abstract_controller/base.rb:121:in `process'
  (gem) actionpack-3.2.12/lib/abstract_controller/rendering.rb:45:in `process'
  (gem) actionmailer-3.2.12/lib/action_mailer/base.rb:458:in `process'
  (gem) actionmailer-3.2.12/lib/action_mailer/base.rb:452:in `initialize'
  (gem) actionmailer-3.2.12/lib/action_mailer/base.rb:439:in `method_missing'
  app/mailers/mail_preview.rb:3:in `welcome_employer'
  (gem) mail_view-1.0.3/lib/mail_view.rb:38:in `call'
  (gem) mail_view-1.0.3/lib/mail_view.rb:19:in `call'
  (gem) journey-1.0.4/lib/journey/router.rb:68:in `block in call'
  (gem) journey-1.0.4/lib/journey/router.rb:56:in `call'
  (gem) actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:601:in `call'
  (gem) rack-pjax-0.7.0/lib/rack/pjax.rb:12:in `call'
  (gem) omniauth-1.1.4/lib/omniauth/strategy.rb:184:in `call!'
  (gem) omniauth-1.1.4/lib/omniauth/strategy.rb:164:in `call'
  (gem) omniauth-1.1.4/lib/omniauth/strategy.rb:184:in `call!'
  (gem) omniauth-1.1.4/lib/omniauth/strategy.rb:164:in `call'
  (gem) meta_request-0.2.3/lib/meta_request/middlewares/app_request_handler.rb:11:in `call'
  (gem) rack-contrib-1.1.0/lib/rack/contrib/response_headers.rb:17:in `call'
  (gem) meta_request-0.2.3/lib/meta_request/middlewares/headers.rb:16:in `call'
  (gem) meta_request-0.2.3/lib/meta_request/middlewares/meta_request_handler.rb:13:in `call'
  (gem) bullet-4.5.0/lib/bullet/rack.rb:11:in `call'
  (gem) newrelic_rpm-3.6.0.83/lib/new_relic/rack/error_collector.rb:12:in `call'
  (gem) newrelic_rpm-3.6.0.83/lib/new_relic/rack/agent_hooks.rb:18:in `call'
  (gem) newrelic_rpm-3.6.0.83/lib/new_relic/rack/browser_monitoring.rb:16:in `call'
  (gem) rack-google-analytics-0.11.0/lib/rack/google-analytics.rb:18:in `_call'
  (gem) rack-google-analytics-0.11.0/lib/rack/google-analytics.rb:15:in `call'
  (gem) warden-1.2.1/lib/warden/manager.rb:35:in `block in call'
  (gem) warden-1.2.1/lib/warden/manager.rb:34:in `call'
  (gem) actionpack-3.2.12/lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
  (gem) rack-1.4.5/lib/rack/etag.rb:23:in `call'
  (gem) rack-1.4.5/lib/rack/conditionalget.rb:25:in `call'
  (gem) actionpack-3.2.12/lib/action_dispatch/middleware/head.rb:14:in `call'
  (gem) remotipart-1.0.5/lib/remotipart/middleware.rb:30:in `call'
  (gem) actionpack-3.2.12/lib/action_dispatch/middleware/params_parser.rb:21:in `call'
  (gem) actionpack-3.2.12/lib/action_dispatch/middleware/flash.rb:242:in `call'
  (gem) rack-1.4.5/lib/rack/session/abstract/id.rb:210:in `context'
  (gem) rack-1.4.5/lib/rack/session/abstract/id.rb:205:in `call'
  (gem) actionpack-3.2.12/lib/action_dispatch/middleware/cookies.rb:341:in `call'
  (gem) activerecord-3.2.12/lib/active_record/query_cache.rb:64:in `call'
  (gem) activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
  (gem) actionpack-3.2.12/lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
  (gem) activesupport-3.2.12/lib/active_support/callbacks.rb:405:in `_run__415732725367540646__call__2241787683446459159__callbacks'
  (gem) activesupport-3.2.12/lib/active_support/callbacks.rb:405:in `__run_callback'
  (gem) activesupport-3.2.12/lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
  (gem) activesupport-3.2.12/lib/active_support/callbacks.rb:81:in `run_callbacks'
  (gem) actionpack-3.2.12/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  (gem) actionpack-3.2.12/lib/action_dispatch/middleware/reloader.rb:65:in `call'
  (gem) actionpack-3.2.12/lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
  (gem) airbrake-3.1.11/lib/airbrake/rails/middleware.rb:13:in `call'
  (gem) better_errors-0.8.0/lib/better_errors/middleware.rb:84:in `protected_app_call'
  (gem) better_errors-0.8.0/lib/better_errors/middleware.rb:79:in `better_errors_call'
  (gem) better_errors-0.8.0/lib/better_errors/middleware.rb:56:in `call'
  (gem) actionpack-3.2.12/lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
  (gem) actionpack-3.2.12/lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  (gem) railties-3.2.12/lib/rails/rack/logger.rb:32:in `call_app'
  (gem) railties-3.2.12/lib/rails/rack/logger.rb:16:in `block in call'
  (gem) activesupport-3.2.12/lib/active_support/tagged_logging.rb:22:in `tagged'
  (gem) railties-3.2.12/lib/rails/rack/logger.rb:16:in `call'
  (gem) quiet_assets-1.0.2/lib/quiet_assets.rb:18:in `call_with_quiet_assets'
  (gem) actionpack-3.2.12/lib/action_dispatch/middleware/request_id.rb:22:in `call'
  (gem) rack-1.4.5/lib/rack/methodoverride.rb:21:in `call'
  (gem) rack-1.4.5/lib/rack/runtime.rb:17:in `call'
  (gem) activesupport-3.2.12/lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  (gem) rack-1.4.5/lib/rack/lock.rb:15:in `call'
  (gem) rack-livereload-0.3.13/lib/rack/livereload.rb:60:in `_call'
  (gem) rack-livereload-0.3.13/lib/rack/livereload.rb:51:in `call'
  (gem) actionpack-3.2.12/lib/action_dispatch/middleware/static.rb:62:in `call'
  (gem) airbrake-3.1.11/lib/airbrake/user_informer.rb:16:in `_call'
  (gem) airbrake-3.1.11/lib/airbrake/user_informer.rb:12:in `call'
  (gem) railties-3.2.12/lib/rails/engine.rb:479:in `call'
  (gem) railties-3.2.12/lib/rails/application.rb:223:in `call'
  (gem) railties-3.2.12/lib/rails/railtie/configurable.rb:30:in `method_missing'
  (gem) unicorn-4.6.2/lib/unicorn/http_server.rb:552:in `process_client'
  (gem) unicorn-4.6.2/lib/unicorn/http_server.rb:632:in `worker_loop'
  (gem) newrelic_rpm-3.6.0.83/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb:22:in `block (4 levels) in <top (required)>'
  (gem) unicorn-4.6.2/lib/unicorn/http_server.rb:500:in `spawn_missing_workers'
  (gem) unicorn-4.6.2/lib/unicorn/http_server.rb:142:in `start'
  (gem) unicorn-4.6.2/bin/unicorn_rails:209:in `<top (required)>'
  /usr/local/rvm/gems/ruby-2.0.0-p0/bin/unicorn_rails:23:in `<main>'
  /usr/local/rvm/gems/ruby-2.0.0-p0/bin/ruby_noexec_wrapper:14:in `<main>'

It is very strange that it only happen sometimes, not always

As you can see, it crashes on an URL that starts with "http://:" which isn't valid. My guess is that there's something wrong with the host inlining, so the full URL looks like "http://:3000". This could, in turn, happen because of some autoload issue with your configuration which would explain why it happens occasionally.

Roadie should not output invalid URLs so this is a bug with the gem if my guess is correct.

Oh the scheme was http:// not http://:
And the @url_options was: (saw from better_errors)

{:host=>"localhost", :port=>3000, :protocol=>"http://"}

That should be

{:host => "localhost", :port => 3000, :protocol => "http"}

I based my guess from the error message:

URI::InvalidComponentError - bad component(expected scheme component): http://:

(emphasis mine)

Because when I am using mail_preview the url_options method always return http:// for protocol (browsing through ActionController)
Sending mails in console does generate the correct link with protocol (http)

Now I am using this as workaround:

class BaseMailer


  private

    def default_url_options
      super.merge(protocol: 'http')
    end
end

I don't know what is the right way to solve it permanently

I cannot find any place where mail_view would set or change the default URL options when reading the source code. Perhaps Rails is doing that automatically because there's an actual request when the email is rendered (I find no evidence for this either) or you might have some other gem doing something.

How are you configuring your options? Are you using A Howard's "Default url options" gem?

I think it is the actionmailer_with_request gem overriding the protocol
I am using it since I might use cells with routes in ActionMailer

I think I will just remove it until I really have that kind of usage

Yeah, that seems like a likely culprit. I'm closing this for now.

I think it is the actionmailer_with_request gem overriding the protocol

I don't think so. The actionmailer_with_request simply acts as a proxy, passing the options it receives as input.
Something else at lower level is setting the protocol to a bad value.

Okay, that is making things more complex for sure. Rails does not appear to have any documentation on what the different keys are supposed to contain, so it hard to know what's supposed to work and what's not supposed to work. I guess I can make a quick patch that just fixes this problem.

This is released in 2.4.0.

I confirm that it's working now :)
Thank you~

commented

{:host => "localhost", :port => 3000, :protocol => "http"}
It's helpful!