Class: Cosmos::ExceptionDialog

Inherits:
Object
  • Object
show all
Defined in:
lib/cosmos/gui/dialogs/exception_dialog.rb

Constant Summary collapse

@@mutex =
Mutex.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, exception, title = 'COSMOS Exception', exit_afterwards = true, log_exception = true, log_file = nil) ⇒ ExceptionDialog

Returns a new instance of ExceptionDialog.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/cosmos/gui/dialogs/exception_dialog.rb', line 22

def initialize(parent, exception, title = 'COSMOS Exception', exit_afterwards = true, log_exception = true, log_file = nil)
  if @@mutex.try_lock
    unless exception.class == SystemExit or exception.class == Interrupt
      # ConfigParser::Errors are configuration user errors. We don't want to clutter
      # up list of real exceptions with user configuration errors.
      # Same goes for FatalErrors
      unless exception.class == ConfigParser::Error || exception.class == FatalError
        log_file = Cosmos.write_exception_file(exception) if log_exception
      end

      if Qt::CoreApplication.instance
        msg = Qt::MessageBox.new(parent)
        msg.setWindowTitle(title)
        msg.setTextFormat(Qt::RichText)
        msg.setIcon(Qt::MessageBox::Critical)
        case exception
        # ConfigParser::Errors are a special case generated with a known format
        # by the ConfigParser
        when ConfigParser::Error
          # Substitute the html tags '<' and '>' and then replace newlines with html breaks
          usage = exception.usage.gsub("<","&#060;").gsub(">","&#062;").gsub("\n","<br/>")
          message = exception.message.gsub("<","&#060;").gsub(">","&#062;").gsub("\n","<br/>")
          line = exception.keyword + ' ' + exception.parameters.join(' ').gsub("<","&#060;").gsub(">","&#062;").gsub("\n","<br/>")
          text = "Error in #{exception.filename}<br/><br/>Line #{exception.line_number}: #{line}<br/><br/>#{message}<br/><br/>#{usage}"
          unless exception.url.nil?
            text << "<br/><br/>For more information see <a href='#{exception.url}'>#{exception.url}</a>."
          end
        # FatalErrors are errors explicitly raised when a known fatal issue
        # occurs. Since it is a known issue we don't put up the full error
        # dialog.
        when FatalError
          text = "Error: #{exception.message.gsub("\n","<br/>")}"
        else
          file_contents = ""
          # First read the log_file we wrote out to the logs directory
          # Change newlines to %0A for Outlook and remove all quotes to avoid breaking the link
          begin
            message = exception.message.gsub("\n","<br/>")
            file_contents = File.read(log_file).gsub("\n","%0A").gsub("'","").gsub("\"","") if log_file
          rescue
          end
          text = "The following error occurred:<br/>#{message}"
          text << "<br/><br/>Please contact your local COSMOS expert.<br/><br/>This error has been logged:<br/>#{log_file}<br/><br/> <a href='mailto:[email protected];[email protected]?subject=COSMOS exception&body=#{file_contents}'>Click here</a> to email this log to the COSMOS developers." if log_file
        end
        text << "<br/><br/>NOTE!: The application will exit once you accept or dismiss this dialog!" if exit_afterwards
        msg.setText(text)
        if log_file
          open_button = Qt::PushButton.new("Open Exception Log in Text Editor")
          open_button.connect(SIGNAL('clicked()')) do
            Cosmos.open_in_text_editor(log_file)
            sleep(2)
          end
          msg.addButton(open_button, Qt::MessageBox::ResetRole)
        end
        close_button = Qt::PushButton.new("Close")
        msg.addButton(close_button, Qt::MessageBox::ActionRole)
        msg.raise
        msg.exec
        msg.dispose
      end # if Qt::CoreApplication.instance
    end #  unless exception.class == SystemExit or exception.class == Interrupt
    @@mutex.unlock
    if exit_afterwards
      Qt::CoreApplication.instance.exit(1) if Qt::CoreApplication.instance
      exit 1 # incase CoreApplication doesn't exit yet or didn't complete the exit
    end
  end # if @@mutex.try_lock
end

Class Method Details

.dialog_open?Boolean

def initialize

Returns:

  • (Boolean)


91
92
93
# File 'lib/cosmos/gui/dialogs/exception_dialog.rb', line 91

def self.dialog_open?
  @@mutex.locked?
end