Debian Codegraph @

plugin/bugreport/bugreport.rb

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 require 'net/http'
4
5 Plugin.create :bugreport do
6
7 @bugreport_uri = Diva::URI('https://mikutter.hachune.net/')
8
9 Delayer.new do |service|
10 popup if crashed_exception.is_a? Exception
11 rescue => e
12 # バグ報告中にバグで死んだらつらいもんな
13 error e
14 end
15
16 def popup
17 alert_thread = if(Thread.main != Thread.current) then Thread.current end
18 dialog = Gtk::Dialog.new(title: "bug report")
19 dialog.set_size_request(600, 400)
20 dialog.window_position = Gtk::WindowPosition::CENTER
21 dialog.child.pack_start(main, expand: true, fill: true, padding: 30)
22 dialog.add_button(Gtk::Stock::OK, Gtk::ResponseType::OK)
23 dialog.add_button(Gtk::Stock::CANCEL, Gtk::ResponseType::CANCEL)
24 dialog.default_response = Gtk::ResponseType::OK
25 quit = lambda{
26 dialog.hide.destroy
27 Gtk.main_iteration_do(false)
28 if alert_thread
29 alert_thread.run
30 else
31 Gtk.main_quit
32 end }
33 dialog.signal_connect("response"){ |widget, response|
34 if response == Gtk::ResponseType::OK
35 send
36 else
37 File.delete(File.expand_path(File.join(Environment::TMPDIR, 'mikutter_error'))) rescue nil
38 File.delete(File.expand_path(File.join(Environment::TMPDIR, 'crashed_exception'))) rescue nil end
39 quit.call }
40 dialog.signal_connect("destroy") {
41 false
42 }
43 dialog.show_all
44 if(alert_thread)
45 Thread.stop
46 else
47 Gtk::main
48 end
49 end
50
51 def imsorry
52 _("%{mikutter} が突然終了してしまったみたいで ヽ('ω')ノ三ヽ('ω')ノもうしわけねぇもうしわけねぇ")%{mikutter: Environment::NAME}+"\n"+
53 _('OKボタンを押したら、自動的に以下のテキストが送られます。これがバグを直すのにとっても役に立つんですよ。よかったら送ってくれません?')
54 end
55
56 def main
57 sw = Gtk::ScrolledWindow.new
58 sw.set_policy :never, :always
59 sw.vexpand = true
60 sw.valign = :fill
61 sw << Gtk::IntelligentTextview.new(backtrace)
62
63 sorry = Gtk::IntelligentTextview.new(imsorry)
64 sorry.hexpand = true
65
66 grid = Gtk::Grid.new
67 grid.orientation = :vertical
68
69 grid << sorry << sw
70 end
71
72 def send
73 Thread.new do
74 exception = crashed_exception
75 m = exception.backtrace.first.match(/(.+?):(\d+)/)
76 crashed_file, crashed_line = m[1], m[2]
77 param = {
78 'backtrace' => JSON.generate(exception.backtrace.map{ |msg| msg.gsub(FOLLOW_DIR, '{MIKUTTER_DIR}') }),
79 'file' => crashed_file.gsub(FOLLOW_DIR, '{MIKUTTER_DIR}'),
80 'line' => crashed_line,
81 'exception_class' => exception.class,
82 'description' => exception.to_s,
83 'ruby_version' => RUBY_VERSION,
84 'rubygtk_version' => GLib::BINDING_VERSION.join('.'),
85 'platform' => RUBY_PLATFORM,
86 'url' => 'exception',
87 'version' => Environment::VERSION
88 }
89 case exception
90 when TypeStrictError
91 param['causing_value'] = exception.value
92 end
93 http = Net::HTTP.new(@bugreport_uri.host, @bugreport_uri.port)
94 http.use_ssl = @bugreport_uri.scheme == 'https'
95
96 req = Net::HTTP::Post.new(@bugreport_uri.path)
97 req.set_form_data(param)
98 res = http.request(req)
99
100 File.delete(File.expand_path(File.join(Environment::TMPDIR, 'mikutter_error'))) rescue nil
101 File.delete(File.expand_path(File.join(Environment::TMPDIR, 'crashed_exception'))) rescue nil
102 Plugin.activity :system, _("エラー報告を送信しました。ありがとう♡")
103 Plugin.call :send_bugreport, param
104 rescue Timeout::Error, StandardError => e
105 Plugin.activity :system, _("ピャアアアアアアアアアアアアアアアアアアアアアアアwwwwwwwwwwwwwwwwwwwwww")
106 Plugin.activity :error, e.to_s, exception: e
107 end
108 end
109
110 def backtrace
111 "#{crashed_exception.class} #{crashed_exception.to_s}\n" +
112 crashed_exception.backtrace.map{ |msg| msg.gsub(FOLLOW_DIR, '{MIKUTTER_DIR}') }.join("\n")
113 end
114
115 def crashed_exception
116 @crashed_exception ||= object_get_contents(File.expand_path(File.join(Environment::TMPDIR, 'crashed_exception'))) rescue nil
117 end
118
119 end