Improve detection of User Profile related errors (#24982)

Closes #24958

Signed-off-by: Jon Koops <jonkoops@gmail.com>
This commit is contained in:
Jon Koops 2023-11-24 10:55:39 +01:00 committed by GitHub
parent 48fc29a5c6
commit 9c92446966
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -105,7 +105,52 @@ function hasRequiredValidators(
} }
export function isUserProfileError(error: unknown): error is UserProfileError { export function isUserProfileError(error: unknown): error is UserProfileError {
return !!(error as UserProfileError).responseData; // Check if the error is an object with a 'responseData' property.
if (
typeof error !== "object" ||
error === null ||
!("responseData" in error)
) {
return false;
}
const { responseData } = error;
if (isFieldError(responseData)) {
return true;
}
// Check if 'responseData' is an object with an 'errors' property that is an array.
if (
typeof responseData !== "object" ||
responseData === null ||
!("errors" in responseData) ||
!Array.isArray(responseData.errors)
) {
return false;
}
// Check if all errors are field errors.
return responseData.errors.every(isFieldError);
}
function isFieldError(error: unknown): error is FieldError {
// Check if the error is an object.
if (typeof error !== "object" || error === null) {
return false;
}
// Check if the error object has a 'field' property that is a string.
if (!("field" in error) || typeof error.field !== "string") {
return false;
}
// Check if the error object has an 'errorMessage' property that is a string.
if (!("errorMessage" in error) || typeof error.errorMessage !== "string") {
return false;
}
return true;
} }
export type TranslationFunction = (key: unknown, params?: object) => string; export type TranslationFunction = (key: unknown, params?: object) => string;