Skip to content

Commit 11532c4

Browse files
authored
Merge pull request #16879 from codeconsole/fix/observe-failed-action-8.0.x
8.0 (fix): Report a failed action's exception and route on the HTTP server observation
2 parents 5a27a30 + a06354e commit 11532c4

34 files changed

Lines changed: 2740 additions & 75 deletions

File tree

‎grails-core/src/main/groovy/org/grails/core/DefaultGrailsControllerClass.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.lang.invoke.MethodHandle;
2222
import java.lang.invoke.MethodHandles;
2323
import java.lang.invoke.MethodType;
24+
import java.lang.reflect.InvocationTargetException;
2425
import java.lang.reflect.Method;
2526
import java.lang.reflect.Modifier;
2627
import java.util.ArrayList;
@@ -212,7 +213,13 @@ public ReflectionInvoker(Method method) {
212213

213214
@Override
214215
public Object invoke(Object controller) throws Throwable {
215-
return method.invoke(controller);
216+
try {
217+
return method.invoke(controller);
218+
}
219+
catch (InvocationTargetException e) {
220+
// what a method handle throws, so that what an action fails with does not depend on the environment
221+
throw e.getTargetException();
222+
}
216223
}
217224
}
218225

‎grails-core/src/test/groovy/org/grails/core/DefaultGrailsControllerClassSpec.groovy‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package org.grails.core
2020

2121
import grails.config.Settings
2222
import grails.core.DefaultGrailsApplication
23+
import grails.web.Action
2324
import spock.lang.Specification
2425

2526
/**
@@ -81,6 +82,27 @@ class DefaultGrailsControllerClassSpec extends Specification {
8182
configScopeValue << [SINGLETON, SESSION]
8283
}
8384

85+
void "an action that fails throws its own exception when it is invoked #invocation"() {
86+
when:
87+
new DefaultGrailsControllerClass(controller).invoke(controller.newInstance(), 'failing')
88+
89+
then:
90+
def exception = thrown(IllegalStateException)
91+
exception.message == 'thrown by the action'
92+
93+
where:
94+
invocation | controller
95+
'through a method handle' | MethodHandleInvokedController
96+
'by reflection, as in development mode' | Class.forName('org.grails.core.nonpublic.ReflectivelyInvokedController')
97+
}
98+
99+
static class MethodHandleInvokedController {
100+
@Action
101+
void failing() {
102+
throw new IllegalStateException('thrown by the action')
103+
}
104+
}
105+
84106
class NotSpecifiedController {
85107
}
86108

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.grails.core.nonpublic
21+
22+
import groovy.transform.PackageScope
23+
24+
import grails.web.Action
25+
26+
/**
27+
* A class in another package than {@code DefaultGrailsControllerClass} and not public, so it cannot look up a method
28+
* handle for the action and invokes it by reflection, which is how it invokes every action in development mode.
29+
*/
30+
@PackageScope
31+
class ReflectivelyInvokedController {
32+
33+
@Action
34+
void failing() {
35+
throw new IllegalStateException('thrown by the action')
36+
}
37+
}

‎grails-doc/src/en/guide/upgrading/upgrading80x.adoc‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4952,3 +4952,14 @@ The `-XX:+/-UseCompressedClassPointers` flag is deprecated in Java 25 and become
49524952
Dropping it changes the memory layout, and from JDK 27 compressed class pointers are always on, whether or not the flag is there.
49534953

49544954
See <<java25-memory,Metaspace and Off-Heap Memory>> in the deployment guide for full details, including a systemd service unit example.
4955+
4956+
==== 92. An Exception an Action Throws Is Recorded on the HTTP Server Observation
4957+
4958+
As of Grails 8.0.1, an exception an action throws is recorded as an error on the observation of the request.
4959+
4960+
* When an error view answered, as the `"500"(view: '/error')` mapping of a generated application does, `http.server.requests` tagged the 500 with `error` and `exception` values of `none`, and tracing did not see a failure.
4961+
* A `500` mapping to a controller tagged it with `GrailsWrappedRuntimeException` and with the `uri` of that controller, so every failure it answered looked like the same route.
4962+
* Both now report the exception the action threw and the route of the action, and the span of the request is marked as failed. That includes an `Error`, such as a failed `assert`, rather than the `ServletException` Spring MVC wraps it in. A client error is not recorded, and neither is a failure inside `g:include`, whose error page is rendered inside a page that goes on to answer the request.
4963+
* After a forward to an error controller, the `jakarta.servlet.error.exception` request attribute holds the exception the action threw rather than a `GrailsWrappedRuntimeException`, for a filter that reads it once the chain has returned. Inside the error controller it still holds the wrapper.
4964+
* These requests were already counted with `outcome=SERVER_ERROR`, but an alert that counts a non-`none` `error` value will see more server errors than before, and a dashboard that reads the `uri` of an error controller will find those failures under the routes of the actions.
4965+
* The route is now set before the Grails interceptors run, so that a failure in one is reported under the route of the action. An interceptor whose `before()` returns `false`, to render a 403 or to redirect, stopped the request before its route was set, so the request was reported with a `uri` of `UNKNOWN`, or `REDIRECTION` for a redirect. It is now reported under the route of the action, so a dashboard or alert keyed on those `uri` values sees those requests move.

‎grails-test-examples/app1/src/integration-test/groovy/functionaltests/RedirectWithAndWithoutParamsFunctionalSpec.groovy‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ class RedirectWithAndWithoutParamsFunctionalSpec extends ContainerGebSpec {
3636
$('input', name:'name').value('foo')
3737
$('input', type:'submit').click()
3838

39-
then: 'the params are not added to the url'
40-
!currentUrl.contains('?name=foo')
41-
pageSource.contains('"id":')
39+
then: 'the page the redirect leads to answers, without the params on its url'
40+
// waitFor guards against asserting on the form before the browser has followed the redirect
41+
waitFor { pageSource.contains('"id":') }
4242
pageSource.contains('"name":"foo"')
43+
!currentUrl.contains('?name=foo')
4344
}
4445

4546
@Issue('grails-core-#10622')

‎grails-test-examples/issue-11102/grails-app/conf/spring/resources.groovy‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* under the License.
1818
*/
1919

20-
// Place your Spring DSL code here
20+
import issue11102.SpringMvcHandlers
21+
2122
beans = {
23+
springMvcHandlers(SpringMvcHandlers)
2224
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package issue11102
20+
21+
class DenyingInterceptor {
22+
23+
DenyingInterceptor() {
24+
match(controller: 'failing', action: 'deniedByInterceptor')
25+
}
26+
27+
boolean before() {
28+
render status: 403, text: 'Denied by the interceptor'
29+
false
30+
}
31+
}
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package issue11102
21+
22+
import org.springframework.web.context.request.async.DeferredResult
23+
import org.springframework.web.context.request.async.WebAsyncManager
24+
import org.springframework.web.context.request.async.WebAsyncUtils
25+
import org.springframework.web.servlet.ModelAndView
26+
import org.springframework.web.servlet.view.InternalResourceView
27+
28+
import org.grails.web.mapping.ForwardUrlMappingInfo
29+
import org.grails.web.mapping.UrlMappingUtils
30+
31+
class FailingController {
32+
33+
def answeredByView() {
34+
throw new IllegalStateException('answered by the error view')
35+
}
36+
37+
def answeredByController() {
38+
throw new UnsupportedOperationException('answered by the error controller')
39+
}
40+
41+
def answeredByViewAsynchronously() {
42+
failAsynchronously(new IllegalStateException('answered by the error view'))
43+
}
44+
45+
def answeredByControllerAsynchronously() {
46+
failAsynchronously(new UnsupportedOperationException('answered by the error controller'))
47+
}
48+
49+
def answeredByViewAfterStartingAsync() {
50+
startAsyncThatTimesOut()
51+
throw new IllegalStateException('answered by the error view')
52+
}
53+
54+
def answeredByControllerAfterStartingAsync() {
55+
startAsync(new DeferredResult<Object>())
56+
throw new UnsupportedOperationException('answered by the error controller')
57+
}
58+
59+
def answeredByViewForAnError() {
60+
throw new AssertionError('answered by the error view')
61+
}
62+
63+
def answeredByControllerForAnError() {
64+
throw new StackOverflowError('answered by the error controller')
65+
}
66+
67+
def answeredByForwardingController() {
68+
throw new ArithmeticException('answered by an error controller that forwards')
69+
}
70+
71+
def answeredByIncludingController() {
72+
throw new NoSuchElementException('answered by an error controller that includes an action')
73+
}
74+
75+
def forwardedToView() {
76+
forward(action: 'answeredByView')
77+
}
78+
79+
def forwardedToViewAsynchronously() {
80+
forwardAsynchronously('/failing/answeredByView')
81+
}
82+
83+
def forwardedToControllerAsynchronously() {
84+
forwardAsynchronously('/failing/answeredByController')
85+
}
86+
87+
def forwardedToController() {
88+
forward(action: 'answeredByController')
89+
}
90+
91+
def failsInInterceptor() {
92+
render 'never reached, because the interceptor fails'
93+
}
94+
95+
def deniedByInterceptor() {
96+
render 'never reached, because the interceptor denies it'
97+
}
98+
99+
def sendsNotFound() {
100+
response.sendError(404)
101+
}
102+
103+
def notFoundHandler() {
104+
render status: 404, text: 'Answered by the not found handler'
105+
}
106+
107+
def failsAfterInclude() {
108+
g.include(action: 'included')
109+
throw new IllegalStateException('answered by the error view')
110+
}
111+
112+
def failsAfterIncludeAnsweredByController() {
113+
g.include(action: 'included')
114+
throw new UnsupportedOperationException('answered by the error controller')
115+
}
116+
117+
def includesAnswerOfView() {
118+
render "before ${g.include(action: 'answeredByView')} after"
119+
}
120+
121+
def includesAnswerOfController() {
122+
render "before ${g.include(action: 'answeredByController')} after"
123+
}
124+
125+
def includesThroughUrlMappingUtils() {
126+
request.setAttribute('greeting', 'outer')
127+
response.writer.write('before ')
128+
UrlMappingUtils.includeRequestForUrlMappingInfo(request, response, new ForwardUrlMappingInfo(controller: 'failing', action: 'includedDetails', id: '9', params: [marker: 'included']), [greeting: 'inner'], false)
129+
render " after, as ${controllerName}/${actionName} with ${params.marker}, id ${params.id} and ${request.getAttribute('greeting')}"
130+
}
131+
132+
def includedDetails() {
133+
render "included with id ${params.id} and ${request.getAttribute('greeting')}"
134+
}
135+
136+
def included() {
137+
render 'included'
138+
}
139+
140+
def errorHandler() {
141+
render status: 500, text: 'Answered by the error controller'
142+
}
143+
144+
def forwardingErrorHandler() {
145+
forward(action: 'errorHandler')
146+
}
147+
148+
def includingErrorHandler() {
149+
response.status = 500
150+
response.writer.write('Answered by the error controller, with ')
151+
UrlMappingUtils.includeRequestForUrlMappingInfo(request, response, new ForwardUrlMappingInfo(controller: 'failing', action: 'included'), [:], false)
152+
render ' inside'
153+
}
154+
155+
private void failAsynchronously(Exception failure) {
156+
DeferredResult<Object> result = new DeferredResult<>()
157+
startAsync(result)
158+
result.errorResult = failure
159+
}
160+
161+
/**
162+
* Completes asynchronous processing with a view that forwards, as a view Spring MVC resolves from a
163+
* {@code forward:} name does, so that the action the path maps to answers on the asynchronous dispatch.
164+
*/
165+
private void forwardAsynchronously(String path) {
166+
DeferredResult<Object> result = new DeferredResult<>()
167+
startAsync(result)
168+
result.result = new ModelAndView(new InternalResourceView(path))
169+
}
170+
171+
/**
172+
* Starts asynchronous processing that nothing completes. Unless the container ends it when the action fails, as
173+
* Tomcat does when the exception is left on the request, it ends when it times out.
174+
*/
175+
private void startAsyncThatTimesOut() {
176+
startAsync(new DeferredResult<Object>(500L))
177+
}
178+
179+
private void startAsync(DeferredResult<Object> result) {
180+
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request)
181+
asyncManager.asyncWebRequest = WebAsyncUtils.createAsyncWebRequest(request, response)
182+
asyncManager.startDeferredResultProcessing(result)
183+
}
184+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package issue11102
21+
22+
class FailingInterceptor {
23+
24+
FailingInterceptor() {
25+
match(controller: 'failing', action: 'failsInInterceptor')
26+
}
27+
28+
boolean before() {
29+
throw new UnsupportedOperationException('answered by the error controller')
30+
}
31+
}

0 commit comments

Comments
 (0)