diff --git a/x-pack/plugins/ml/server/routes/annotations.js b/x-pack/plugins/ml/server/routes/annotations.js index bfb3a439a30d..e7945cf6b1f9 100644 --- a/x-pack/plugins/ml/server/routes/annotations.js +++ b/x-pack/plugins/ml/server/routes/annotations.js @@ -4,14 +4,24 @@ * you may not use this file except in compliance with the Elastic License. */ +import Boom from 'boom'; import _ from 'lodash'; +import { i18n } from '@kbn/i18n'; import { callWithRequestFactory } from '../client/call_with_request_factory'; +import { isAnnotationsFeatureAvailable } from '../lib/check_annotations'; import { wrapError } from '../client/errors'; import { annotationServiceProvider } from '../models/annotation_service'; import { ANNOTATION_USER_UNKNOWN } from '../../common/constants/annotations'; +function getAnnotationsFeatureUnavailableErrorMessage() { + return Boom.badRequest( + i18n.translate('xpack.ml.routes.annotations.annotationsFeatureUnavailableErrorMessage', { + defaultMessage: 'Index and aliases required for the annotations feature have not been created.', + }) + ); +} export function annotationRoutes(server, commonRouteConfig) { server.route({ method: 'POST', @@ -30,7 +40,12 @@ export function annotationRoutes(server, commonRouteConfig) { server.route({ method: 'PUT', path: '/api/ml/annotations/index', - handler(request) { + async handler(request) { + const annotationsFeatureAvailable = await isAnnotationsFeatureAvailable(server); + if (annotationsFeatureAvailable === false) { + return getAnnotationsFeatureUnavailableErrorMessage(); + } + const callWithRequest = callWithRequestFactory(server, request); const { indexAnnotation } = annotationServiceProvider(callWithRequest); const username = _.get(request, 'auth.credentials.username', ANNOTATION_USER_UNKNOWN); @@ -45,7 +60,12 @@ export function annotationRoutes(server, commonRouteConfig) { server.route({ method: 'DELETE', path: '/api/ml/annotations/delete/{annotationId}', - handler(request) { + async handler(request) { + const annotationsFeatureAvailable = await isAnnotationsFeatureAvailable(server); + if (annotationsFeatureAvailable === false) { + return getAnnotationsFeatureUnavailableErrorMessage(); + } + const callWithRequest = callWithRequestFactory(server, request); const annotationId = request.params.annotationId; const { deleteAnnotation } = annotationServiceProvider(callWithRequest);