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

Enhancement: Generic method to reduce errors for different scenarios. #19

Open
sfdcschwabe opened this issue Dec 9, 2020 · 3 comments
Labels
enhancement New feature or request

Comments

@sfdcschwabe
Copy link
Contributor

Related to this article named Error Handling Best Practices for Lightning Web Components I would like to suggest to create a reusable component like the below one.

It could be used in the following components:

  • formulaShareRuleDetail.js:
  • formulaShareRulesListView.js:

`/**

  • Reduces one or more LDS errors into a string[] of error messages.

  • @param {FetchResponse|FetchResponse[]} errors

  • @return {String[]} Error messages
    */
    export function reduceErrors(errors) {
    if (!Array.isArray(errors)) {
    errors = [errors];
    }

    return (
    errors
    // Remove null/undefined items
    .filter((error) => !!error)
    // Extract an error message
    .map((error) => {
    // UI API read errors
    if (Array.isArray(error.body)) {
    return error.body.map((e) => e.message);
    }
    // UI API DML, Apex and network errors
    else if (error.body && typeof error.body.message === 'string') {
    return error.body.message;
    }
    // JS errors
    else if (typeof error.message === 'string') {
    return error.message;
    }
    // Unknown error shape so try HTTP status text
    return error.statusText;
    })
    // Flatten
    .reduce((prev, curr) => prev.concat(curr), [])
    // Remove empty strings
    .filter((message) => !!message)
    );
    }`

Source: https://github.com/trailheadapps/lwc-recipes/blob/master/force-app/main/default/lwc/ldsUtils/ldsUtils.js

@LawrenceLoz
Copy link
Owner

Yeah I like this a lot. There are inconsistencies with the way toast messages are constructed at the moment so this would definitely be an improvement. Once this is in place we could agree some standards around where toast messages are raised (e.g. we should catch errors from all server methods and show as toasts) and implement these

@sfdcschwabe
Copy link
Contributor Author

Hi Lawrence

I hope you are fine and started smoothly into the new year?

What do you think is the best source branch to work on this in a separated branch?

@LawrenceLoz
Copy link
Owner

Yes doing well thanks. I have some time off but somehow still found myself overrun with things! Hope you're doing well and enjoying 2021 so far 👍

I'd say at this point branching from the release branch is probably best. You might want to wait until we've got your other branch merged which should hopefully be quick to do (sorry this has sat unmerged for a while I must have missed a notification)

@LawrenceLoz LawrenceLoz added the enhancement New feature or request label Apr 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants