From b5321f8d2214e4e4fd7b25e615d44eb54c7aa04e Mon Sep 17 00:00:00 2001 From: mauvaisetroupe Date: Tue, 23 Apr 2024 12:15:21 +0200 Subject: [PATCH] Improve message error in toast popup windows Add a guessError function in AlertSerice to catch intergity violation during delete Fix #150 --- src/main/webapp/app/main.ts | 4 ++++ .../webapp/app/shared/alert/alert.service.ts | 21 ++++++++++++++++--- .../app/shared/config/axios-interceptor.ts | 9 ++++---- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/main/webapp/app/main.ts b/src/main/webapp/app/main.ts index 2f733cba..94d9fdb3 100644 --- a/src/main/webapp/app/main.ts +++ b/src/main/webapp/app/main.ts @@ -133,6 +133,8 @@ const app = createApp({ setupAxiosInterceptors( error => { + console.error('Error onUnauthenticated'); + console.error(error); const url = error.response?.config?.url; const status = error.status || error.response.status; if (status === 401) { @@ -147,6 +149,8 @@ const app = createApp({ return Promise.reject(error); }, error => { + console.error('Error onServerError'); + console.log(error); return Promise.reject(error); }, ); diff --git a/src/main/webapp/app/shared/alert/alert.service.ts b/src/main/webapp/app/shared/alert/alert.service.ts index 14bdefe7..b1eeb8a4 100644 --- a/src/main/webapp/app/shared/alert/alert.service.ts +++ b/src/main/webapp/app/shared/alert/alert.service.ts @@ -54,12 +54,12 @@ export default class AlertService { this.showHttpError(error.response); } else { console.error(error); - this.showError('Unknow Error, please consult console errors to report a bug'); + this.showError('Unknow Error, please consult browser console errors to report a bug'); } } public showHttpError(httpErrorResponse: any) { - console.log(httpErrorResponse.type); + console.log(httpErrorResponse); let errorMessage: string | null = null; switch (httpErrorResponse.status) { case 0: @@ -81,13 +81,28 @@ export default class AlertService { break; } + case 500: { + errorMessage = + 'Server responded with a status of 500 (Internal Server Error). Please consult browser console errors to report a bug'; + break; + } + case 404: errorMessage = 'The page does not exist.'; break; default: - errorMessage = httpErrorResponse.data.message; + errorMessage = httpErrorResponse?.data?.message; } + errorMessage = guessError(errorMessage, httpErrorResponse); this.showError(errorMessage); } } + +function guessError(errorMessage: string | null, httpErrorResponse: any): string | null { + const concatString = httpErrorResponse?.data?.message + ' ' + httpErrorResponse?.data?.detail; + if (concatString.toLowerCase().includes('constraint violation')) { + return 'Error during operation : integrity constraint violation'; + } + return errorMessage; +} diff --git a/src/main/webapp/app/shared/config/axios-interceptor.ts b/src/main/webapp/app/shared/config/axios-interceptor.ts index ebe4a1df..a5beb63b 100644 --- a/src/main/webapp/app/shared/config/axios-interceptor.ts +++ b/src/main/webapp/app/shared/config/axios-interceptor.ts @@ -17,10 +17,11 @@ const setupAxiosInterceptors = (onUnauthenticated, onServerError) => { const onResponseError = err => { const status = err.status || err.response.status; if (status === 403 || status === 401) { - onUnauthenticated(); - } - if (status >= 500) { - onServerError(); + onUnauthenticated(err); + } else if (status >= 500) { + onServerError(err); + } else { + onServerError(err); } return Promise.reject(err); };