Skip to content

Commit

Permalink
chore: improve error handling logic
Browse files Browse the repository at this point in the history
  • Loading branch information
D-D-H committed Mar 20, 2024
1 parent eacea2e commit 6431ca7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
14 changes: 9 additions & 5 deletions frontend/src/components/Analysis.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -171,7 +175,7 @@ onMounted(() => {
analyze();
}
})
.catch((e) => handleError(e.response?.data?.message ? e.response.data.message : e));
.catch(handleError);
});
onUnmounted(() => {
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/composables/analysis-api-requester.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<Requester>(byAxios);
rp = new Promise<Requester>(byStomp);
}

return rp.then((requester) => {
Expand Down
13 changes: 11 additions & 2 deletions frontend/src/support/utils.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
},
});
}
16 changes: 10 additions & 6 deletions server/src/main/java/org/eclipse/jifa/server/util/ErrorUtil.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -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);
}

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 6431ca7

Please sign in to comment.