refactor(schedule): 新增自定义异常优化任务管理错误提示

This commit is contained in:
2025-05-21 22:24:05 +08:00
parent a07a2dcd65
commit 9d6d7984d5
3 changed files with 90 additions and 12 deletions

View File

@@ -18,6 +18,7 @@ package top.continew.admin.schedule.api;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
@@ -32,6 +33,8 @@ import com.aizuda.snailjob.common.core.model.Result;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import top.continew.admin.schedule.constant.JobConstants;
import top.continew.admin.schedule.exception.ScheduleClientException;
import top.continew.admin.schedule.exception.ScheduleServerException;
import top.continew.admin.schedule.model.JobPageResult;
import top.continew.starter.cache.redisson.util.RedisUtils;
import top.continew.starter.extension.crud.model.resp.PageResp;
@@ -76,7 +79,7 @@ public class JobClient {
public <T> T request(Supplier<Result<T>> apiSupplier) {
Result<T> result = apiSupplier.get();
if (!STATUS_SUCCESS.equals(result.getStatus())) {
throw new IllegalStateException(result.getMessage());
throw new ScheduleClientException(result.getMessage());
}
return result.getData();
}
@@ -91,7 +94,7 @@ public class JobClient {
public <T> PageResp<T> requestPage(Supplier<JobPageResult<List<T>>> apiSupplier) {
JobPageResult<List<T>> result = apiSupplier.get();
if (!STATUS_SUCCESS.equals(result.getStatus())) {
throw new IllegalStateException(result.getMessage());
throw new ScheduleClientException(result.getMessage());
}
PageResp<T> page = new PageResp<>();
page.setList(result.getData());
@@ -126,16 +129,19 @@ public class JobClient {
paramMap.put("password", SecureUtil.md5(password));
HttpRequest httpRequest = HttpUtil.createPost("%s%s".formatted(url, AUTH_URL));
httpRequest.body(JSONUtil.toJsonStr(paramMap));
HttpResponse response = httpRequest.execute();
if (!response.isOk() || response.body() == null) {
throw new IllegalStateException("连接任务调度中心异常");
try (HttpResponse response = httpRequest.execute()) {
if (!response.isOk() || response.body() == null) {
throw new ScheduleServerException("连接任务调度中心异常");
}
Result<?> result = JSONUtil.toBean(response.body(), Result.class);
if (!STATUS_SUCCESS.equals(result.getStatus())) {
log.warn("Password Authentication failed, expected a successful response. error msg: {}", result
.getMessage());
throw new ScheduleServerException(result.getMessage());
}
return JSONUtil.parseObj(result.getData()).getStr("token");
} catch (IORuntimeException e) {
throw new ScheduleServerException("无法连接任务调度中心,请检查调度中心服务");
}
Result<?> result = JSONUtil.toBean(response.body(), Result.class);
if (!STATUS_SUCCESS.equals(result.getStatus())) {
log.warn("Password Authentication failed, expected a successful response. error msg: {}", result
.getMessage());
throw new IllegalStateException(result.getMessage());
}
return JSONUtil.parseObj(result.getData()).getStr("token");
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.continew.admin.schedule.exception;
import top.continew.starter.core.exception.BaseException;
/**
* 调度客户端异常
*
* @author Charles7c
* @since 2025/5/21 22:05
*/
public class ScheduleClientException extends BaseException {
public ScheduleClientException(String message) {
super(message);
}
public ScheduleClientException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.continew.admin.schedule.exception;
import top.continew.starter.core.exception.BaseException;
/**
* 调度服务异常
*
* @author Charles7c
* @since 2025/5/21 22:05
*/
public class ScheduleServerException extends BaseException {
public ScheduleServerException(String message) {
super(message);
}
public ScheduleServerException(String message, Throwable cause) {
super(message, cause);
}
}