No logs if null zone id in other-field; smaller logs if invalid zone (shouldn't really need stack)

This commit is contained in:
2024-04-17 17:04:20 -05:00
parent af3c0cd041
commit 34b5da78eb
2 changed files with 51 additions and 11 deletions

View File

@ -118,32 +118,49 @@ public class DateTimeDisplayValueBehavior implements FieldDisplayBehavior<DateTi
Instant instant = record.getValueInstant(field.getName()); Instant instant = record.getValueInstant(field.getName());
String zoneString = record.getValueString(zoneIdFromFieldName); String zoneString = record.getValueString(zoneIdFromFieldName);
ZoneId zoneId; ZoneId zoneId = null;
try if(StringUtils.hasContent(zoneString))
{ {
zoneId = ZoneId.of(zoneString); try
{
zoneId = ZoneId.of(zoneString);
}
catch(Exception e)
{
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// we probably(?) don't need a stack trace here (and it could get noisy?), so just info w/ the exception message... //
// and we expect this might be somewhat frequent, if you might have invalid values in your zoneId field... //
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LOG.info("Exception applying zoneIdFromFieldName behavior", logPair("message", e.getMessage()), logPair("table", table.getName()), logPair("field", field.getName()), logPair("id", record.getValue(table.getPrimaryKeyField())));
}
} }
catch(Exception e)
if(zoneId == null)
{ {
//////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////
// if the zone string from the other field isn't valid, and we have a fallback, try to use it // // if the zone string from the other field isn't valid, and we have a fallback, try to use it //
//////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////
if(StringUtils.hasContent(fallbackZoneId)) if(StringUtils.hasContent(fallbackZoneId))
{ {
////////////////////////////////////////////////////////////////////////////////////////////
// assume that validation has confirmed this is a valid zone - so no try-catch right here //
////////////////////////////////////////////////////////////////////////////////////////////
zoneId = ZoneId.of(fallbackZoneId); zoneId = ZoneId.of(fallbackZoneId);
} }
else
{
throw (e);
}
} }
ZonedDateTime zonedDateTime = instant.atZone(zoneId); if(zoneId != null)
record.setDisplayValue(field.getName(), QValueFormatter.formatDateTimeWithZone(zonedDateTime)); {
ZonedDateTime zonedDateTime = instant.atZone(zoneId);
record.setDisplayValue(field.getName(), QValueFormatter.formatDateTimeWithZone(zonedDateTime));
}
} }
catch(Exception e) catch(Exception e)
{ {
LOG.info("Error applying zoneIdFromFieldName DateTimeDisplayValueBehavior", e, logPair("table", table.getName()), logPair("field", field.getName()), logPair("id", record.getValue(table.getPrimaryKeyField()))); ///////////////////////////////////////////////////////////////////////
// we don't expect this to ever hit - so warn it w/ stack if it does //
///////////////////////////////////////////////////////////////////////
LOG.warn("Unexpected error applying zoneIdFromFieldName behavior", e, logPair("table", table.getName()), logPair("field", field.getName()), logPair("id", record.getValue(table.getPrimaryKeyField())));
} }
} }
} }

View File

@ -36,6 +36,7 @@ import com.kingsrook.qqq.backend.core.utils.TestUtils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
/******************************************************************************* /*******************************************************************************
@ -100,6 +101,28 @@ class DateTimeDisplayValueBehaviorTest extends BaseTest
} }
/*******************************************************************************
**
*******************************************************************************/
@Test
void testBadZoneIdFromOtherField()
{
QInstance qInstance = QContext.getQInstance();
QTableMetaData table = qInstance.getTable(TestUtils.TABLE_NAME_PERSON_MEMORY);
table.withField(new QFieldMetaData("timeZone", QFieldType.STRING));
table.getField("createDate").withBehavior(new DateTimeDisplayValueBehavior().withZoneIdFromFieldName("timeZone"));
QRecord record = new QRecord().withValue("createDate", Instant.parse("2024-04-04T19:12:00Z")).withValue("timeZone", "fail");
ValueBehaviorApplier.applyFieldBehaviors(ValueBehaviorApplier.Action.FORMATTING, qInstance, table, List.of(record), null);
assertNull(record.getDisplayValue("createDate"));
record = new QRecord().withValue("createDate", Instant.parse("2024-04-04T19:12:00Z")).withValue("timeZone", null);
ValueBehaviorApplier.applyFieldBehaviors(ValueBehaviorApplier.Action.FORMATTING, qInstance, table, List.of(record), null);
assertNull(record.getDisplayValue("createDate"));
}
/******************************************************************************* /*******************************************************************************
** **