diff --git a/continew-plugin/continew-plugin-schedule/src/main/java/top/continew/admin/schedule/api/JobClient.java b/continew-plugin/continew-plugin-schedule/src/main/java/top/continew/admin/schedule/api/JobClient.java index 96e68e0f..26a362f4 100644 --- a/continew-plugin/continew-plugin-schedule/src/main/java/top/continew/admin/schedule/api/JobClient.java +++ b/continew-plugin/continew-plugin-schedule/src/main/java/top/continew/admin/schedule/api/JobClient.java @@ -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 request(Supplier> apiSupplier) { Result 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 PageResp requestPage(Supplier>> apiSupplier) { JobPageResult> result = apiSupplier.get(); if (!STATUS_SUCCESS.equals(result.getStatus())) { - throw new IllegalStateException(result.getMessage()); + throw new ScheduleClientException(result.getMessage()); } PageResp 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"); } } diff --git a/continew-plugin/continew-plugin-schedule/src/main/java/top/continew/admin/schedule/exception/ScheduleClientException.java b/continew-plugin/continew-plugin-schedule/src/main/java/top/continew/admin/schedule/exception/ScheduleClientException.java new file mode 100644 index 00000000..16dfd119 --- /dev/null +++ b/continew-plugin/continew-plugin-schedule/src/main/java/top/continew/admin/schedule/exception/ScheduleClientException.java @@ -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); + } +} diff --git a/continew-plugin/continew-plugin-schedule/src/main/java/top/continew/admin/schedule/exception/ScheduleServerException.java b/continew-plugin/continew-plugin-schedule/src/main/java/top/continew/admin/schedule/exception/ScheduleServerException.java new file mode 100644 index 00000000..f4f50771 --- /dev/null +++ b/continew-plugin/continew-plugin-schedule/src/main/java/top/continew/admin/schedule/exception/ScheduleServerException.java @@ -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); + } +}