Skip to content

Commit

Permalink
optimse the error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuadkitenge committed Jan 21, 2025
1 parent 6db1d22 commit d33f462
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
13 changes: 5 additions & 8 deletions src/api/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,17 @@ 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 : error.message;

// 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')
) {
Expand Down
7 changes: 4 additions & 3 deletions src/handleOG_APIError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ 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;

Check failure on line 9 in src/handleOG_APIError.ts

View workflow job for this annotation

GitHub Actions / Lint & Unit Tests

src/handleOG_APIError.test.ts > handleOG_APIError > logs network error message if there is no response

TypeError: Cannot read properties of undefined (reading 'detail') ❯ Module.handleOG_APIError [as default] src/handleOG_APIError.ts:9:58 ❯ src/handleOG_APIError.test.ts:146:5

const message = typeof errorDetail === 'string' ? errorDetail : error.message;

log.error(message);
// Don't broadcast any error for an authentication issue - navigating via homepage links causes
Expand Down

0 comments on commit d33f462

Please sign in to comment.