Problem/Motivation

`Drupal.throwError` is rethrow the error message. If you are passing String into `Drupal.throwError`, there's no more useful error stack for debugging.

It need not to check param type explicitly but should discourage its usage in our API design.

Proposed resolution

- Remove String type in JSDoc

Remaining tasks

- Review patch

User interface changes

- N/A

API changes

- Docs change. Do not affect any code usages.

Data model changes

- N/A

CommentFileSizeAuthor
Drupal.throwError.patch447 bytesdroplet
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

droplet created an issue. See original summary.

droplet’s picture

Issue summary: View changes

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Don't expect any failures but running 10.1 tests.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Passes 10.1 and I can't see a reason to not add it but will leave it to the committers

xjm’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +JavaScript, +Needs JavaScript review

Thanks for finding and resurrecting this issue!

To review whether a documented typehint is correct, we need to look at the method code and the usages: Is it possible to pass a string, and if so, does any code actually do that?

I grepped and found the following non-documentation references in core:

core/misc/drupal.js:  Drupal.throwError = function (error) {
core/misc/drupal.js:          Drupal.throwError(e);
core/misc/drupal.js:          Drupal.throwError(e);
core/modules/system/tests/modules/js_errors_test/js/js_errors_test.js:(function ({ throwError, behaviors }) {
core/modules/system/tests/modules/js_errors_test/js/js_errors_test.js:      throwError(new Error('A manually thrown error.'));
core/modules/system/tests/modules/js_testing_log_test/js/js_testing_log.js:    const originalThrowFunction = Drupal.throwError;
core/modules/system/tests/modules/js_testing_log_test/js/js_testing_log.js:    Drupal.throwError = (error) => {

The code of the method itself is simple:

  Drupal.throwError = function (error) {
    setTimeout(() => {
      throw error;
    }, 0);
  };

As far as I can tell, that code does not restrict the type of error. Since I'm not the world's most pro JavaScript developer, I also looked up the docs for throw and confirmed that it can be not only an object but also a string, a number, a boolean... So string is currently allowed, and changing the code to disallow it would be a pretty dramatic BC break.

I checked the six usages in core. Five of them were indeed simply throwing Error(), and the sixth (from the recent change to get JS errors to fail tests) is a bit more complicated:

    const originalThrowFunction = Drupal.throwError;
    Drupal.throwError = (error) => {
      const errors = JSON.parse(
        sessionStorage.getItem('js_testing_log_test.errors') ||
          JSON.stringify([]),
      );
      errors.push(error.stack);
      sessionStorage.setItem(
        'js_testing_log_test.errors',
        JSON.stringify(errors),
      );
      originalThrowFunction(error);
    };

All that said, it's entirely possible, even probable, that contrib and custom code is already using this method with a string message. So while throwing Error might be better, we can't just remove string from the docs.

We have three options:

  1. Leave it as-is.
  2. Add to the docs saying that throwing Error is preferable for developer experience and better debugging.
  3. Deprecate allowing plain strings as the argument, and enforce that the argument is of type Error in Drupal 11.

This is too JavaScript-y for me to make the decision, so I defer to the expertise of those that have it.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.