Skip to content

Commit c204060

Browse files
committed
Update Error codes docs
1 parent e81fb8d commit c204060

File tree

6 files changed

+265
-225
lines changed

6 files changed

+265
-225
lines changed

core/esmf-aspect-model-validator/src/main/java/org/eclipse/esmf/aspectmodel/validation/services/ViolationFormatter.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public class ViolationFormatter implements Function<List<Violation>, String>, Vi
4242
protected final String additionalHints;
4343
protected final RustLikeFormatter formatter;
4444

45+
private static final String ERROR_CODES_DOC_LINK = "https://eclipse-esmf.github.io/esmf-developer-guide/tooling-guide/error-codes.html";
46+
private static final String ERROR_CODES_DOC_STRING = "For more information, see: documentation: " + ERROR_CODES_DOC_LINK;
47+
4548
public ViolationFormatter( final String additionalHints ) {
4649
this( new PlainTextFormatter(), additionalHints );
4750
}
@@ -93,7 +96,7 @@ public String apply( final List<Violation> violations ) {
9396
.append( possibleFix.description() );
9497
}
9598
// Add documentation link
96-
builder.append( String.format( " > For more information, see: https://eclipse-esmf.github.io/esmf-sdk/tooling-guide/error-codes.html#%s%n",
99+
builder.append( String.format( ERROR_CODES_DOC_STRING + "#%s%n",
97100
errorCode.toLowerCase().replace( "_", "-" ) ) );
98101
builder.append( System.lineSeparator() );
99102
}
@@ -108,15 +111,15 @@ public String apply( final List<Violation> violations ) {
108111
final String enhancedMessage = String.format( "[%s] %s", errorCode, violation.message() );
109112
builder.append( String.format( "> %s%n", enhancedMessage ) );
110113
// Add documentation link for context-free violations
111-
builder.append( " > For more information, see: documentation: https://eclipse-esmf.github.io/esmf-developer-guide/tooling-guide/error-codes.html"
114+
builder.append( ERROR_CODES_DOC_STRING
112115
+ errorCode.toLowerCase().replace( "_", "-" ) );
113116
} else {
114117
final String errorCode = violation.errorCode();
115118
final String enhancedMessage = String.format( "[%s] %s", errorCode, violation.message() );
116119
builder.append( String.format( "> %s: %n", enhancedMessage ) );
117120
builder.append( indent( violation.accept( this ), 2 ) ).append( System.lineSeparator() );
118121
// Add documentation link
119-
builder.append( " > For more information, see: documentation: https://eclipse-esmf.github.io/esmf-developer-guide/tooling-guide/error-codes.html"
122+
builder.append( ERROR_CODES_DOC_STRING
120123
+ errorCode.toLowerCase().replace( "_", "-" ) );
121124
}
122125
builder.append( System.lineSeparator() );
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2025 Robert Bosch Manufacturing Solutions GmbH
3+
*
4+
* See the AUTHORS file(s) distributed with this work for additional
5+
* information regarding authorship.
6+
*
7+
* This Source Code Form is subject to the terms of the Mozilla Public
8+
* License, v. 2.0. If a copy of the MPL was not distributed with this
9+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
10+
*
11+
* SPDX-License-Identifier: MPL-2.0
12+
*/
13+
14+
package examples;
15+
16+
// tag::imports[]
17+
import java.util.List;
18+
import java.util.Map;
19+
import java.util.stream.Collectors;
20+
21+
import org.eclipse.esmf.aspectmodel.shacl.violation.DatatypeViolation;
22+
import org.eclipse.esmf.aspectmodel.shacl.violation.MaxCountViolation;
23+
import org.eclipse.esmf.aspectmodel.shacl.violation.Violation;
24+
// end::imports[]
25+
26+
public class CustomViolationFormatter implements Violation.Visitor<String> {
27+
28+
// tag::custom-formatter[]
29+
// Implement other visitor methods for different violation types
30+
@Override
31+
public String visit(Violation violation) {
32+
// Generic fallback for unhandled violation types
33+
return String.format("Validation error: %s", violation.message());
34+
}
35+
// end::custom-formatter[]
36+
37+
// tag::error-aggregation[]
38+
public void aggregateErrors(List<Violation> violations) {
39+
// Group violations by error code for batch processing
40+
Map<String, List<Violation>> errorsByCode = violations.stream()
41+
.collect(Collectors.groupingBy(Violation::errorCode));
42+
43+
// Handle each error type separately
44+
List<Violation> dataTypeErrors = errorsByCode.get("ERR_TYPE");
45+
List<Violation> cardinalityErrors = errorsByCode.get("ERR_MAX_COUNT");
46+
47+
// Process each error type with specific handling logic
48+
if (dataTypeErrors != null && !dataTypeErrors.isEmpty()) {
49+
System.out.println("Found " + dataTypeErrors.size() + " data type errors");
50+
// Handle data type errors...
51+
}
52+
53+
if (cardinalityErrors != null && !cardinalityErrors.isEmpty()) {
54+
System.out.println("Found " + cardinalityErrors.size() + " cardinality errors");
55+
// Handle cardinality errors...
56+
}
57+
}
58+
// end::error-aggregation[]
59+
}

documentation/developer-guide/modules/tooling-guide/nav.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
* xref:java-aspect-tooling.adoc[Java APIs for Aspect Models]
33
* xref:maven-plugin.adoc[Maven Plugin]
44
* xref:error-codes.adoc[Error Codes and Validation Results]
5+
* xref:best-practices.adoc[Best Practices]
56
* xref:bamm-migration.adoc[Migration from BAMM]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
:page-partial:
2+
3+
[[best-practices]]
4+
= Best Practices
5+
6+
This guide provides best practices for developing Aspect Models and handling validation errors effectively.
7+
8+
[[analyzing-modeling-errors]]
9+
== Analyzing Modeling Errors
10+
11+
When working with ESMF SDK validation results, follow these practices to efficiently identify and resolve modeling issues.
12+
13+
=== Model Development
14+
15+
1. **Validate Early**: Run validation frequently during model development
16+
2. **Use Detailed Output**: Enable detailed error reporting for comprehensive feedback
17+
3. **Address Errors Systematically**: Start with meta-violations before fixing constraint violations
18+
4. **Test Edge Cases**: Verify models with boundary values and optional properties
19+
20+
=== Error Handling
21+
22+
1. **Programmatic Handling**: Use the visitor pattern for type-safe error handling
23+
2. **User-Friendly Messages**: Transform technical errors into user-friendly guidance
24+
3. **Logging**: Log error codes and context for troubleshooting
25+
4. **Documentation**: Link to error code documentation in error messages where appropriate
26+
27+

0 commit comments

Comments
 (0)