Skip to content

Commit 7186191

Browse files
GRAILS-10845 - controller exception handler methods need to be non-private
1 parent 8f797d1 commit 7186191

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

grails-plugin-controllers/src/main/groovy/org/codehaus/groovy/grails/compiler/web/ControllerActionTransformer.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,12 @@ public void performInjectionOnAnnotatedClass(SourceUnit source, ClassNode classN
206206

207207
private boolean isExceptionHandlingMethod(MethodNode methodNode) {
208208
boolean isExceptionHandler = false;
209-
Parameter[] parameters = methodNode.getParameters();
210-
if(parameters.length == 1) {
211-
ClassNode parameterTypeClassNode = parameters[0].getType();
212-
isExceptionHandler = parameterTypeClassNode.isDerivedFrom(new ClassNode(Exception.class));
209+
if(!methodNode.isPrivate()) {
210+
Parameter[] parameters = methodNode.getParameters();
211+
if(parameters.length == 1) {
212+
ClassNode parameterTypeClassNode = parameters[0].getType();
213+
isExceptionHandler = parameterTypeClassNode.isDerivedFrom(new ClassNode(Exception.class));
214+
}
213215
}
214216
return isExceptionHandler;
215217
}

grails-test-suite-web/src/test/groovy/org/codehaus/groovy/grails/web/controllers/ControllerExceptionHandlerSpec.groovy

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import grails.test.mixin.TestFor
66
import java.sql.BatchUpdateException
77
import java.sql.SQLException
88

9+
import javax.xml.soap.SOAPException
10+
911
import spock.lang.Issue
1012
import spock.lang.Specification
1113

@@ -93,6 +95,24 @@ class ControllerExceptionHandlerSpec extends Specification {
9395
model.problemDescription == 'A Number Was Invalid'
9496
}
9597

98+
void 'Test throwing an exception that does not have a handler'() {
99+
when:
100+
params.exceptionToThrow = 'javax.xml.soap.SOAPException'
101+
def model = controller.testActionWithNonCommandObjectParameter()
102+
103+
then:
104+
thrown SOAPException
105+
}
106+
107+
void 'Test throwing an exception that does not have a handler and does match a private method in the parent controller'() {
108+
when: 'a controller action throws an exception which matches an inherited private method which should not be treated as an exception handler'
109+
params.exceptionToThrow = 'java.io.IOException'
110+
def model = controller.testActionWithNonCommandObjectParameter()
111+
112+
then: 'the method is ignored and the exception is thrown'
113+
thrown IOException
114+
}
115+
96116
void 'Test action throws an exception that does not have a corresponding error handler'() {
97117
when:
98118
params.exceptionToThrow = 'java.lang.UnsupportedOperationException'
@@ -132,7 +152,14 @@ class ControllerExceptionHandlerSpec extends Specification {
132152
}
133153

134154
@Artefact('Controller')
135-
class ErrorHandlersController {
155+
abstract class SomeAbstractController {
156+
157+
private somePrivateMethodWhichIsNotAnExceptionHandler(IOException e) {
158+
}
159+
}
160+
161+
@Artefact('Controller')
162+
class ErrorHandlersController extends SomeAbstractController {
136163

137164
def testAction() {
138165
def exceptionClass = Class.forName(params.exceptionToThrow)

0 commit comments

Comments
 (0)