Commit feec92ee4ebab38220c6a8577a47009b2f83beee

Authored by João M. M. da Silva + Paulo Meirelles
Committed by Paulo Meireles
1 parent 5f3b95e1

[Mezuro] refactoring Handling Exceptions

plugins/mezuro/controllers/mezuro_plugin_profile_controller.rb
1 class MezuroPluginProfileController < ProfileController 1 class MezuroPluginProfileController < ProfileController
2 2
3 append_view_path File.join(File.dirname(__FILE__) + '/../views') 3 append_view_path File.join(File.dirname(__FILE__) + '/../views')
4 - 4 +
  5 + rescue_from Exception do |exception|
  6 + message = URI.escape(CGI.escape(exception.message),'.')
  7 + redirect_to "/myprofile/#{profile.identifier}/plugin/mezuro/error_page?message=#{message}"
  8 + end
  9 +
  10 + def error_page
  11 + end
  12 +
5 def project_state 13 def project_state
6 @content = profile.articles.find(params[:id]) 14 @content = profile.articles.find(params[:id])
7 project = @content.project 15 project = @content.project
plugins/mezuro/lib/kalibro/configuration.rb
@@ -7,11 +7,7 @@ class Kalibro::Configuration &lt; Kalibro::Model @@ -7,11 +7,7 @@ class Kalibro::Configuration &lt; Kalibro::Model
7 end 7 end
8 8
9 def metric_configurations 9 def metric_configurations
10 - if @metric_configuration != nil  
11 - @metric_configuration  
12 - else  
13 - []  
14 - end 10 + @metric_configuration.nil? ? [] : @metric_configuration
15 end 11 end
16 12
17 def metric_configurations=(metric_configurations) 13 def metric_configurations=(metric_configurations)
@@ -19,19 +15,11 @@ class Kalibro::Configuration &lt; Kalibro::Model @@ -19,19 +15,11 @@ class Kalibro::Configuration &lt; Kalibro::Model
19 end 15 end
20 16
21 def self.find_by_name(configuration_name) 17 def self.find_by_name(configuration_name)
22 - begin  
23 - new request("Configuration", :get_configuration, {:configuration_name => configuration_name})[:configuration]  
24 - rescue Exception  
25 - nil  
26 - end 18 + new request("Configuration", :get_configuration, {:configuration_name => configuration_name})[:configuration]
27 end 19 end
28 20
29 def self.all_names 21 def self.all_names
30 - begin  
31 - request("Configuration", :get_configuration_names)[:configuration_name]  
32 - rescue Exception  
33 - []  
34 - end 22 + request("Configuration", :get_configuration_names)[:configuration_name]
35 end 23 end
36 24
37 def update_attributes(attributes={}) 25 def update_attributes(attributes={})
plugins/mezuro/lib/kalibro/error.rb
@@ -2,10 +2,6 @@ class Kalibro::Error &lt; Kalibro::Model @@ -2,10 +2,6 @@ class Kalibro::Error &lt; Kalibro::Model
2 2
3 attr_accessor :error_class, :message, :stack_trace_element, :cause 3 attr_accessor :error_class, :message, :stack_trace_element, :cause
4 4
5 - def initialize(exception)  
6 - @message = exception.message  
7 - end  
8 -  
9 def stack_trace_element=(value) 5 def stack_trace_element=(value)
10 @stack_trace_element = Kalibro::StackTraceElement.to_objects_array value 6 @stack_trace_element = Kalibro::StackTraceElement.to_objects_array value
11 end 7 end
plugins/mezuro/lib/kalibro/metric_configuration.rb
@@ -49,10 +49,14 @@ class Kalibro::MetricConfiguration &lt; Kalibro::Model @@ -49,10 +49,14 @@ class Kalibro::MetricConfiguration &lt; Kalibro::Model
49 end 49 end
50 50
51 def destroy 51 def destroy
52 - self.class.request("MetricConfiguration", :remove_metric_configuration, { 52 + begin
  53 + self.class.request("MetricConfiguration", :remove_metric_configuration, {
53 :configuration_name => configuration_name, 54 :configuration_name => configuration_name,
54 :metric_name=> metric.name 55 :metric_name=> metric.name
55 }) 56 })
  57 + rescue Exception => exception
  58 + add_error exception
  59 + end
56 end 60 end
57 61
58 def to_hash 62 def to_hash
plugins/mezuro/lib/kalibro/model.rb
@@ -10,6 +10,7 @@ class Kalibro::Model @@ -10,6 +10,7 @@ class Kalibro::Model
10 def to_hash(options={}) 10 def to_hash(options={})
11 hash = Hash.new 11 hash = Hash.new
12 excepts = !options[:except].nil? ? options[:except] : [] 12 excepts = !options[:except].nil? ? options[:except] : []
  13 + excepts << :errors
13 fields.each do |field| 14 fields.each do |field|
14 if(!excepts.include?(field)) 15 if(!excepts.include?(field))
15 field_value = send(field) 16 field_value = send(field)
plugins/mezuro/lib/kalibro/project.rb
@@ -19,19 +19,31 @@ class Kalibro::Project &lt; Kalibro::Model @@ -19,19 +19,31 @@ class Kalibro::Project &lt; Kalibro::Model
19 end 19 end
20 20
21 def process_project(days = '0') 21 def process_project(days = '0')
22 - if days.to_i.zero?  
23 - self.class.request("Kalibro", :process_project, {:project_name => name})  
24 - else  
25 - self.class.request("Kalibro", :process_periodically, {:project_name => name, :period_in_days => days})  
26 - end 22 + begin
  23 + if days.to_i.zero?
  24 + self.class.request("Kalibro", :process_project, {:project_name => name})
  25 + else
  26 + self.class.request("Kalibro", :process_periodically, {:project_name => name, :period_in_days => days})
  27 + end
  28 + rescue Exception => exception
  29 + add_error exception
  30 + end
27 end 31 end
28 32
29 def process_period 33 def process_period
30 - self.class.request("Kalibro", :get_process_period, {:project_name => name})[:period] 34 + begin
  35 + self.class.request("Kalibro", :get_process_period, {:project_name => name})[:period]
  36 + rescue Exception => exception
  37 + add_error exception
  38 + end
31 end 39 end
32 40
33 def cancel_periodic_process 41 def cancel_periodic_process
34 - self.class.request("Kalibro", :cancel_periodic_process, {:project_name => name}) 42 + begin
  43 + self.class.request("Kalibro", :cancel_periodic_process, {:project_name => name})
  44 + rescue Exception => exception
  45 + add_error exception
  46 + end
35 end 47 end
36 48
37 end 49 end
plugins/mezuro/test/unit/kalibro/configuration_test.rb
@@ -44,7 +44,9 @@ class ConfigurationTest &lt; ActiveSupport::TestCase @@ -44,7 +44,9 @@ class ConfigurationTest &lt; ActiveSupport::TestCase
44 should 'return nil when configuration doesnt exist' do 44 should 'return nil when configuration doesnt exist' do
45 request_body = {:configuration_name => @configuration.name} 45 request_body = {:configuration_name => @configuration.name}
46 Kalibro::Configuration.expects(:request).with("Configuration", :get_configuration, request_body).raises(Exception.new) 46 Kalibro::Configuration.expects(:request).with("Configuration", :get_configuration, request_body).raises(Exception.new)
47 - assert_nil Kalibro::Configuration.find_by_name(@configuration.name) 47 + assert_raise Exception do
  48 + Kalibro::Configuration.find_by_name(@configuration.name)
  49 + end
48 end 50 end
49 51
50 should 'destroy configuration by name' do 52 should 'destroy configuration by name' do