Skip to content

Commit 3ba7cc7

Browse files
authored
YAML Error Handling (#2747)
* refactor(annot): enhance YAML parsing error reporting and handle nested contexts - Replaced `ParsingException` with `YamlParsingException` for improved error details. - Introduced `YamlErrorRenderer` for visual error markers in YAML reports. - Added support for nested parsing contexts in `YamlParsingException`. - Improved validation logic and error messages in `NodeValidationUtils` and `MethodSetter`. - Updated `GenericYamlParser` to enhance schema validation handling. * refactor(yaml-parsing): rename `YamlParsingException` to `ConfigurationParsingException` and update related classes - Replaced `YamlParsingException` with `ConfigurationParsingException` for better clarity in exception usage. - Updated `GenericYamlParser` and test cases to align with the new exception class. - Simplified exception handling and removed redundant YAML-related logic. * refactor(yaml-parsing): remove unused parameters and simplify logic in parsing methods - Eliminated redundant parameters from `resolveSpelValue` and `replaceMarkerWithError` methods. - Improved clarity in conditionals flagged as always true or false, annotated with TODO comments for future review. - Streamlined exception handling in `GenericYamlParser` and optimized parsing utility logic. * add copyright headers and improve YAML context handling - Added missing Apache License headers to multiple classes. - Introduced `updateContext` in `ParsingContext` for enhanced YAML parsing context tracking. - Fixed formatting and improved error handling logic in `YamlErrorRenderer`. - Applied minor code cleanups in `MethodSetter` and `GenericYamlParser`. * refactor(tests): remove redundant `System.out.println` calls and outdated YAML parsing test blocks - Cleaned up unused debug statements in `YAMLParsingReportTest`. - Removed obsolete and commented-out test sections to simplify and improve readability. - Adjusted YAML error context validation to reflect updated parsing behavior. * refactor(yaml-parsing): replace `YamlErrorRenderer` with `LineYamlErrorRenderer` - Replaced `YamlErrorRenderer` with `LineYamlErrorRenderer` for improved line-based error rendering in YAML context. - Removed obsolete `YamlErrorRenderer` class and its dependencies. - Refactored `MethodSetter` and `ParsingContext` to enhance error handling and context tracking. - Updated test cases to align with the new error rendering approach and improved YAML parsing validation logic. * refactor(yaml-parsing): clean up null checks, update formatting, and remove unused constants - Replaced `assertEquals(null, ...)` with `assertNull(...)` for better readability. - Updated `ParsingContext#toString` with a modern `formatted` multi-line string. - Removed unused `BASE_INDENT` constant from `LineYamlErrorRenderer`. * refactor(yaml-parsing): improve error handling, logging, and simplify exception messages - Added SLF4J logging to `MethodSetter` for detailed debug messages on missing methods/beans. - Simplified `ConfigurationParsingException` message initialization in `GenericYamlParser`. - Replaced redundant `System.out.println` call with direct usage of `renderErrorReport`. - Enhanced error messages in `McYamlIntrospector` for clarity on list vs. object mismatches. - Imported SLF4J and utility classes as part of code cleanup. * refactor(yaml-parsing): fix typos, improve logging, and update generic handling - Corrected typos in method names (e.g., `validateAgaistSchema` to `validateAgainstSchema`). - Introduced additional debug logging in `GenericYamlParser` for enhanced error tracking. - Updated generic type handling in `ParsingContext` and `NodeValidationUtils`. - Improved exception message formatting for clarity. * test(yaml-parsing): reformat YAML test cases and update validation methods - Reformatted YAML sources in `YAMLParsingCollapsedTest` and `YAMLParsingTest` for consistent indentation. - Replaced redundant inline structure definitions with properly aligned multi-line structures in test assertions. - Updated exception validation logic to use `assertNull` and `assertTrue` for better clarity. * test(tests): clean up redundant methods and minor fixes in YAML parsing tests - Removed unused `rootCause` method from `YAMLParsingCollapsedTest`. - Updated exception handling to ignore caught exceptions for clarity. - Replaced `assertEquals(null, ...)` with `assertNull(...)` in `YAMLParsingTest`. - Fixed minor formatting issues in test cases. * refactor: standardize accessor methods in `ParsingContext` and update usages - Updated `ParsingContext` to use consistent `get*` accessor methods. - Replaced direct field accesses with the new accessor methods across all relevant classes and test files. - Improved exception messages and updated formatting for enhanced readability and clarity. * refactor: migrate `ParsingContext` to record, simplify exception handling in YAML tests - Converted `ParsingContext` to a record for cleaner implementation. - Updated YAML parsing tests to use the new `getRootCause` helper method with type assertions. - Reintroduced a previously commented-out YAML parsing test with improved exception handling. * refactor: clean up tests and improve annotation handling in YAML parsing - Replaced `assertEquals(null, ...)` with `assertNull(...)` in `YAMLParsingErrorTest`. - Added `@NotNull` annotation to `ParsingContext#toString` for improved null safety. - Removed unused `assertSchemaErrorContains` method from `YAMLComponentsParsingTest`. - Simplified `getRootCause` logic by removing redundant null checks.
1 parent 0fe9f66 commit 3ba7cc7

27 files changed

Lines changed: 1996 additions & 1043 deletions

annot/pom.xml

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,21 @@
1212
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
15-
--><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
15+
-->
16+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
17+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
1618

1719
<modelVersion>4.0.0</modelVersion>
1820
<artifactId>service-proxy-annot</artifactId>
1921
<name>${project.artifactId}</name>
2022
<packaging>jar</packaging>
2123

22-
<parent>
23-
<groupId>org.membrane-soa</groupId>
24-
<artifactId>service-proxy-parent</artifactId>
25-
<relativePath>../pom.xml</relativePath>
26-
<version>7.0.7-SNAPSHOT</version>
27-
</parent>
24+
<parent>
25+
<groupId>org.membrane-soa</groupId>
26+
<artifactId>service-proxy-parent</artifactId>
27+
<relativePath>../pom.xml</relativePath>
28+
<version>7.0.7-SNAPSHOT</version>
29+
</parent>
2830

2931
<dependencies>
3032
<dependency>
@@ -64,6 +66,11 @@
6466
<artifactId>jakarta.annotation-api</artifactId>
6567
<version>3.0.0</version>
6668
</dependency>
69+
<dependency>
70+
<groupId>com.jayway.jsonpath</groupId>
71+
<artifactId>json-path</artifactId>
72+
<version>2.10.0</version>
73+
</dependency>
6774

6875
<!-- Test dependencies -->
6976
<dependency>

annot/src/main/java/com/predic8/membrane/annot/MCChildElement.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
limitations under the License. */
1414
package com.predic8.membrane.annot;
1515

16-
import java.lang.annotation.ElementType;
17-
import java.lang.annotation.Retention;
18-
import java.lang.annotation.RetentionPolicy;
19-
import java.lang.annotation.Target;
16+
import java.lang.annotation.*;
2017

21-
@Target(ElementType.METHOD)
22-
@Retention(RetentionPolicy.RUNTIME)
18+
import static java.lang.annotation.ElementType.*;
19+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
20+
21+
@Target(METHOD)
22+
@Retention(RUNTIME)
2323
public @interface MCChildElement {
2424

2525
/**

annot/src/main/java/com/predic8/membrane/annot/bean/BeanFactory.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
package com.predic8.membrane.annot.bean;
1616

1717
import com.fasterxml.jackson.databind.*;
18-
import com.predic8.membrane.annot.beanregistry.BeanRegistry;
18+
import com.predic8.membrane.annot.beanregistry.*;
1919
import com.predic8.membrane.annot.util.*;
20+
import com.predic8.membrane.annot.yaml.*;
2021
import org.jetbrains.annotations.*;
2122

2223
import java.lang.reflect.*;
2324
import java.util.*;
2425
import java.util.stream.*;
2526

26-
import static com.predic8.membrane.annot.util.ReflectionUtil.isWrapperOfPrimitive;
27+
import static com.predic8.membrane.annot.util.ReflectionUtil.*;
2728

2829
/**
2930
* Builds Java objects from a "bean" JSON node (YAML).

annot/src/main/java/com/predic8/membrane/annot/beanregistry/BeanContainer.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import com.predic8.membrane.annot.Grammar;
1818
import com.predic8.membrane.annot.bean.BeanFactory;
19-
import com.predic8.membrane.annot.yaml.GenericYamlParser;
19+
import com.predic8.membrane.annot.yaml.*;
2020
import org.jetbrains.annotations.*;
2121
import org.slf4j.Logger;
2222
import org.slf4j.LoggerFactory;
@@ -30,7 +30,7 @@ public class BeanContainer {
3030
/**
3131
* The class of the bean.
3232
*/
33-
private final Class clazz;
33+
private final Class<?> clazz;
3434
/**
3535
* Constructed bean after initialization.
3636
*/
@@ -88,7 +88,11 @@ public String toString() {
8888
grammar,
8989
definition.getNode(),
9090
registry);
91-
} catch (Exception e) {
91+
} catch (ConfigurationParsingException e) {
92+
throw e;
93+
}
94+
catch (Exception e) {
95+
log.error("Could not instantiate bean: {}", definition.getNode(), e);
9296
throw new RuntimeException(e);
9397
}
9498
}
@@ -101,7 +105,6 @@ public String toString() {
101105
return define(registry, grammar);
102106
}
103107

104-
105108
// Singleton: ensure define() runs at most once per BeanContainer.
106109
synchronized (this) {
107110
Object existing = getSingleton();

annot/src/main/java/com/predic8/membrane/annot/beanregistry/BeanRegistryImplementation.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
package com.predic8.membrane.annot.beanregistry;
1515

1616
import com.predic8.membrane.annot.*;
17-
import com.predic8.membrane.annot.bean.*;
1817
import com.predic8.membrane.annot.yaml.*;
1918
import org.jetbrains.annotations.*;
2019
import org.slf4j.*;
@@ -126,7 +125,10 @@ private void activationRun() {
126125
bcs.remove(bc.getDefinition().getUid());
127126
}
128127
uidsToRemove.add(uidAction);
129-
} catch (Exception e) {
128+
} catch (ConfigurationParsingException e) {
129+
throw e;
130+
}
131+
catch (Exception e) {
130132
log.error("Could not handle {} {}/{}", uidAction.action,
131133
bc.getDefinition().getNamespace(), bc.getDefinition().getName(), e);
132134
throw new RuntimeException(e);
@@ -156,7 +158,6 @@ public Object resolve(String url) {
156158
public List<Object> getBeans() {
157159
return bcs.values().stream().filter(bd -> !bd.getDefinition().isComponent())
158160
.map(bc -> bc.getOrCreate(this, grammar))
159-
.filter(Objects::nonNull)
160161
.toList();
161162
}
162163

@@ -170,7 +171,6 @@ public <T> List<T> getBeans(Class<T> clazz) {
170171
return bcs.values().stream()
171172
.filter(bd -> bd.produces(clazz))
172173
.map(bc -> bc.getOrCreate(this, grammar))
173-
.filter(Objects::nonNull)
174174
.filter(clazz::isInstance)
175175
.map(clazz::cast)
176176
.toList();
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* Copyright 2026 predic8 GmbH, www.predic8.com
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
package com.predic8.membrane.annot.yaml;
16+
17+
import com.fasterxml.jackson.core.*;
18+
import com.fasterxml.jackson.databind.*;
19+
20+
import static com.predic8.membrane.annot.yaml.error.LineYamlErrorRenderer.renderErrorReport;
21+
22+
public class ConfigurationParsingException extends RuntimeException {
23+
24+
private ParsingContext<?> parsingContext;
25+
private JsonNode wrong;
26+
27+
public ConfigurationParsingException(String message) {
28+
super(message);
29+
}
30+
31+
public ConfigurationParsingException(Throwable cause) {
32+
super(cause);
33+
}
34+
35+
public ConfigurationParsingException(String message, Throwable cause, ParsingContext<?> pc) {
36+
super(message, cause);
37+
this.parsingContext = pc;
38+
}
39+
40+
public ParsingContext<?> getParsingContext() {
41+
return parsingContext;
42+
}
43+
44+
public void setParsingContext(ParsingContext<?> parsingContext) {
45+
this.parsingContext = parsingContext;
46+
}
47+
48+
/**
49+
* Returns a complete formatted error report including highlighted YAML.
50+
*/
51+
public String getFormattedReport() throws JsonProcessingException {
52+
return renderErrorReport(this.parsingContext);
53+
}
54+
55+
public JsonNode getWrong() {
56+
return wrong;
57+
}
58+
59+
public void setWrong(JsonNode wrong) {
60+
this.wrong = wrong;
61+
}
62+
}

0 commit comments

Comments
 (0)