ExpediaGroup/adaptive-alerting

DetectorRepositoryImpl.findByUuid() assumes at least one result

williewheeler opened this issue · 1 comments

In the following code

    @Override
    public DetectorDocument findByUuid(String uuid) {
        val queryBuilder = QueryBuilders.termQuery("uuid", uuid);
        val searchSourceBuilder = elasticsearchUtil.getSourceBuilder(queryBuilder).size(DEFAULT_ES_RESULTS_SIZE);
        val searchRequest = elasticsearchUtil.getSearchRequest(searchSourceBuilder, DETECTOR_INDEX, DETECTOR_DOC_TYPE);
        return getDetectorsFromElasticSearch(searchRequest).get(0);
    }

the last line assumes that there will be at least one detector in the search results, which need not be the case. And we are generating a stack trace over it:

Oct 29, 2019 3:02:02 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IndexOutOfBoundsException: Index: 0, Size: 0] with root cause
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
	at java.util.ArrayList.rangeCheck(ArrayList.java:657)
	at java.util.ArrayList.get(ArrayList.java:433)
	at com.expedia.adaptivealerting.modelservice.repo.impl.DetectorRepositoryImpl.findByUuid(DetectorRepositoryImpl.java:144)
	at com.expedia.adaptivealerting.modelservice.web.DetectorController.findByUuid(DetectorController.java:55)
	at sun.reflect.GeneratedMethodAccessor82.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)

[snip]

Instead this method should handle a missing detector gracefully -- probably by throwing some kind of "not found" exception.

I'm picking up this issue.