-
-
Notifications
You must be signed in to change notification settings - Fork 974
GrailsUtil: honor stackTraceFiltererClass and logFullStackTraceOnFilter #15666
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
codeconsole
wants to merge
3
commits into
apache:7.1.x
Choose a base branch
from
codeconsole:7.1.x-grailsutil-honor-filterer-config
base: 7.1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 172 additions & 0 deletions
172
grails-core/src/test/groovy/grails/util/GrailsUtilStackFiltererSpec.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package grails.util | ||
|
|
||
| import grails.config.Config | ||
| import grails.core.GrailsApplication | ||
| import org.grails.exceptions.reporting.DefaultStackTraceFilterer | ||
| import org.grails.exceptions.reporting.StackTraceFilterer | ||
| import spock.lang.Specification | ||
|
|
||
| import java.lang.reflect.Field | ||
|
|
||
| /** | ||
| * Verifies that {@link GrailsUtil#initializeStackFilterer} resolves the configured filterer class | ||
| * from the application's config and propagates {@code grails.exceptionresolver.logFullStackTraceOnFilter} | ||
| * to {@link DefaultStackTraceFilterer} instances. Before initialization the FALLBACK_FILTERER | ||
| * (a {@link DefaultStackTraceFilterer} singleton) is used so CLI/test/main paths work unchanged. | ||
| */ | ||
| class GrailsUtilStackFiltererSpec extends Specification { | ||
|
|
||
| StackTraceFilterer previous | ||
|
|
||
| def setup() { | ||
| previous = currentFilterer() | ||
| setFilterer(fallbackFilterer()) | ||
| } | ||
|
|
||
| def cleanup() { | ||
| setFilterer(previous) | ||
| } | ||
|
|
||
| def 'deepSanitize uses the fallback filterer before initializeStackFilterer is called'() { | ||
| when: | ||
| GrailsUtil.deepSanitize(new RuntimeException('boom')) | ||
|
|
||
| then: | ||
| noExceptionThrown() | ||
| currentFilterer().is(fallbackFilterer()) | ||
| } | ||
|
|
||
| def 'initializeStackFilterer is a no-op when application is null'() { | ||
| when: | ||
| GrailsUtil.initializeStackFilterer(null) | ||
|
|
||
| then: | ||
| currentFilterer().is(fallbackFilterer()) | ||
| } | ||
|
|
||
| def 'initializeStackFilterer wires the class declared by grails.logging.stackTraceFiltererClass'() { | ||
| given: | ||
| def application = Mock(GrailsApplication) | ||
| def config = Mock(Config) | ||
| config.getProperty('grails.logging.stackTraceFiltererClass', Class, DefaultStackTraceFilterer) >> RecordingStackTraceFilterer | ||
| config.getProperty('grails.exceptionresolver.logFullStackTraceOnFilter', Boolean, true) >> true | ||
| application.getConfig() >> config | ||
|
|
||
| when: | ||
| GrailsUtil.initializeStackFilterer(application) | ||
| GrailsUtil.deepSanitize(new RuntimeException('boom')) | ||
|
|
||
| then: | ||
| currentFilterer() instanceof RecordingStackTraceFilterer | ||
| RecordingStackTraceFilterer.lastInstance.recursiveCalls == 1 | ||
| } | ||
|
|
||
| def 'initializeStackFilterer propagates logFullStackTraceOnFilter to DefaultStackTraceFilterer instances'() { | ||
| given: | ||
| def application = Mock(GrailsApplication) | ||
| def config = Mock(Config) | ||
| config.getProperty('grails.logging.stackTraceFiltererClass', Class, DefaultStackTraceFilterer) >> DefaultStackTraceFilterer | ||
| config.getProperty('grails.exceptionresolver.logFullStackTraceOnFilter', Boolean, true) >> false | ||
| application.getConfig() >> config | ||
|
|
||
| and: 'captured StackTrace logger output' | ||
| def originalErr = System.err | ||
| def baos = new ByteArrayOutputStream() | ||
| System.setErr(new PrintStream(baos, true)) | ||
|
|
||
| when: | ||
| GrailsUtil.initializeStackFilterer(application) | ||
| GrailsUtil.deepSanitize(new RuntimeException('boom')) | ||
|
|
||
| then: | ||
| System.err.flush() | ||
| !baos.toString().contains('ERROR StackTrace') | ||
|
|
||
| cleanup: | ||
| System.setErr(originalErr) | ||
| } | ||
|
|
||
| def 'last initializeStackFilterer call wins when invoked more than once'() { | ||
| given: | ||
| def first = mockApplicationFor(RecordingStackTraceFilterer) | ||
| def second = mockApplicationFor(SecondRecordingStackTraceFilterer) | ||
|
|
||
| when: | ||
| GrailsUtil.initializeStackFilterer(first) | ||
| GrailsUtil.initializeStackFilterer(second) | ||
|
|
||
| then: | ||
| currentFilterer() instanceof SecondRecordingStackTraceFilterer | ||
| } | ||
|
|
||
| private GrailsApplication mockApplicationFor(Class<? extends StackTraceFilterer> filtererClass) { | ||
| def application = Mock(GrailsApplication) | ||
| def config = Mock(Config) | ||
| config.getProperty('grails.logging.stackTraceFiltererClass', Class, DefaultStackTraceFilterer) >> filtererClass | ||
| config.getProperty('grails.exceptionresolver.logFullStackTraceOnFilter', Boolean, true) >> true | ||
| application.getConfig() >> config | ||
| application | ||
| } | ||
|
|
||
| private static StackTraceFilterer currentFilterer() { | ||
| filtererField().get(null) as StackTraceFilterer | ||
| } | ||
|
|
||
| private static void setFilterer(StackTraceFilterer filterer) { | ||
| filtererField().set(null, filterer) | ||
| } | ||
|
|
||
| private static StackTraceFilterer fallbackFilterer() { | ||
| Field field = GrailsUtil.getDeclaredField('FALLBACK_FILTERER') | ||
| field.accessible = true | ||
| field.get(null) as StackTraceFilterer | ||
| } | ||
|
|
||
| private static Field filtererField() { | ||
| Field field = GrailsUtil.getDeclaredField('stackFilterer') | ||
| field.accessible = true | ||
| field | ||
| } | ||
|
|
||
| static class RecordingStackTraceFilterer implements StackTraceFilterer { | ||
| static RecordingStackTraceFilterer lastInstance | ||
| int singleCalls = 0 | ||
| int recursiveCalls = 0 | ||
|
|
||
| RecordingStackTraceFilterer() { | ||
| lastInstance = this | ||
| } | ||
|
|
||
| Throwable filter(Throwable source) { singleCalls++; source } | ||
| Throwable filter(Throwable source, boolean recursive) { recursiveCalls++; source } | ||
| void addInternalPackage(String name) {} | ||
| void setCutOffPackage(String cutOffPackage) {} | ||
| void setShouldFilter(boolean shouldFilter) {} | ||
| } | ||
|
|
||
| static class SecondRecordingStackTraceFilterer implements StackTraceFilterer { | ||
| Throwable filter(Throwable source) { source } | ||
| Throwable filter(Throwable source, boolean recursive) { source } | ||
| void addInternalPackage(String name) {} | ||
| void setCutOffPackage(String cutOffPackage) {} | ||
| void setShouldFilter(boolean shouldFilter) {} | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you point me to an integration test that exercises this logic?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently only the unit spec; same coverage level the parent PR #15564 shipped with for the resolver side of these keys. Happy to add an
@Integrationspec ingrails-test-examples/app2that boots a real context, setsgrails.logging.stackTraceFiltererClassinapplication.yml, callsGrailsUtil.deepSanitizefrom inside the running app, and asserts the configured class was used. Preferapp2(it already exercises exception handling) or a dedicated minimal app likeconfig-report?