|
23 | 23 | import java.util.Map; |
24 | 24 | import java.util.function.Predicate; |
25 | 25 | import java.util.regex.Pattern; |
| 26 | +import java.util.regex.PatternSyntaxException; |
26 | 27 | import java.util.stream.Stream; |
27 | 28 |
|
28 | 29 | import com.fasterxml.jackson.annotation.JsonInclude; |
29 | 30 | import org.jspecify.annotations.Nullable; |
30 | 31 |
|
| 32 | +import org.springframework.boot.actuate.endpoint.InvalidEndpointRequestException; |
31 | 33 | import org.springframework.boot.actuate.endpoint.OperationResponseBody; |
32 | 34 | import org.springframework.boot.actuate.endpoint.SanitizableData; |
33 | 35 | import org.springframework.boot.actuate.endpoint.Sanitizer; |
@@ -87,11 +89,21 @@ public EnvironmentDescriptor environment(@Nullable String pattern) { |
87 | 89 |
|
88 | 90 | EnvironmentDescriptor getEnvironmentDescriptor(@Nullable String pattern, boolean showUnsanitized) { |
89 | 91 | if (StringUtils.hasText(pattern)) { |
90 | | - return getEnvironmentDescriptor(Pattern.compile(pattern).asPredicate(), showUnsanitized); |
| 92 | + return getEnvironmentDescriptor(getPatternPredicate(pattern), showUnsanitized); |
91 | 93 | } |
92 | 94 | return getEnvironmentDescriptor((name) -> true, showUnsanitized); |
93 | 95 | } |
94 | 96 |
|
| 97 | + private Predicate<String> getPatternPredicate(String pattern) { |
| 98 | + try { |
| 99 | + return Pattern.compile(pattern).asPredicate(); |
| 100 | + } |
| 101 | + catch (PatternSyntaxException ex) { |
| 102 | + throw new InvalidEndpointRequestException("Pattern '" + pattern + "' is not a valid regular expression", |
| 103 | + ex.getMessage()); |
| 104 | + } |
| 105 | + } |
| 106 | + |
95 | 107 | private EnvironmentDescriptor getEnvironmentDescriptor(Predicate<String> propertyNamePredicate, |
96 | 108 | boolean showUnsanitized) { |
97 | 109 | List<PropertySourceDescriptor> propertySources = new ArrayList<>(); |
|
0 commit comments