From f86bd4832d2d3f2d8e59020b414e8e6c95603e88 Mon Sep 17 00:00:00 2001 From: Joshua Kitenge Date: Tue, 21 Jan 2025 13:10:00 +0000 Subject: [PATCH] optimse the error messages --- src/api/api.tsx | 15 +++++++-------- src/handleOG_APIError.ts | 10 +++++++--- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/api/api.tsx b/src/api/api.tsx index 75a20dbd2..457c03c90 100644 --- a/src/api/api.tsx +++ b/src/api/api.tsx @@ -40,20 +40,19 @@ ogApi.interceptors.response.use( (error) => { const originalRequest = error.config; - const errorMessage: string = error.response?.data - ? Array.isArray((error.response.data as APIError).detail) - ? '' - : (( - (error.response.data as APIError).detail as string - )?.toLocaleLowerCase() ?? error.message) - : error.message; + const errorDetail = (error.response.data as APIError).detail; + + const errorMessage = + typeof errorDetail === 'string' + ? errorDetail + : 'Something went wrong, please contact the system administrator'; // Check if the token is invalid and needs refreshing // only allow a request to be retried once. Don't retry if not logged // in, it should not have been accessible if ( error.response?.status === 403 && - errorMessage.includes('expired token') && + errorMessage.includes('invalid token') && !originalRequest._retried && localStorage.getItem('scigateway:token') ) { diff --git a/src/handleOG_APIError.ts b/src/handleOG_APIError.ts index 6d8acefb4..70f837098 100644 --- a/src/handleOG_APIError.ts +++ b/src/handleOG_APIError.ts @@ -5,9 +5,13 @@ import { NotificationType } from './state/scigateway.actions'; const handleOG_APIError = (error: AxiosError, broadcast = true): void => { const status = error.response?.status; - const message = error.response?.data - ? ((error.response.data as APIError).detail ?? error.message) - : error.message; + + const errorDetail = (error.response?.data as APIError).detail; + + const message = + typeof errorDetail === 'string' + ? errorDetail + : 'Something went wrong, please contact the system administrator'; log.error(message); // Don't broadcast any error for an authentication issue - navigating via homepage links causes