Skip to content

Commit

Permalink
Show HTTP response incase of error.
Browse files Browse the repository at this point in the history
Presently it only show the first response (which technically could be a redirect to an error response).
  • Loading branch information
sorbits committed Jul 30, 2011
1 parent 551de75 commit 3c04b1a
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions Commands/Paste Selection : Line Online….tmCommand
Expand Up @@ -162,7 +162,11 @@ URL_REGEXP = %r{
}xi
def connect_to_server(name, url, *args)
$status, res = :unknown, nil
$status, res, response = :unknown, nil, '???'
tmpFile = Tempfile.new('tm_paste_headers')
tmpFilePath = tmpFile.path.dup
tmpFile.close!
server = $1 if url =~ %r{.*?://(.*?)(/.*)?$}
TextMate.call_with_progress(:title => "Paste to #{name}", :message => "Contacting Host “#{server}”…", :cancel => lambda { $status = :cancel; Process.kill("INT", -Process.pid) }) do
Expand All @@ -174,7 +178,7 @@ def connect_to_server(name, url, *args)
pid = fork do
STDOUT.reopen(wr)
rd.close
exec('curl', url, '-sLo/dev/null', '-w%{url_effective}', *args)
exec('curl', url, "-D#{tmpFilePath}", '-sLo/dev/null', '-w%{url_effective}', *args)
end
Process.setpgid(0, oldpgid)
wr.close
Expand All @@ -187,8 +191,11 @@ def connect_to_server(name, url, *args)
thr.join
end
response = $1 if File.read(tmpFilePath) =~ /\AHTTP\/[0-9.]+ (.*?)\r?\n/
File.unlink(tmpFilePath)
$status = :error if res !~ URL_REGEXP || res == url # pastie returns the initial URL on some errors
return $status, res
return $status, res, response
end
def paste_stdin(priv, wrap)
Expand All @@ -209,7 +216,7 @@ def paste_stdin(priv, wrap)
ext = find_language_ext
start_time = Time.now
$status, res = connect_to_server("Pastie", PASTE_URL,
$status, res, response = connect_to_server("Pastie", PASTE_URL,
"-HExpect:",
"-Fpaste[parser]=plaintext",
"-Fpaste[restricted]=#{priv}",
Expand All @@ -223,25 +230,25 @@ def paste_stdin(priv, wrap)
open(File.expand_path('~/Library/Logs/Pastie.log'), 'a') do |io|
amount = (File.size(text_file.path) + File.size(html_file.path)).to_s.gsub(/\d{1,3}(?=(\d{3})+(?!\d))/, '\0,') + " bytes"
time_elapsed = '%.1f seconds' % (Time.now - start_time)
io << start_time.strftime('%F %T %Z: ') <<
io << start_time.strftime('%F %T %Z: ') << response << " " <<
case $status
when :timeout then "Timeout pasting #{amount} after #{time_elapsed}\n"
when :cancel then "User cancelled pasting of #{amount} after #{time_elapsed}\n"
when :error then "Unknown error after #{time_elapsed} pasting #{amount}\n"
when :error then "Error after #{time_elapsed} pasting #{amount}\n"
else "Pasted #{amount} in #{time_elapsed}: #{res}\n"
end
end
paste_via_rafb =
case $status
when :timeout then TextMate::UI.alert(:warning, "Timeout Pasting", "There was a problem pasting your text (timeout).\nWould you like to use RAFB.net instead?", "Paste via RAFB", "Cancel") != "Cancel"
when :error then TextMate::UI.alert(:warning, "Error Pasting", "There was a problem pasting your text (unknown).\nWould you like to use RAFB.net instead?", "Paste via RAFB", "Cancel") != "Cancel"
when :error then TextMate::UI.alert(:warning, "Error Pasting", "There was a problem pasting your text, pastie.org responded with #{response}\nWould you like to use RAFB.net instead?", "Paste via RAFB", "Cancel") != "Cancel"
else false
end
if paste_via_rafb
mode = $& if ENV['TM_MODE'] =~ /^(C89|C|C\+\+|C#|Java|Pascal|Perl|PHP|PL\/I|Python|Ruby|SQL|VB)$/
$status, res = connect_to_server("RAFB.net", "http://rafb.net/paste/paste.php",
$status, res, response = connect_to_server("RAFB.net", "http://rafb.net/paste/paste.php",
"-Fnick=#{ENV['USER']}",
"-Fcvt_tabs=#{ENV['TM_TAB_SIZE']}",
"-Ftext=<#{text_file.path}",
Expand Down

0 comments on commit 3c04b1a

Please sign in to comment.