From e0b6630e3d051c88258930dccefd7615eb439d28 Mon Sep 17 00:00:00 2001 From: Dimitris Athanasiou Date: Fri, 21 Apr 2017 16:42:39 +0100 Subject: [PATCH] [ML] Add the current job state in conflict error messages (elastic/x-pack-elasticsearch#1158) Relates elastic/x-pack-elasticsearch#878 Original commit: elastic/x-pack-elasticsearch@2be8b6c9a18ba3ac0b232e996b98511cb3917806 --- .../java/org/elasticsearch/xpack/ml/MlMetadata.java | 3 ++- .../elasticsearch/xpack/ml/action/CloseJobAction.java | 11 ++++------- .../xpack/ml/utils/ExceptionsHelper.java | 6 ++++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/MlMetadata.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/MlMetadata.java index 0cdab26f9fa..d461307988a 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/MlMetadata.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/MlMetadata.java @@ -361,7 +361,8 @@ public class MlMetadata implements MetaData.Custom { } PersistentTask jobTask = getJobTask(jobId, tasks); if (jobTask != null) { - throw ExceptionsHelper.conflictStatusException("Cannot delete job [" + jobId + "] because the job hasn't been closed"); + throw ExceptionsHelper.conflictStatusException("Cannot delete job [" + jobId + "] because the job is " + + jobTask.getStatus()); } Job.Builder jobBuilder = new Job.Builder(job); jobBuilder.setDeleted(true); diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/action/CloseJobAction.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/action/CloseJobAction.java index fe61ad00ace..e2dead6ebc6 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/action/CloseJobAction.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/action/CloseJobAction.java @@ -573,21 +573,18 @@ public class CloseJobAction extends Action jobTask = MlMetadata.getJobTask(jobId, tasks); if (jobTask == null || jobTask.getStatus() == null) { - throw new ElasticsearchStatusException("cannot close job, because job [" + jobId + "] is not open", RestStatus.CONFLICT); + throw ExceptionsHelper.conflictStatusException("cannot close job, because job [" + jobId + "] is " + JobState.CLOSED); } JobTaskStatus jobTaskStatus = (JobTaskStatus) jobTask.getStatus(); if (jobTaskStatus.getState().isAnyOf(JobState.OPENED, JobState.FAILED) == false) { - throw new ElasticsearchStatusException("cannot close job, because job [" + jobId + "] is not open", RestStatus.CONFLICT); + throw ExceptionsHelper.conflictStatusException("cannot close job, because job [" + jobId + "] is " + jobTaskStatus.getState()); } Optional datafeed = mlMetadata.getDatafeedByJobId(jobId); if (datafeed.isPresent()) { - DatafeedState datafeedState = MlMetadata.getDatafeedState(datafeed.get().getId(), - tasks); + DatafeedState datafeedState = MlMetadata.getDatafeedState(datafeed.get().getId(), tasks); if (datafeedState != DatafeedState.STOPPED) { - throw new ElasticsearchStatusException( - "cannot close job [{}], datafeed hasn't been stopped", RestStatus.CONFLICT, - jobId); + throw ExceptionsHelper.conflictStatusException("cannot close job [{}], datafeed hasn't been stopped", jobId); } } return jobTask; diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/utils/ExceptionsHelper.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/utils/ExceptionsHelper.java index 920e8560ba7..9d4e7c7c048 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/utils/ExceptionsHelper.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/utils/ExceptionsHelper.java @@ -16,6 +16,8 @@ import org.elasticsearch.xpack.ml.job.messages.Messages; public class ExceptionsHelper { + private ExceptionsHelper() {} + public static ResourceNotFoundException missingJobException(String jobId) { return new ResourceNotFoundException(Messages.getMessage(Messages.JOB_UNKNOWN_ID, jobId)); } @@ -36,8 +38,8 @@ public class ExceptionsHelper { return new ElasticsearchException(msg, cause); } - public static ElasticsearchStatusException conflictStatusException(String msg) { - return new ElasticsearchStatusException(msg, RestStatus.CONFLICT); + public static ElasticsearchStatusException conflictStatusException(String msg, Object... args) { + return new ElasticsearchStatusException(msg, RestStatus.CONFLICT, args); } /**