Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add catch all error code for metrics #26

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@ public int getFieldNumber(String name) {
protected Set<Prediction> predictions = new HashSet<>();

public static final String DATAWAVE = "DATAWAVE";
public static final String DEFAULT_ERROR_CODE = "500-1";
protected static final Map<String,String> discoveredVersionMap = BaseQueryMetric.getVersionsFromClasspath();
protected long numUpdates = 0;

Expand Down Expand Up @@ -938,12 +939,32 @@ public void addPrediction(Prediction prediction) {
this.predictions.add(prediction);
}

/**
* Sets the error code and error message of the metric. There are a few cases that can occur: <br>
* <br>
* <u>The throwable cause <b>IS</b> an instance of {@link QueryException}:</u>
* <ul>
* <li>In this case, the error message and error code will be set to the values that were passed when the exception was thrown.</li>
* <li>If the error code happens to be blank, {@link BaseQueryMetric#DEFAULT_ERROR_CODE} will be used.</li>
* </ul>
* <br>
* <u>The throwable cause <b>IS NOT</b> an instance of {@link QueryException}:</u>
* <ul>
* <li>In this case, there is no error code given by the exception, so the error code will be set to {@link BaseQueryMetric#DEFAULT_ERROR_CODE}.</li>
* <li>The error message is set to the message passed by either the cause of the throwable (if it is not null) or the throwable itself.</li>
* </ul>
* <em>All possible error codes can be found here {@link datawave.webservice.query.exception.DatawaveErrorCode}.</em>
*
* @param t
* Object containing the exception associated with the error.
*/
public void setError(Throwable t) {
if (t.getCause() instanceof QueryException) {
QueryException qe = (QueryException) t.getCause();
this.setErrorCode(qe.getErrorCode());
this.setErrorCode(qe.getErrorCode() != null ? qe.getErrorCode() : DEFAULT_ERROR_CODE);
this.setErrorMessage(qe.getMessage());
} else {
this.setErrorCode(DEFAULT_ERROR_CODE);
this.setErrorMessage(t.getCause() != null ? t.getCause().getMessage() : t.getMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class QueryMetricTest {
private static ArrayList<PageMetric> pageTimes = null;
private static List<String> positiveSelectors = null;
private static List<String> proxyServers = null;
private static final String DEFAULT_ERROR_CODE = "500-1";

@BeforeAll
public static void setup() {
Expand Down Expand Up @@ -78,7 +79,7 @@ public void testSetError() {
Throwable t = new Throwable("non-datawave error");
queryMetric.setError(t);
assertEquals("non-datawave error", queryMetric.getErrorMessage());
assertEquals("", queryMetric.getErrorCode());
assertEquals(DEFAULT_ERROR_CODE, queryMetric.getErrorCode());
}

@Test
Expand Down
Loading