Skip to content

Commit

Permalink
Added code to display error message from grapqhl response in form
Browse files Browse the repository at this point in the history
  • Loading branch information
del22123 committed Oct 17, 2024
1 parent 78280c8 commit 6d696f3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
22 changes: 21 additions & 1 deletion packages/peregrine/lib/talons/FormError/useFormError.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,27 @@ export const useFormError = props => {
defaultMessage:
'An error has occurred. Please check the input and try again.'
});
return deriveErrorMessage(errors, defaultErrorMessage);

const firstError = errors
.filter(error => error !== null||undefined)
.map(error => Array.isArray(error) ? error[0] : error)
.find(message => message);
var graphqlErrorMessage;

if (firstError) {
var category = firstError.graphQLErrors?.find(extensions => extensions).extensions?.category;
category = category ? category
.split('-')
.map((word, index) => index === 0 ? word : word.charAt(0).toUpperCase() + word.slice(1))
.join('') : null;
const errorId = category ? 'formError.'+category : 'formError.responseError';
graphqlErrorMessage = formatMessage({
id: errorId,
defaultMessage: firstError.message
});
}

return (graphqlErrorMessage) ? deriveErrorMessage(errors, graphqlErrorMessage) : deriveErrorMessage(errors, defaultErrorMessage);
}, [errors, formatMessage, allowErrorMessages]);

return {
Expand Down
1 change: 1 addition & 0 deletions packages/venia-ui/i18n/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@
"forgotPasswordPage.header": "Forgot Your Password?",
"forgotPasswordPage.title": "Forgot Your Password?",
"formError.errorMessage": "An error has occurred. Please check the input and try again.",
"formError.graphqlAuthenticationEmailNotConfirmed": "Your account is created, You must confirm your account. Please check your email for the confirmation link.",
"formSubmissionSuccessful.recoverPasswordText": "Recover Password",
"formSubmissionSuccessful.textMessage": "If there is an account associated with {email} you will receive an email with a link to change your password.",
"galleryItem.unavailableProduct": "Currently unavailable for purchase.",
Expand Down

0 comments on commit 6d696f3

Please sign in to comment.