From bafcbc66e3e34888ea8fd1d62f40dfc10e51342c Mon Sep 17 00:00:00 2001 From: Jeff Zhu Date: Sat, 7 Apr 2018 16:54:23 +0800 Subject: [PATCH] =?UTF-8?q?#387=20=E5=A2=9E=E5=8A=A0=E5=AF=B9=E5=89=8D?= =?UTF-8?q?=E7=BC=80=E4=B8=BA=E2=80=9C=E4=B8=8D=E6=AD=A3=E7=A1=AE=E7=9A=84?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E6=88=96=E5=8F=82=E6=95=B0=E2=80=9D=E7=9A=84?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86=EF=BC=8C=E4=BD=BF=E4=B9=8B?= =?UTF-8?q?=E8=BF=94=E5=9B=9E400=E3=80=82=E5=90=8C=E6=97=B6=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=97=A5=E5=BF=97=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RestApiExceptionHandlerController.java | 36 ++++++++++++++++-- .../console/service/impl/JobServiceImpl.java | 37 ++++++++++--------- .../job/console/utils/SaturnConstants.java | 2 + .../JobOperationRestApiControllerTest.java | 14 +++++++ 4 files changed, 68 insertions(+), 21 deletions(-) diff --git a/saturn-console-api/src/main/java/com/vip/saturn/job/console/controller/rest/RestApiExceptionHandlerController.java b/saturn-console-api/src/main/java/com/vip/saturn/job/console/controller/rest/RestApiExceptionHandlerController.java index 6d9426047..262e83d50 100644 --- a/saturn-console-api/src/main/java/com/vip/saturn/job/console/controller/rest/RestApiExceptionHandlerController.java +++ b/saturn-console-api/src/main/java/com/vip/saturn/job/console/controller/rest/RestApiExceptionHandlerController.java @@ -1,9 +1,13 @@ package com.vip.saturn.job.console.controller.rest; +import com.vip.saturn.job.console.controller.gui.AbstractGUIController; import com.vip.saturn.job.console.domain.RestApiErrorResult; import com.vip.saturn.job.console.exception.SaturnJobConsoleException; import com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException; +import com.vip.saturn.job.console.utils.SaturnConstants; import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -21,6 +25,8 @@ @ControllerAdvice public class RestApiExceptionHandlerController { + private static final Logger log = LoggerFactory.getLogger(RestApiExceptionHandlerController.class); + private final static String NOT_EXISTED_PREFIX = "does not exists"; @ExceptionHandler @@ -31,9 +37,15 @@ public ResponseEntity handleSaturnJobConsoleException(SaturnJobConsoleEx } if (message.contains(NOT_EXISTED_PREFIX)) { + log.warn("Resource not found while calling REST API:" + message); return constructErrorResponse(message, HttpStatus.NOT_FOUND); + } else if (message.startsWith(SaturnConstants.INVALID_PARAMETER_PREFIX)) { + String tmpMsg = StringUtils.removeStart(message, SaturnConstants.INVALID_PARAMETER_PREFIX); + log.warn("Bad request while calling REST API:" + tmpMsg); + return constructErrorResponse(tmpMsg, HttpStatus.BAD_REQUEST); } + log.error("Internal server error happens while calling REST API:" + message); return constructErrorResponse(message, HttpStatus.INTERNAL_SERVER_ERROR); } @@ -41,14 +53,30 @@ public ResponseEntity handleSaturnJobConsoleException(SaturnJobConsoleEx public ResponseEntity handleSaturnJobConsoleHttpException(SaturnJobConsoleHttpException e) { HttpHeaders httpHeaders = new HttpHeaders(); - SaturnJobConsoleHttpException saturnJobConsoleHttpException = (SaturnJobConsoleHttpException) e; - int statusCode = saturnJobConsoleHttpException.getStatusCode(); - + int statusCode = e.getStatusCode(); if (statusCode == HttpStatus.CREATED.value()) { return new ResponseEntity<>(httpHeaders, HttpStatus.CREATED); } - return constructErrorResponse(e.getMessage(), HttpStatus.valueOf(statusCode)); + HttpStatus httpStatus; + try { + httpStatus = HttpStatus.valueOf(statusCode); + } catch (IllegalArgumentException e1) { + httpStatus = HttpStatus.INTERNAL_SERVER_ERROR; + } + + String message = e.getMessage(); + if (StringUtils.isBlank(message)) { + message = e.toString(); + } + + if (httpStatus.is5xxServerError()) { + log.error("Internal server error happens while calling REST API:" + message); + } else { + log.warn("Exception happens while calling REST API:" + message); + } + + return constructErrorResponse(message, httpStatus); } @ExceptionHandler diff --git a/saturn-console-api/src/main/java/com/vip/saturn/job/console/service/impl/JobServiceImpl.java b/saturn-console-api/src/main/java/com/vip/saturn/job/console/service/impl/JobServiceImpl.java index aa357a6be..bfe7de991 100644 --- a/saturn-console-api/src/main/java/com/vip/saturn/job/console/service/impl/JobServiceImpl.java +++ b/saturn-console-api/src/main/java/com/vip/saturn/job/console/service/impl/JobServiceImpl.java @@ -43,6 +43,8 @@ import java.text.ParseException; import java.util.*; +import static com.vip.saturn.job.console.utils.SaturnConstants.INVALID_PARAMETER_PREFIX; + /** * @author hebelala */ @@ -82,6 +84,7 @@ public class JobServiceImpl implements JobService { private static final int DEFAULT_MAX_JOB_NUM = 100; private static final int DEFAULT_INTERVAL_TIME_OF_ENABLED_REPORT = 5; private static final String ERR_MSG_PENDING_STATUS = "job:[{}] item:[{}] on executor:[{}] execution status is PENDING as {}"; + @Resource private RegistryCenterService registryCenterService; @@ -526,30 +529,30 @@ public void setPreferList(String namespace, String jobName, String preferList, S private void validateJobConfig(JobConfig jobConfig) throws SaturnJobConsoleException { // 作业名必填 if (jobConfig.getJobName() == null || jobConfig.getJobName().trim().isEmpty()) { - throw new SaturnJobConsoleException("作业名必填"); + throw new SaturnJobConsoleException(INVALID_PARAMETER_PREFIX + "作业名必填"); } // 作业名只允许包含:数字0-9、小写字符a-z、大写字符A-Z、下划线_ if (!jobConfig.getJobName().matches("[0-9a-zA-Z_]*")) { - throw new SaturnJobConsoleException("作业名只允许包含:数字0-9、小写字符a-z、大写字符A-Z、下划线_"); + throw new SaturnJobConsoleException(INVALID_PARAMETER_PREFIX + "作业名只允许包含:数字0-9、小写字符a-z、大写字符A-Z、下划线_"); } // 依赖的作业只允许包含:数字0-9、小写字符a-z、大写字符A-Z、下划线_、英文逗号, if (jobConfig.getDependencies() != null && !jobConfig.getDependencies().matches("[0-9a-zA-Z_,]*")) { - throw new SaturnJobConsoleException("依赖的作业只允许包含:数字0-9、小写字符a-z、大写字符A-Z、下划线_、英文逗号,"); + throw new SaturnJobConsoleException(INVALID_PARAMETER_PREFIX + "依赖的作业只允许包含:数字0-9、小写字符a-z、大写字符A-Z、下划线_、英文逗号,"); } // 作业类型必填 if (jobConfig.getJobType() == null || jobConfig.getJobType().trim().isEmpty()) { - throw new SaturnJobConsoleException("作业类型必填"); + throw new SaturnJobConsoleException(INVALID_PARAMETER_PREFIX + "作业类型必填"); } // 验证作业类型 if (JobType.getJobType(jobConfig.getJobType()).equals(JobType.UNKOWN_JOB)) { - throw new SaturnJobConsoleException("作业类型未知"); + throw new SaturnJobConsoleException(INVALID_PARAMETER_PREFIX + "作业类型未知"); } // 如果是JAVA作业 if ((jobConfig.getJobType().equals(JobType.JAVA_JOB.name()) || jobConfig.getJobType() .equals(JobType.MSG_JOB.name())) && (jobConfig.getJobClass() == null || jobConfig.getJobClass().trim() .isEmpty())) { // 作业实现类必填 - throw new SaturnJobConsoleException("对于JAVA或消息作业,作业实现类必填"); + throw new SaturnJobConsoleException(INVALID_PARAMETER_PREFIX + "对于JAVA或消息作业,作业实现类必填"); } validateCronFieldOfJobConfig(jobConfig); @@ -557,7 +560,7 @@ private void validateJobConfig(JobConfig jobConfig) throws SaturnJobConsoleExcep // 不能添加系统作业 if (jobConfig.getJobMode() != null && jobConfig.getJobMode().startsWith(JobMode.SYSTEM_PREFIX)) { - throw new SaturnJobConsoleException("作业模式有误,不能添加系统作业"); + throw new SaturnJobConsoleException(INVALID_PARAMETER_PREFIX + "作业模式有误,不能添加系统作业"); } } @@ -567,13 +570,13 @@ private void validateCronFieldOfJobConfig(JobConfig jobConfig) throws SaturnJobC .equals(JobType.SHELL_JOB.name())) { // cron表达式必填 if (jobConfig.getCron() == null || jobConfig.getCron().trim().isEmpty()) { - throw new SaturnJobConsoleException("对于JAVA/SHELL作业,cron表达式必填"); + throw new SaturnJobConsoleException(INVALID_PARAMETER_PREFIX + "对于JAVA/SHELL作业,cron表达式必填"); } // cron表达式语法验证 try { CronExpression.validateExpression(jobConfig.getCron()); } catch (ParseException e) { - throw new SaturnJobConsoleException("cron表达式语法有误" + e); + throw new SaturnJobConsoleException(INVALID_PARAMETER_PREFIX + "cron表达式语法有误" + e); } } else { jobConfig.setCron(""); // 其他类型的不需要持久化保存cron表达式 @@ -583,7 +586,7 @@ private void validateCronFieldOfJobConfig(JobConfig jobConfig) throws SaturnJobC private void validateShardingItemFieldOfJobConfig(JobConfig jobConfig) throws SaturnJobConsoleException { if (jobConfig.getLocalMode() != null && jobConfig.getLocalMode()) { if (jobConfig.getShardingItemParameters() == null) { - throw new SaturnJobConsoleException("对于本地模式作业,分片参数必填。"); + throw new SaturnJobConsoleException(INVALID_PARAMETER_PREFIX + "对于本地模式作业,分片参数必填。"); } else { String[] split = jobConfig.getShardingItemParameters().split(","); boolean includeXing = false; @@ -595,18 +598,18 @@ private void validateShardingItemFieldOfJobConfig(JobConfig jobConfig) throws Sa } } if (!includeXing) { - throw new SaturnJobConsoleException("对于本地模式作业,分片参数必须包含如*=xx。"); + throw new SaturnJobConsoleException(INVALID_PARAMETER_PREFIX + "对于本地模式作业,分片参数必须包含如*=xx。"); } } } else { // 分片参数不能小于分片总数 if (jobConfig.getShardingTotalCount() == null || jobConfig.getShardingTotalCount() < 1) { - throw new SaturnJobConsoleException("分片数不能为空,并且不能小于1"); + throw new SaturnJobConsoleException(INVALID_PARAMETER_PREFIX + "分片数不能为空,并且不能小于1"); } if ((jobConfig.getShardingTotalCount() > 0) && (jobConfig.getShardingItemParameters() == null || jobConfig .getShardingItemParameters().trim().isEmpty() || jobConfig.getShardingItemParameters().split(",").length < jobConfig.getShardingTotalCount())) { - throw new SaturnJobConsoleException("分片参数不能小于分片总数"); + throw new SaturnJobConsoleException(INVALID_PARAMETER_PREFIX + "分片参数不能小于分片总数"); } } } @@ -630,11 +633,11 @@ private void addOrCopyJob(String namespace, JobConfig jobConfig, String jobNameC String jobName = jobConfig.getJobName(); JobConfig4DB oldJobConfig = currentJobConfigService.findConfigByNamespaceAndJobName(namespace, jobName); if (oldJobConfig != null) { - throw new SaturnJobConsoleException(String.format("该作业(%s)已经存在", jobName)); + throw new SaturnJobConsoleException(INVALID_PARAMETER_PREFIX + String.format("该作业(%s)已经存在", jobName)); } int maxJobNum = getMaxJobNum(); if (jobIncExceeds(namespace, maxJobNum, 1)) { - throw new SaturnJobConsoleException(String.format("总作业数超过最大限制(%d),作业名%s创建失败", maxJobNum, jobName)); + throw new SaturnJobConsoleException(INVALID_PARAMETER_PREFIX + String.format("总作业数超过最大限制(%d),作业名%s创建失败", maxJobNum, jobName)); } else { if (jobNameCopied == null) { persistJob(namespace, jobConfig, createdBy); @@ -1615,7 +1618,7 @@ public void updateJobCron(String namespace, String jobName, String cron, Map