net-http icon indicating copy to clipboard operation
net-http copied to clipboard

Set correct uplevel to warning on supply_default_content_type

Open pocke opened this issue 3 years ago • 1 comments

This PR fixes uplevel of supply_default_content_type.

Problem

Currently supply_default_content_type specifies an incorrect uplevel to warn method. For example:

require 'net/http'

url = URI.parse('http://www.example.com/index.html')
req = Net::HTTP::Post.new(url.path)
res = Net::HTTP.start(url.host, url.port) {|http|
  http.request(req)
}
res.body
$ ruby test.rb
/Users/kuwabara.masataka/.rbenv/versions/trunk/lib/ruby/3.2.0+0/net/http/generic_request.rb:186: warning: net/http: Content-Type did not set; using application/x-www-form-urlencoded

The warning points to net/http/generic_request.rb which is not written by the user. The location is not helpful to fix the warning by the user.

Solution

Find the correct uplevel from the caller and specify it.

I also considered using a fixed uplevel for it, like uplevel: 6. But I think caller_location is better than the fixed value. Because the uplevel is too deep, and supply_default_content_type is called from multiple places so the fixed value doesn't work properly for all callers perhaps.

pocke avatar Apr 01 '22 04:04 pocke

I think a possible direction is to:

  1. Make InputMethod act as IOMethod. That is, make it responsible for output too (by adding puts and print methods to it?) as Reline/ReadlineInputMethod both already hold @stdout anyway.
  2. Change all Kernel#puts invocation to irb_context.io.puts (with some refactors, ofc).

st0012 avatar Aug 07 '23 15:08 st0012