Skip to content

Commit 715e039

Browse files
committed
sanitize index errors
1 parent 1717b95 commit 715e039

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

packages/compass-indexes/src/modules/regular-indexes.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,18 @@ export const INITIAL_STATE: State = {
182182
error: undefined,
183183
};
184184

185+
function processError(error: string): string {
186+
const internalCodePattern = /[A-Z_]+:/; // Matches all caps with underscores followed by a colon anywhere in the string
187+
const httpCodePattern = /\b(4\d{2}|5\d{2})\b/; // Matches HTTP codes 400-599 anywhere in the string
188+
189+
if (internalCodePattern.test(error) || httpCodePattern.test(error)) {
190+
console.log(error);
191+
return 'Sorry, an unexpected error has occurred. Try refreshing the page or reporting the issue.';
192+
}
193+
194+
return error; // Return original error if it doesn't match the patterns
195+
}
196+
185197
export default function reducer(
186198
state = INITIAL_STATE,
187199
action: AnyAction
@@ -248,7 +260,9 @@ export default function reducer(
248260
// previous list of indexes is shown to the user.
249261
// If fetch fails for refresh or polling, set the status to READY again.
250262
error:
251-
state.status === FetchStatuses.FETCHING ? action.error : state.error,
263+
state.status === FetchStatuses.FETCHING
264+
? processError(action.error)
265+
: state.error,
252266
status:
253267
state.status === FetchStatuses.FETCHING
254268
? FetchStatuses.ERROR

0 commit comments

Comments
 (0)