Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(v2): add clearer error messages when updating collaborators #3864

Merged

Conversation

siddarth2824
Copy link
Collaborator

Problem

Currently the error messages when users attempt to update collaborator list on admin form are not user friendly.

Closes [#3851]

Solution

Update the client side to have a mapping between status codes and user friendly error messages displayed.

Comment on lines 119 to 120
requestEmail = typeof requestEmail !== 'undefined' ? requestEmail : ''
errorMessage = `${requestEmail} is not part of a whitelisted agency`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message will not be correct if requestEmail is an empty string. You probably need a conditional block to format 2 distinct error messages.

If requestEmail is this block can in fact never be undefined and converted to empty string, let's try to make that explicit in code.

let errorMessage
switch (error.code) {
case 422:
errorMessage = `${!requestEmail} is not part of a whitelisted agency`
Copy link
Contributor

@timotheeg timotheeg May 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ! a not a non-null assertion, it’s the logical not operator. It will convert whatever truthiness requestEmail represents to its opposite true or false, and so the resulting message will be either :

true is not part of a whitelisted agency

or

false is not part of a whitelisted agency

which is not what we want 😅 .

If requestEmail is always defined on 422 errors, we can make it clear with a cast:
errorMessage = ${requestEmail as string} is not part of a whitelisted agency

If requestEmail may be undefined, we’d be better off with a conditional block:

errorMessage = requestEmail
    ? `${requestEmail} is not part of a whitelisted agency`
    : `An unexpected error 422 happened`

One small issue I have with doing a straight cast in this case is code self-consistency and implicit assumptions. Maybe we can try to make things a little clearer 🤔 . Based on the call tree of getMappedErrorMessage(), requestEmail is only supplied if formCollaboratorAction is FormCollaboratorAction.ADD. The 422 error code is not inspected in a context where formCollaboratorAction has been checked, so if we just cast requestEmail as string to show we know it's true, we've implicitly stated only FormCollaboratorAction.ADD returns 422. It may be true, but it's a bit dirty and not obvious from the code itself (the function signature shows requestEmail can be undefined).

Long story short: I'd recommend going the defensive programming route here. Either do a check to verify requestEmail is not undefined like above, and have an error message for unexpected cases. Or check on formCollaboratorAction first, and then cast to string, and you likely still need an error message for unexpected cases.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the detailed explanation. I initially assumed that it was okay to add an empty string if in the case requestEmail is empty because the 422 error thrown only occurs for FormCollaboratorAction.Add. I do realise now that it is not clear in the code that that is the case and it is better just to do a simple non-null check and throw a different error if requestEmail is null.

Copy link
Contributor

@timotheeg timotheeg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@siddarth2824 siddarth2824 merged commit c8e77f6 into form-v2/develop May 19, 2022
@siddarth2824 siddarth2824 deleted the form-v2/feat/refactor-collab-error-messages branch May 19, 2022 02:05
@justynoh justynoh mentioned this pull request Oct 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants