Commit d04295bc6b0ba83a9463de4ad711fe13d1311eb1
Committed by
Paulo Meireles
1 parent
cde54408
Exists in
master
and in
29 other branches
[Mezuro] Handling exceptions in models, controllers and views
Showing
9 changed files
with
92 additions
and
45 deletions
Show diff stats
plugins/mezuro/controllers/mezuro_plugin_myprofile_controller.rb
| ... | ... | @@ -4,7 +4,7 @@ class MezuroPluginMyprofileController < ProfileController |
| 4 | 4 | |
| 5 | 5 | rescue_from Exception do |exception| |
| 6 | 6 | message = URI.escape(CGI.escape(exception.message),'.') |
| 7 | - redirect_to "/myprofile/#{profile.identifier}/plugin/mezuro/error_page?message=#{message}" | |
| 7 | + redirect_to_error_page message | |
| 8 | 8 | end |
| 9 | 9 | |
| 10 | 10 | def error_page |
| ... | ... | @@ -31,6 +31,7 @@ class MezuroPluginMyprofileController < ProfileController |
| 31 | 31 | def new_compound_metric_configuration |
| 32 | 32 | @configuration_content = profile.articles.find(params[:id]) |
| 33 | 33 | @metric_configurations = @configuration_content.metric_configurations |
| 34 | + look_for_configuration_content_errors | |
| 34 | 35 | end |
| 35 | 36 | |
| 36 | 37 | def edit_metric_configuration |
| ... | ... | @@ -45,7 +46,7 @@ class MezuroPluginMyprofileController < ProfileController |
| 45 | 46 | @metric_configurations = @configuration_content.metric_configurations |
| 46 | 47 | @metric = @metric_configuration.metric |
| 47 | 48 | end |
| 48 | - | |
| 49 | + | |
| 49 | 50 | def create_metric_configuration |
| 50 | 51 | id = params[:id] |
| 51 | 52 | metric_name = params[:metric_configuration][:metric][:name] |
| ... | ... | @@ -56,7 +57,9 @@ class MezuroPluginMyprofileController < ProfileController |
| 56 | 57 | def create_compound_metric_configuration |
| 57 | 58 | id = params[:id] |
| 58 | 59 | metric_name = params[:metric_configuration][:metric][:name] |
| 59 | - Kalibro::MetricConfiguration.new(params[:metric_configuration]).save | |
| 60 | + metric_configuration = Kalibro::MetricConfiguration.new(params[:metric_configuration]) | |
| 61 | + metric_configuration.save | |
| 62 | + look_for_model_error metric_configuration | |
| 60 | 63 | redirect_to "/myprofile/#{profile.identifier}/plugin/mezuro/edit_compound_metric_configuration?id=#{id}&metric_name=#{metric_name.gsub(/\s/, '+')}" |
| 61 | 64 | end |
| 62 | 65 | |
| ... | ... | @@ -65,6 +68,7 @@ class MezuroPluginMyprofileController < ProfileController |
| 65 | 68 | metric_name = params[:metric_configuration][:metric][:name] |
| 66 | 69 | metric_configuration = Kalibro::MetricConfiguration.find_by_configuration_name_and_metric_name(@configuration_content.name, metric_name) |
| 67 | 70 | metric_configuration.update_attributes params[:metric_configuration] |
| 71 | + look_for_model_error metric_configuration | |
| 68 | 72 | redirect_to "/#{profile.identifier}/#{@configuration_content.slug}" |
| 69 | 73 | end |
| 70 | 74 | |
| ... | ... | @@ -73,6 +77,7 @@ class MezuroPluginMyprofileController < ProfileController |
| 73 | 77 | metric_name = params[:metric_configuration][:metric][:name] |
| 74 | 78 | metric_configuration = Kalibro::MetricConfiguration.find_by_configuration_name_and_metric_name(@configuration_content.name, metric_name) |
| 75 | 79 | metric_configuration.update_attributes params[:metric_configuration] |
| 80 | + look_for_model_error metric_configuration | |
| 76 | 81 | redirect_to "/#{profile.identifier}/#{@configuration_content.slug}" |
| 77 | 82 | end |
| 78 | 83 | |
| ... | ... | @@ -81,6 +86,7 @@ class MezuroPluginMyprofileController < ProfileController |
| 81 | 86 | metric_name = params[:metric_name] |
| 82 | 87 | metric_configuration = Kalibro::MetricConfiguration.find_by_configuration_name_and_metric_name(configuration_content.name, metric_name) |
| 83 | 88 | metric_configuration.destroy |
| 89 | + look_for_model_error metric_configuration | |
| 84 | 90 | redirect_to "/#{profile.identifier}/#{configuration_content.slug}" |
| 85 | 91 | end |
| 86 | 92 | |
| ... | ... | @@ -105,6 +111,7 @@ class MezuroPluginMyprofileController < ProfileController |
| 105 | 111 | metric_configuration = Kalibro::MetricConfiguration.find_by_configuration_name_and_metric_name(@configuration_content.name, metric_name) |
| 106 | 112 | metric_configuration.add_range(@range) |
| 107 | 113 | metric_configuration.save |
| 114 | + look_for_model_error metric_configuration | |
| 108 | 115 | end |
| 109 | 116 | |
| 110 | 117 | def update_range |
| ... | ... | @@ -115,6 +122,7 @@ class MezuroPluginMyprofileController < ProfileController |
| 115 | 122 | index = metric_configuration.ranges.index{ |range| range.beginning == beginning_id.to_f || beginning_id == "-INF" } |
| 116 | 123 | metric_configuration.ranges[index] = Kalibro::Range.new params[:range] |
| 117 | 124 | metric_configuration.save |
| 125 | + look_for_model_error metric_configuration | |
| 118 | 126 | end |
| 119 | 127 | |
| 120 | 128 | def remove_range |
| ... | ... | @@ -124,6 +132,7 @@ class MezuroPluginMyprofileController < ProfileController |
| 124 | 132 | metric_configuration = Kalibro::MetricConfiguration.find_by_configuration_name_and_metric_name(configuration_content.name, metric_name) |
| 125 | 133 | metric_configuration.ranges.delete_if { |range| range.beginning == beginning_id.to_f || beginning_id == "-INF" } |
| 126 | 134 | metric_configuration.save |
| 135 | + | |
| 127 | 136 | formatted_metric_name = metric_name.gsub(/\s/, '+') |
| 128 | 137 | if metric_configuration.metric.class == Kalibro::CompoundMetric |
| 129 | 138 | redirect_to "/myprofile/#{profile.identifier}/plugin/mezuro/edit_compound_metric_configuration?id=#{configuration_content.id}&metric_name=#{formatted_metric_name}" |
| ... | ... | @@ -132,4 +141,18 @@ class MezuroPluginMyprofileController < ProfileController |
| 132 | 141 | end |
| 133 | 142 | end |
| 134 | 143 | |
| 144 | + private | |
| 145 | + | |
| 146 | + def redirect_to_error_page(message) | |
| 147 | + redirect_to "/myprofile/#{profile.identifier}/plugin/mezuro/error_page?message=#{message}" | |
| 148 | + end | |
| 149 | + | |
| 150 | + def look_for_configuration_content_errors | |
| 151 | + redirect_to_error_page(@configuration_content.errors[:base]) if not @configuration_content.errors.nil? | |
| 152 | + end | |
| 153 | + | |
| 154 | + def look_for_model_error(model) | |
| 155 | + redirect_to_error_page(model.errors[0].message) if not model.errors.empty? | |
| 156 | + end | |
| 157 | + | |
| 135 | 158 | end | ... | ... |
plugins/mezuro/controllers/mezuro_plugin_profile_controller.rb
| ... | ... | @@ -2,11 +2,6 @@ class MezuroPluginProfileController < ProfileController |
| 2 | 2 | |
| 3 | 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_error_page message | |
| 8 | - end | |
| 9 | - | |
| 10 | 5 | def error_page |
| 11 | 6 | @message = params[:message] |
| 12 | 7 | end |
| ... | ... | @@ -14,52 +9,79 @@ class MezuroPluginProfileController < ProfileController |
| 14 | 9 | def project_state |
| 15 | 10 | @content = profile.articles.find(params[:id]) |
| 16 | 11 | project = @content.project |
| 17 | - redirect_to_error_page(project.errors[0].message) if not project.errors.empty? | |
| 18 | - state = project.kalibro_error.nil? ? project.state : "ERROR" | |
| 19 | - render :text => state | |
| 12 | + if project_content_errors? | |
| 13 | + redirect_to_error_page(@content.errors[:base]) | |
| 14 | + else | |
| 15 | + state = project.kalibro_error.nil? ? project.state : "ERROR" | |
| 16 | + render :text => state | |
| 17 | + end | |
| 20 | 18 | end |
| 21 | 19 | |
| 22 | 20 | def project_error |
| 23 | 21 | @content = profile.articles.find(params[:id]) |
| 24 | 22 | @project = @content.project |
| 25 | - render :partial => 'content_viewer/project_error' | |
| 23 | + if project_content_errors? | |
| 24 | + redirect_to_error_page(@content.errors[:base]) | |
| 25 | + else | |
| 26 | + render :partial => 'content_viewer/project_error' | |
| 27 | + end | |
| 26 | 28 | end |
| 27 | 29 | |
| 28 | 30 | def project_result |
| 29 | 31 | @content = profile.articles.find(params[:id]) |
| 30 | 32 | date = params[:date] |
| 31 | 33 | @project_result = date.nil? ? @content.project_result : @content.project_result_with_date(date) |
| 32 | - render :partial => 'content_viewer/project_result' | |
| 34 | + if project_content_errors? | |
| 35 | + redirect_to_error_page(@content.errors[:base]) | |
| 36 | + else | |
| 37 | + render :partial => 'content_viewer/project_result' | |
| 38 | + end | |
| 33 | 39 | end |
| 34 | 40 | |
| 35 | 41 | def module_result |
| 36 | 42 | @content = profile.articles.find(params[:id]) |
| 37 | 43 | @module_result = @content.module_result(params) |
| 38 | - render :partial => 'content_viewer/module_result' | |
| 44 | + if project_content_errors? | |
| 45 | + redirect_to_error_page(@content.errors[:base]) | |
| 46 | + else | |
| 47 | + render :partial => 'content_viewer/module_result' | |
| 48 | + end | |
| 39 | 49 | end |
| 40 | 50 | |
| 41 | 51 | def project_tree |
| 42 | 52 | @content = profile.articles.find(params[:id]) |
| 43 | 53 | date = params[:date] |
| 44 | 54 | project_result = date.nil? ? @content.project_result : @content.project_result_with_date(date) |
| 45 | - @project_name = @content.project.name | |
| 46 | - @source_tree = project_result.node_of(params[:module_name]) | |
| 47 | - render :partial =>'content_viewer/source_tree' | |
| 55 | + @project_name = @content.project.name if not @content.project.nil? | |
| 56 | + if project_content_errors? | |
| 57 | + redirect_to_error_page(@content.errors[:base]) | |
| 58 | + else | |
| 59 | + @source_tree = project_result.node_of(params[:module_name]) | |
| 60 | + render :partial =>'content_viewer/source_tree' | |
| 61 | + end | |
| 48 | 62 | end |
| 49 | 63 | |
| 50 | 64 | def module_metrics_history |
| 51 | 65 | metric_name = params[:metric_name] |
| 52 | 66 | @content = profile.articles.find(params[:id]) |
| 53 | 67 | module_history = @content.result_history(params[:module_name]) |
| 54 | - @score_history = filtering_metric_history(metric_name, module_history) | |
| 55 | - render :partial => 'content_viewer/score_history' | |
| 68 | + if project_content_errors? | |
| 69 | + redirect_to_error_page(@content.errors[:base]) | |
| 70 | + else | |
| 71 | + @score_history = filtering_metric_history(metric_name, module_history) | |
| 72 | + render :partial => 'content_viewer/score_history' | |
| 73 | + end | |
| 56 | 74 | end |
| 57 | 75 | |
| 58 | 76 | def module_grade_history |
| 59 | 77 | @content = profile.articles.find(params[:id]) |
| 60 | 78 | modules_results = @content.result_history(params[:module_name]) |
| 61 | - @score_history = modules_results.collect { |module_result| module_result.grade } | |
| 62 | - render :partial => 'content_viewer/score_history' | |
| 79 | + if project_content_errors? | |
| 80 | + redirect_to_error_page(@content.errors[:base]) | |
| 81 | + else | |
| 82 | + @score_history = modules_results.collect { |module_result| module_result.grade } | |
| 83 | + render :partial => 'content_viewer/score_history' | |
| 84 | + end | |
| 63 | 85 | end |
| 64 | 86 | |
| 65 | 87 | private |
| ... | ... | @@ -82,5 +104,8 @@ class MezuroPluginProfileController < ProfileController |
| 82 | 104 | redirect_to "/profile/#{profile.identifier}/plugin/mezuro/error_page?message=#{message}" |
| 83 | 105 | end |
| 84 | 106 | |
| 107 | + def project_content_errors? | |
| 108 | + not @content.errors.nil? | |
| 109 | + end | |
| 85 | 110 | end |
| 86 | 111 | ... | ... |
plugins/mezuro/lib/mezuro_plugin/configuration_content.rb
| ... | ... | @@ -35,7 +35,12 @@ class MezuroPlugin::ConfigurationContent < Article |
| 35 | 35 | end |
| 36 | 36 | |
| 37 | 37 | def kalibro_configuration_names |
| 38 | - ["None"] + Kalibro::Configuration.all_names.sort | |
| 38 | + begin | |
| 39 | + ["None"] + Kalibro::Configuration.all_names.sort | |
| 40 | + rescue Exception => exception | |
| 41 | + errors.add_to_base(exception.message) | |
| 42 | + ["None"] | |
| 43 | + end | |
| 39 | 44 | end |
| 40 | 45 | |
| 41 | 46 | private | ... | ... |
plugins/mezuro/lib/mezuro_plugin/project_content.rb
| ... | ... | @@ -25,8 +25,8 @@ class MezuroPlugin::ProjectContent < Article |
| 25 | 25 | @project ||= Kalibro::Project.find_by_name(name) |
| 26 | 26 | rescue Exception => error |
| 27 | 27 | errors.add_to_base(error.message) |
| 28 | - @project | |
| 29 | 28 | end |
| 29 | + @project | |
| 30 | 30 | end |
| 31 | 31 | |
| 32 | 32 | def project_result |
| ... | ... | @@ -35,6 +35,7 @@ class MezuroPlugin::ProjectContent < Article |
| 35 | 35 | rescue Exception => error |
| 36 | 36 | errors.add_to_base(error.message) |
| 37 | 37 | end |
| 38 | + @project_result | |
| 38 | 39 | end |
| 39 | 40 | |
| 40 | 41 | def project_result_with_date(date) |
| ... | ... | @@ -44,6 +45,7 @@ Kalibro::ProjectResult.first_result_after(name, date) |
| 44 | 45 | rescue Exception => error |
| 45 | 46 | errors.add_to_base(error.message) |
| 46 | 47 | end |
| 48 | + @project_result | |
| 47 | 49 | end |
| 48 | 50 | |
| 49 | 51 | def module_result(attributes) |
| ... | ... | @@ -52,8 +54,9 @@ Kalibro::ProjectResult.first_result_after(name, date) |
| 52 | 54 | begin |
| 53 | 55 | @module_result ||= Kalibro::ModuleResult.find_by_project_name_and_module_name_and_date(name, module_name, date) |
| 54 | 56 | rescue Exception => error |
| 55 | - raise error | |
| 57 | + errors.add_to_base(error.message) | |
| 56 | 58 | end |
| 59 | + @module_result | |
| 57 | 60 | end |
| 58 | 61 | |
| 59 | 62 | def result_history(module_name) |
| ... | ... | @@ -74,6 +77,7 @@ Kalibro::ProjectResult.first_result_after(name, date) |
| 74 | 77 | existing = Kalibro::Project.all_names |
| 75 | 78 | rescue Exception => error |
| 76 | 79 | errors.add_to_base(error.message) |
| 80 | + existing = [] | |
| 77 | 81 | end |
| 78 | 82 | |
| 79 | 83 | if existing.any?{|existing_name| existing_name.casecmp(name)==0} # existing.include?(name) + case insensitive | ... | ... |
plugins/mezuro/test/functional/mezuro_plugin_myprofile_controller_test.rb
| ... | ... | @@ -39,7 +39,7 @@ class MezuroPluginMyprofileControllerTest < ActionController::TestCase |
| 39 | 39 | @range = RangeFixtures.range_excellent |
| 40 | 40 | @range_hash = RangeFixtures.range_excellent_hash |
| 41 | 41 | end |
| 42 | - | |
| 42 | + | |
| 43 | 43 | should 'test choose base tool' do |
| 44 | 44 | Kalibro::BaseTool.expects(:request).with("BaseTool", :get_base_tool_names).returns({:base_tool_name => @base_tool.name}) |
| 45 | 45 | get :choose_base_tool, :profile => @profile.identifier, :id => @content.id | ... | ... |
plugins/mezuro/test/functional/mezuro_plugin_profile_controller_test.rb
| ... | ... | @@ -25,13 +25,6 @@ class MezuroPluginProfileControllerTest < ActionController::TestCase |
| 25 | 25 | @content.save |
| 26 | 26 | end |
| 27 | 27 | |
| 28 | - should 'show an error page if an exception is raised' #do | |
| 29 | -# Kalibro::Project.expects(:request).with("Project", :get_project, :project_name => @project.name).raises(Exception, "Error message") | |
| 30 | -# get :project_state, :profile => @profile.identifier, :id => @content.id | |
| 31 | -# assert_response 302 | |
| 32 | -# assert_select('h2', 'An error occured: ') | |
| 33 | -# end | |
| 34 | - | |
| 35 | 28 | should 'test project state without kalibro_error' do |
| 36 | 29 | Kalibro::Project.expects(:request).with("Project", :get_project, :project_name => @project.name).returns({:project => @project.to_hash}) |
| 37 | 30 | get :project_state, :profile => @profile.identifier, :id => @content.id | ... | ... |
plugins/mezuro/views/cms/mezuro_plugin/_configuration_content.html.erb
| 1 | 1 | <h1> <%= _(MezuroPlugin::ConfigurationContent.short_description) %> </h1> |
| 2 | 2 | |
| 3 | 3 | <% |
| 4 | - begin | |
| 5 | - kalibro_configuration = @article.title.nil? ? nil : @article.kalibro_configuration | |
| 6 | - rescue | |
| 7 | - kalibro_configuration = nil | |
| 8 | - end | |
| 4 | + kalibro_configuration = @article.title.nil? ? nil : @article.kalibro_configuration | |
| 5 | + kalibro_configuration_names = @article.kalibro_configuration_names | |
| 9 | 6 | %> |
| 10 | 7 | |
| 11 | 8 | <%= error_messages_for 'kalibro_configuration' %> |
| ... | ... | @@ -13,8 +10,7 @@ |
| 13 | 10 | <%= hidden_field_tag 'kalibro_configuration[profile_id]', profile.id %> |
| 14 | 11 | <%= hidden_field_tag 'id', @article.id %> |
| 15 | 12 | |
| 16 | -<% kalibro_configuration_names = @article.kalibro_configuration_names %> | |
| 17 | - | |
| 13 | + | |
| 18 | 14 | <% selected = (kalibro_configuration.nil? ? "None" : @article.configuration_to_clone_name) %> |
| 19 | 15 | |
| 20 | 16 | <%= required_fields_message %> | ... | ... |
plugins/mezuro/views/cms/mezuro_plugin/_project_content.html.erb
| 1 | 1 | <h1> <%= _(MezuroPlugin::ProjectContent.short_description) %> </h1> |
| 2 | 2 | |
| 3 | 3 | <% |
| 4 | + @project = @article.title.nil? ? nil : @article.project | |
| 4 | 5 | begin |
| 5 | - @project = @article.title.nil? ? nil : Kalibro::Project.find_by_name(@article.title) | |
| 6 | - rescue | |
| 7 | - @project = nil | |
| 6 | + @repository_types = Kalibro::Repository.repository_types.sort | |
| 7 | + @configuration_names = Kalibro::Configuration.all_names.sort | |
| 8 | + rescue Exception => exception | |
| 9 | + @article.errors.add_to_base(exception.message) | |
| 10 | + @repository_types = [] | |
| 11 | + @configuration_names = [] | |
| 8 | 12 | end |
| 9 | 13 | %> |
| 10 | 14 | |
| ... | ... | @@ -24,14 +28,12 @@ |
| 24 | 28 | |
| 25 | 29 | <%= f.text_field :description %><br/> |
| 26 | 30 | |
| 27 | -<% @repository_types = Kalibro::Repository.repository_types.sort %> | |
| 28 | 31 | <% @selected = (@project.nil? ? @repository_types : @project.repository.type) %> |
| 29 | 32 | <%= required labelled_form_field _('Repository type'), |
| 30 | 33 | f.select(:repository_type, @repository_types, {:selected => @selected}) %><br/> |
| 31 | 34 | |
| 32 | 35 | <%= required f.text_field(:repository_url) %><br/> |
| 33 | 36 | |
| 34 | -<% @configuration_names = Kalibro::Configuration.all_names.sort %> | |
| 35 | 37 | <% @selected = (@project.nil? ? @configuration_names[0] : @project.configuration_name) %> |
| 36 | 38 | |
| 37 | 39 | <% if !@project.nil? && !@article.id.nil? %> | ... | ... |
plugins/mezuro/views/mezuro_plugin_profile/error_page.html.erb