From 6431ca749622f690328ac8d0a8b60007c0d62b6c Mon Sep 17 00:00:00 2001 From: Denghui Dong Date: Thu, 21 Mar 2024 00:15:46 +0800 Subject: [PATCH] chore: improve error handling logic --- frontend/src/components/Analysis.vue | 14 +++++++++----- .../src/composables/analysis-api-requester.ts | 4 ++-- frontend/src/support/utils.ts | 13 +++++++++++-- .../org/eclipse/jifa/server/util/ErrorUtil.java | 16 ++++++++++------ 4 files changed, 32 insertions(+), 15 deletions(-) diff --git a/frontend/src/components/Analysis.vue b/frontend/src/components/Analysis.vue index a653ae3e..3c4829a0 100644 --- a/frontend/src/components/Analysis.vue +++ b/frontend/src/components/Analysis.vue @@ -17,7 +17,7 @@ import { useAnalysisApiRequester } from '@/composables/analysis-api-requester'; import { ElMessageBox, ElNotification } from 'element-plus'; import type { FileType } from '@/composables/file-types'; import { t } from '@/i18n/i18n'; -import axios from 'axios'; +import axios, { AxiosError } from 'axios'; import { useHeaderToolbar } from '@/composables/header-toolbar'; import { useEnv } from '@/stores/env'; @@ -121,9 +121,13 @@ function pollProgress() { .catch(handleError); } -function handleError(error?) { - if (error) { - log.value = error; +function handleError(e?) { + if (e instanceof AxiosError) { + log.value = e.response?.data?.message ? e.response.data.message : e; + } else if (e.message) { + log.value = e.message; + } else if (e) { + log.value = e; } analysis.setPhase(Phase.FAILURE); } @@ -171,7 +175,7 @@ onMounted(() => { analyze(); } }) - .catch((e) => handleError(e.response?.data?.message ? e.response.data.message : e)); + .catch(handleError); }); onUnmounted(() => { diff --git a/frontend/src/composables/analysis-api-requester.ts b/frontend/src/composables/analysis-api-requester.ts index 667ec354..822c2972 100644 --- a/frontend/src/composables/analysis-api-requester.ts +++ b/frontend/src/composables/analysis-api-requester.ts @@ -1,5 +1,5 @@ /******************************************************************************** - * Copyright (c) 2023 Contributors to the Eclipse Foundation + * Copyright (c) 2023, 2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -158,7 +158,7 @@ function request(api: string, parameters?: object) { function requestWithTarget(api: string, type: FileType, target: string, parameters?: object) { if (!rp) { - rp = new Promise(byAxios); + rp = new Promise(byStomp); } return rp.then((requester) => { diff --git a/frontend/src/support/utils.ts b/frontend/src/support/utils.ts index d36fb15a..cfad82f0 100644 --- a/frontend/src/support/utils.ts +++ b/frontend/src/support/utils.ts @@ -1,5 +1,5 @@ /******************************************************************************** - * Copyright (c) 2023 Contributors to the Eclipse Foundation + * Copyright (c) 2023, 2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -79,12 +79,21 @@ export function prettyTime(number: number, format: string) { return format; } +let hasUnclosedError = false + export function showErrorNotification(errorCode: string, message: string) { + if (hasUnclosedError) { + return; + } + hasUnclosedError = true; ElNotification.error({ title: errorCode, message: h('p', { style: 'word-break: break-all' }, message), offset: 50, duration: 0, - showClose: true + showClose: true, + onClose() { + hasUnclosedError = false; + }, }); } diff --git a/server/src/main/java/org/eclipse/jifa/server/util/ErrorUtil.java b/server/src/main/java/org/eclipse/jifa/server/util/ErrorUtil.java index ca906e16..fec98db8 100644 --- a/server/src/main/java/org/eclipse/jifa/server/util/ErrorUtil.java +++ b/server/src/main/java/org/eclipse/jifa/server/util/ErrorUtil.java @@ -1,5 +1,5 @@ /******************************************************************************** - * Copyright (c) 2023 Contributors to the Eclipse Foundation + * Copyright (c) 2023, 2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -15,7 +15,6 @@ import com.google.gson.JsonObject; import io.netty.handler.timeout.ReadTimeoutException; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; import org.eclipse.jifa.common.domain.exception.ErrorCode; import org.eclipse.jifa.common.domain.exception.ErrorCodeAccessor; import org.eclipse.jifa.common.enums.CommonErrorCode; @@ -29,10 +28,7 @@ public class ErrorUtil { public static byte[] toJson(Throwable throwable) { JsonObject json = new JsonObject(); json.addProperty("errorCode", getErrorCodeOf(throwable).name()); - String message = getMessage(throwable); - if (StringUtils.isNotBlank(message)) { - json.addProperty("message", message); - } + json.addProperty("message", getMessage(throwable)); return json.toString().getBytes(Constant.CHARSET); } @@ -69,6 +65,14 @@ private static String getMessage(Throwable throwable) { message = fallbackMessage; } + if (message == null) { + if (throwable instanceof ErrorCodeAccessor errorCodeAccessor) { + message = errorCodeAccessor.getErrorCode().name(); + } else { + message = "Error occurred"; + } + } + try { return message; } catch (Throwable t) {