3131import io .apicurio .registry .resolver .config .SchemaResolverConfig ;
3232import io .apicurio .registry .resolver .data .Record ;
3333import io .apicurio .registry .resolver .strategy .ArtifactReference ;
34+ import io .apicurio .registry .rest .client .models .ProblemDetails ;
3435import io .apicurio .registry .types .ArtifactType ;
3536import io .apicurio .registry .utils .IoUtil ;
3637import org .json .JSONObject ;
@@ -86,9 +87,15 @@ protected JsonValidator() {
8687 */
8788 public JsonValidationResult validateByArtifactReference (Object bean ) {
8889 Objects .requireNonNull (this .artifactReference , "ArtifactReference must be provided when creating JsonValidator in order to use this feature" );
89- SchemaLookupResult <JsonSchema > schema = this .schemaResolver .resolveSchemaByArtifactReference (this .artifactReference );
90- JsonNode jsonPayload = createJSONObject (bean );
91- return validate (schema .getParsedSchema ().getParsedSchema (), jsonPayload );
90+ try {
91+ SchemaLookupResult <JsonSchema > schema = this .schemaResolver .resolveSchemaByArtifactReference (this .artifactReference );
92+ JsonNode jsonPayload = createJSONObject (bean );
93+ return validate (schema .getParsedSchema ().getParsedSchema (), jsonPayload );
94+ } catch (Exception e ) {
95+ return JsonValidationResult .fromErrors (List .of (
96+ new ValidationError ("Failed to resolve schema from registry: " + extractErrorMessage (e ), "SCHEMA_RESOLUTION_ERROR" )
97+ ));
98+ }
9299 }
93100
94101 /**
@@ -101,9 +108,15 @@ public JsonValidationResult validateByArtifactReference(Object bean) {
101108 * @return JsonValidationResult
102109 */
103110 public JsonValidationResult validate (Record <Object > record ) {
104- SchemaLookupResult <JsonSchema > schema = this .schemaResolver .resolveSchema (record );
105- JsonNode jsonPayload = createJSONObject (record .payload ());
106- return validate (schema .getParsedSchema ().getParsedSchema (), jsonPayload );
111+ try {
112+ SchemaLookupResult <JsonSchema > schema = this .schemaResolver .resolveSchema (record );
113+ JsonNode jsonPayload = createJSONObject (record .payload ());
114+ return validate (schema .getParsedSchema ().getParsedSchema (), jsonPayload );
115+ } catch (Exception e ) {
116+ return JsonValidationResult .fromErrors (List .of (
117+ new ValidationError ("Failed to resolve schema from registry: " + extractErrorMessage (e ), "SCHEMA_RESOLUTION_ERROR" )
118+ ));
119+ }
107120 }
108121
109122 protected JsonValidationResult validate (JsonSchema schema , JsonNode jsonPayload ) {
@@ -142,6 +155,41 @@ private List<ValidationError> extractValidationErrors(Set<ValidationMessage> val
142155 return errors ;
143156 }
144157
158+ private String extractErrorMessage (Exception e ) {
159+ StringBuilder errorMessage = new StringBuilder ();
160+
161+ // Start with the exception type and message
162+ errorMessage .append (e .getClass ().getSimpleName ());
163+ String message = getDetailedMessage (e );
164+ if (message != null && !message .isEmpty ()) {
165+ errorMessage .append (": " ).append (message );
166+ }
167+
168+ // Add cause chain for more context
169+ Throwable cause = e .getCause ();
170+ while (cause != null ) {
171+ errorMessage .append (" | Caused by: " ).append (cause .getClass ().getSimpleName ());
172+ String causeMessage = getDetailedMessage (cause );
173+ if (causeMessage != null && !causeMessage .isEmpty ()) {
174+ errorMessage .append (": " ).append (causeMessage );
175+ }
176+ cause = cause .getCause ();
177+ }
178+
179+ return errorMessage .toString ();
180+ }
181+
182+ private String getDetailedMessage (Throwable throwable ) {
183+ // Special handling for ProblemDetails from Apicurio Registry REST client
184+ if (throwable instanceof ProblemDetails ) {
185+ String detail = ((ProblemDetails ) throwable ).getDetail ();
186+ if (detail != null && !detail .isEmpty ()) {
187+ return detail ;
188+ }
189+ }
190+ return throwable .getMessage ();
191+ }
192+
145193 public static class JsonSchemaParser implements SchemaParser <JsonSchema , Object > {
146194 @ Override
147195 public String artifactType () {
0 commit comments