Commit 541f6b9e6b3f5bd48a5cbc3658aa7749e7f4d825
1 parent
cea888b2
【增加】增加 Midjourney 提交异常提示
Showing
2 changed files
with
19 additions
and
14 deletions
yudao-module-ai/yudao-module-ai-api/src/main/java/cn/iocoder/yudao/module/ai/enums/ErrorCodeConstants.java
| ... | ... | @@ -43,5 +43,6 @@ public interface ErrorCodeConstants { |
| 43 | 43 | // ========== API 绘画 1-040-005-000 ========== |
| 44 | 44 | |
| 45 | 45 | ErrorCode AI_IMAGE_NOT_EXISTS = new ErrorCode(1_022_005_000, "image 不存在!"); |
| 46 | + ErrorCode AI_IMAGE_MIDJOURNEY_SUBMIT_FAIL = new ErrorCode(1_022_005_001, "Midjourney 提交失败! {}"); | |
| 46 | 47 | |
| 47 | 48 | } | ... | ... |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/image/AiImageServiceImpl.java
| 1 | 1 | package cn.iocoder.yudao.module.ai.service.image; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.codec.Base64; |
| 4 | +import cn.hutool.core.exceptions.ExceptionUtil; | |
| 4 | 5 | import cn.hutool.core.map.MapUtil; |
| 5 | 6 | import cn.hutool.core.util.ObjUtil; |
| 6 | 7 | import cn.hutool.core.util.StrUtil; |
| ... | ... | @@ -39,6 +40,7 @@ import org.springframework.stereotype.Service; |
| 39 | 40 | import org.springframework.transaction.annotation.Transactional; |
| 40 | 41 | |
| 41 | 42 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 43 | +import static cn.iocoder.yudao.module.ai.enums.ErrorCodeConstants.AI_IMAGE_MIDJOURNEY_SUBMIT_FAIL; | |
| 42 | 44 | import static cn.iocoder.yudao.module.ai.enums.ErrorCodeConstants.AI_IMAGE_NOT_EXISTS; |
| 43 | 45 | |
| 44 | 46 | /** |
| ... | ... | @@ -157,19 +159,12 @@ public class AiImageServiceImpl implements AiImageService { |
| 157 | 159 | // 设置参数 |
| 158 | 160 | imagineReqVO.setState(midjourneySizeParam.concat(midjourneyVersionParam).concat(midjourneyNijiParam)); |
| 159 | 161 | MidjourneySubmitRespVO submitRespVO = midjourneyProxyClient.imagine(imagineReqVO); |
| 160 | - | |
| 161 | 162 | // 4、保存任务 id (状态码: 1(提交成功), 21(已存在), 22(排队中), other(错误)) |
| 162 | - String updateStatus = null; | |
| 163 | - String errorMessage = null; | |
| 164 | - | |
| 165 | 163 | if (!MidjourneySubmitCodeEnum.SUCCESS_CODES.contains(submitRespVO.getCode())) { |
| 166 | - updateStatus = AiImageStatusEnum.FAIL.getStatus(); | |
| 167 | - errorMessage = submitRespVO.getDescription(); | |
| 164 | + throw exception(AI_IMAGE_MIDJOURNEY_SUBMIT_FAIL, submitRespVO.getDescription()); | |
| 168 | 165 | } |
| 169 | 166 | imageMapper.updateById(new AiImageDO() |
| 170 | 167 | .setId(aiImageDO.getId()) |
| 171 | - .setStatus(updateStatus) | |
| 172 | - .setErrorMessage(errorMessage) | |
| 173 | 168 | .setJobId(submitRespVO.getResult()) |
| 174 | 169 | ); |
| 175 | 170 | return aiImageDO.getId(); |
| ... | ... | @@ -194,24 +189,33 @@ public class AiImageServiceImpl implements AiImageService { |
| 194 | 189 | log.warn("midjourneyNotify 回调的 jobId 不存在! jobId: {}", notifyReqVO.getId()); |
| 195 | 190 | return false; |
| 196 | 191 | } |
| 197 | - // | |
| 192 | + // 2、转换状态 | |
| 198 | 193 | String imageStatus = null; |
| 199 | - if (MidjourneyTaskStatusEnum.SUCCESS == notifyReqVO.getStatus()) { | |
| 194 | + MidjourneyTaskStatusEnum taskStatusEnum = MidjourneyTaskStatusEnum.valueOf(notifyReqVO.getStatus()); | |
| 195 | + if (MidjourneyTaskStatusEnum.SUCCESS == taskStatusEnum) { | |
| 200 | 196 | imageStatus = AiImageStatusEnum.SUCCESS.getStatus(); |
| 201 | - } else if (MidjourneyTaskStatusEnum.FAILURE == notifyReqVO.getStatus()) { | |
| 197 | + } else if (MidjourneyTaskStatusEnum.FAILURE == taskStatusEnum) { | |
| 202 | 198 | imageStatus = AiImageStatusEnum.FAIL.getStatus(); |
| 203 | 199 | } |
| 204 | - // 2、上传图片 | |
| 200 | + // 3、上传图片 | |
| 205 | 201 | String filePath = null; |
| 206 | 202 | if (!StrUtil.isBlank(notifyReqVO.getImageUrl())) { |
| 207 | - filePath = fileApi.createFile(HttpUtil.downloadBytes(notifyReqVO.getImageUrl())); | |
| 203 | + try { | |
| 204 | + filePath = fileApi.createFile(HttpUtil.downloadBytes(notifyReqVO.getImageUrl())); | |
| 205 | + } catch (Exception e) { | |
| 206 | + log.warn("midjourneyNotify 图片上传失败! {} 异常:{}", notifyReqVO.getImageUrl(), ExceptionUtil.getMessage(e)); | |
| 207 | + } | |
| 208 | 208 | } |
| 209 | - // 2、更新 image 状态 | |
| 209 | + // 4、更新 image 状态 | |
| 210 | 210 | imageMapper.updateById( |
| 211 | 211 | new AiImageDO() |
| 212 | 212 | .setId(image.getId()) |
| 213 | 213 | .setStatus(imageStatus) |
| 214 | 214 | .setPicUrl(filePath) |
| 215 | + .setProgress(notifyReqVO.getProgress()) | |
| 216 | + .setResponse(notifyReqVO) | |
| 217 | + .setButtons(notifyReqVO.getButtons()) | |
| 218 | + .setErrorMessage(notifyReqVO.getFailReason()) | |
| 215 | 219 | ); |
| 216 | 220 | return true; |
| 217 | 221 | } | ... | ... |