planetiler/planetiler-core/src/main/java/com/onthegomap/planetiler/geo/GeometryException.java

109 wiersze
3.8 KiB
Java
Czysty Zwykły widok Historia

package com.onthegomap.planetiler.geo;
2021-05-20 09:59:18 +00:00
import com.onthegomap.planetiler.stats.Stats;
2021-07-18 12:17:58 +00:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2021-09-10 00:46:20 +00:00
/**
* An error caused by unexpected input geometry that should be handled to avoid halting the entire program for bad data
* we are sure to encounter in the wild.
*/
2021-05-20 09:59:18 +00:00
public class GeometryException extends Exception {
2021-07-18 12:17:58 +00:00
private static final Logger LOGGER = LoggerFactory.getLogger(GeometryException.class);
2021-06-06 12:00:04 +00:00
private final String stat;
2023-03-20 20:41:18 +00:00
private final boolean nonFatal;
2021-05-20 09:59:18 +00:00
2021-09-10 00:46:20 +00:00
/**
* Constructs a new exception with a detailed error message caused by {@code cause}.
*
* @param stat string that uniquely defines this error that will be used to count number of occurrences in stats
* @param message description of the error to log that should be detailed enough that you can find the offending
* geometry from it
* @param cause the original exception that was thrown
*/
2021-06-06 12:00:04 +00:00
public GeometryException(String stat, String message, Throwable cause) {
2021-05-20 09:59:18 +00:00
super(message, cause);
2021-06-06 12:00:04 +00:00
this.stat = stat;
2023-03-20 20:41:18 +00:00
this.nonFatal = false;
}
/**
* Constructs a new exception with a detailed error message for. Use
* {@link #GeometryException(String, String, boolean)} for non-fatal exceptions.
*/
public GeometryException(String stat, String message) {
this(stat, message, false);
2021-05-20 09:59:18 +00:00
}
2021-09-10 00:46:20 +00:00
/**
* Constructs a new exception with a detailed error message.
*
2023-03-20 20:41:18 +00:00
* @param stat string that uniquely defines this error that will be used to count number of occurrences in stats
* @param message description of the error to log that should be detailed enough that you can find the offending
* geometry from it
* @param nonFatal When true, won't cause an assertion error when thrown
2021-09-10 00:46:20 +00:00
*/
2023-03-20 20:41:18 +00:00
public GeometryException(String stat, String message, boolean nonFatal) {
2021-05-20 09:59:18 +00:00
super(message);
2021-06-06 12:00:04 +00:00
this.stat = stat;
2023-03-20 20:41:18 +00:00
this.nonFatal = nonFatal;
2021-05-20 09:59:18 +00:00
}
2021-09-10 00:46:20 +00:00
/** Returns the unique code for this error condition to use for counting the number of occurrences in stats. */
2021-06-06 12:00:04 +00:00
public String stat() {
return stat;
}
2021-07-18 12:17:58 +00:00
2021-09-10 00:46:20 +00:00
/** Prints the error and also increments a stat counter for this error and logs it. */
public void log(Stats stats, String statPrefix, String logPrefix) {
stats.dataError(statPrefix + "_" + stat());
log(logPrefix);
2021-07-18 12:17:58 +00:00
}
2021-09-10 00:46:20 +00:00
/** Prints the error but does not increment any stats. */
2021-07-18 12:17:58 +00:00
public void log(String logContext) {
logMessage(logContext + ": " + getMessage());
}
void logMessage(String log) {
LOGGER.warn(log);
2023-03-20 20:41:18 +00:00
assert nonFatal : log; // make unit tests fail if fatal
2021-07-18 12:17:58 +00:00
}
2021-09-10 00:46:20 +00:00
/**
* An error that we expect to encounter often so should only be logged at {@code TRACE} level.
*/
2021-07-18 12:17:58 +00:00
public static class Verbose extends GeometryException {
2021-09-10 00:46:20 +00:00
/**
* Constructs a new verbose exception with a detailed error message caused by {@code cause}.
*
* @param stat string that uniquely defines this error that will be used to count number of occurrences in stats
* @param message description of the error to log that should be detailed enough that you can find the offending
* geometry from it
* @param cause the original exception that was thrown
*/
2021-07-18 12:17:58 +00:00
public Verbose(String stat, String message, Throwable cause) {
super(stat, message, cause);
}
2021-09-10 00:46:20 +00:00
/**
* Constructs a new verbose exception with a detailed error message.
*
* @param stat string that uniquely defines this error that will be used to count number of occurrences in stats
* @param message description of the error to log that should be detailed enough that you can find the offending
* geometry from it
*/
2021-07-18 12:17:58 +00:00
public Verbose(String stat, String message) {
super(stat, message);
}
@Override
void logMessage(String log) {
LOGGER.trace(log);
}
}
2021-05-20 09:59:18 +00:00
}