Skip to content

Commit

Permalink
Improve message error in toast popup windows
Browse files Browse the repository at this point in the history
Add a guessError function in AlertSerice to catch intergity violation during delete
Fix #150
  • Loading branch information
mauvaisetroupe committed Apr 23, 2024
1 parent c3e16c6 commit b5321f8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/main/webapp/app/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -147,6 +149,8 @@ const app = createApp({
return Promise.reject(error);
},
error => {
console.error('Error onServerError');
console.log(error);
return Promise.reject(error);
},
);
Expand Down
21 changes: 18 additions & 3 deletions src/main/webapp/app/shared/alert/alert.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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;
}
9 changes: 5 additions & 4 deletions src/main/webapp/app/shared/config/axios-interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down

0 comments on commit b5321f8

Please sign in to comment.