From 060d0a43a78d759cf430e75b21ec2ee7d1dd5ed2 Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Wed, 4 Apr 2012 22:42:49 +0600 Subject: [PATCH] SONAR-2424 Fix possible NPE during export of profile Also provide generic handler of java.lang.IllegalArgumentException on Ruby side. --- .../java/org/sonar/server/rules/ProfilesConsole.java | 3 +++ .../main/java/org/sonar/server/ui/JRubyFacade.java | 3 +++ .../app/controllers/application_controller.rb | 12 +++++++++++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/sonar-server/src/main/java/org/sonar/server/rules/ProfilesConsole.java b/sonar-server/src/main/java/org/sonar/server/rules/ProfilesConsole.java index 6cbd6e6b266..16d5c5c65bd 100644 --- a/sonar-server/src/main/java/org/sonar/server/rules/ProfilesConsole.java +++ b/sonar-server/src/main/java/org/sonar/server/rules/ProfilesConsole.java @@ -110,6 +110,9 @@ public final class ProfilesConsole implements ServerComponent { RulesProfile profile = loadProfile(session, profileId); if (profile != null) { ProfileExporter exporter = getProfileExporter(exporterKey); + if (exporter == null) { + throw new IllegalArgumentException("No such exporter"); + } Writer writer = new StringWriter(); exporter.exportProfile(profile, writer); return writer.toString(); diff --git a/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java b/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java index 8c34f40739d..51372edf0d5 100644 --- a/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java +++ b/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java @@ -265,6 +265,9 @@ public final class JRubyFacade { return getContainer().getComponentByType(ProfilesConsole.class).getProfileImportersForLanguage(language); } + /** + * @throws IllegalArgumentException if no such exporter + */ public String exportProfile(int profileId, String exporterKey) { return getContainer().getComponentByType(ProfilesConsole.class).exportProfile(profileId, exporterKey); } diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/application_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/application_controller.rb index a705aed664d..47b683e68b1 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/application_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/application_controller.rb @@ -25,6 +25,7 @@ class ApplicationController < ActionController::Base before_filter :check_database_version, :set_locale, :check_authentication rescue_from Exception, :with => :render_error + rescue_from NativeException, :with => :render_native_exception rescue_from Errors::BadRequest, :with => :render_bad_request rescue_from ActionController::UnknownAction, :with => :render_not_found rescue_from ActionController::RoutingError, :with => :render_not_found @@ -131,7 +132,16 @@ class ApplicationController < ActionController::Base end def render_bad_request(error) - render :text => error.message, :status => 400 + message = error.respond_to?('message') ? error.message : error.to_s + render :text => message, :status => 400 + end + + def render_native_exception(error) + if error.cause.java_kind_of? Java::JavaLang::IllegalArgumentException + render_bad_request(error.cause.getMessage) + else + render_error(error) + end end def render_error(error) -- 2.39.5