Skip to content

Commit

Permalink
Merge pull request #25 from NOAA-OWP/issue24
Browse files Browse the repository at this point in the history
Produce an informative declaration error, #24
  • Loading branch information
james-d-brown committed Jul 3, 2024
2 parents 5b07a6e + 8e6f4d7 commit a802b0a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
12 changes: 10 additions & 2 deletions wres-config/src/wres/config/yaml/DeclarationFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.io.IOContext;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
Expand Down Expand Up @@ -464,8 +465,15 @@ static JsonSchema getSchema() throws IOException

static EvaluationDeclaration deserialize( JsonNode declaration ) throws IOException
{
return DESERIALIZER.reader()
.readValue( declaration, EvaluationDeclaration.class );
try
{
return DESERIALIZER.reader()
.readValue( declaration, EvaluationDeclaration.class );
}
catch ( JsonMappingException e )
{
throw new IOException( "Failed to deserialize an evaluation declaration.", e );
}
}

/**
Expand Down
27 changes: 23 additions & 4 deletions wres-config/src/wres/config/yaml/DeclarationValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,30 @@ public static List<EvaluationStatusEvent> validate( String yaml,
// schema validation errors: see #57969
if ( schemaEvents.isEmpty() )
{
EvaluationDeclaration deserialized = DeclarationFactory.deserialize( declaration );
try
{
EvaluationDeclaration deserialized = DeclarationFactory.deserialize( declaration );

// Validate against business logic
List<EvaluationStatusEvent> businessEvents = DeclarationValidator.validate( deserialized, omitSources );
events.addAll( businessEvents );
}
// Catch exceptions here that can occur during deserialization
catch ( IOException | DeclarationException e )
{
EvaluationStatusEvent error =
EvaluationStatusEvent.newBuilder()
.setStatusLevel( StatusLevel.ERROR )
.setEventMessage( "Failed to deserialize and fully validate the "
+ "declaration because it contains errors: "
+ "please fix the declaration and try "
+ "again. The cause of the error was: \n"
+ e.getCause()
.getMessage() )
.build();

// Validate against business logic
List<EvaluationStatusEvent> businessEvents = DeclarationValidator.validate( deserialized, omitSources );
events.addAll( businessEvents );
return List.of( error );
}
}

return Collections.unmodifiableList( events );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import wres.config.yaml.DeclarationException;

/**
* Custom deserializer for a {@link ZoneId} that admits several informal shorthands, as well as all named time zones
* accepted by {@link ZoneId#of(String)}.
Expand Down Expand Up @@ -115,7 +117,17 @@ public static ZoneOffset getZoneOffset( String zoneOffset )
// Otherwise, pass through directly
else
{
return ZoneOffset.of( zoneOffset );
try
{
return ZoneOffset.of( zoneOffset );
}
catch ( DateTimeException e )
{
throw new DeclarationException( "Failed to deserialize a 'time_zone_offset'. Please use a valid offset "
+ "name, such as 'EST' or a valid ISO 8601 UTC offset, such as "
+ "'-05:00'.",
e );
}
}
}
}
Expand Down

0 comments on commit a802b0a

Please sign in to comment.