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); } /**