Skip to content

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

@sfdcschwabe

Description

@sfdcschwabe

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions