Bug: Errors thrown inside of executeAsModal are being converted to strings

Tested On:
Photoshop 24.0.0
UXP Version: 6.3.3

Photoshop 23.5.0
UXP Version: 6.2.1

Issue:
Errors thrown from inside of core.executeAsModal()'s targetFunction are being converted to string. This prevents the caller from accessing properties and other information from the original error object.

Ideally the original error object should be preserved.

Consider the following code in index.js:

const psCore = require('photoshop').core;

class CustomError extends Error {
  // A super important value attached to our error object.
  superImportantValue;
}

async function DoWork() {
  // Create and throw our custom error type.
  let customError = new CustomError('Bad stuff happened!');
  customError.superImportantValue = 'Foo';

  throw customError;
}

// Entrypoint
(async () => {

  try {
    await psCore.executeAsModal(async () => {
      await DoWork();
    }, {
      commandName: 'Please wait...',
    });
  } catch (error) {
    console.log('typeof error: ' + typeof error);
    console.dir(error);
  }

})();

Executing the code above results in the following console log where the error is a simple string:

typeof error: string
Error: Bad stuff happened!

To work around this issue, I am currently using the following pattern to capture and rethrow the error outside of executeAsModal:

...
try {

    let thrownError = null;

    await psCore.executeAsModal(async () => {
      try {
        await DoWork();
      } catch (error) {
        thrownError = error;
      }
    }, {
      commandName: 'Please wait...',
    });

    // Rethrow error
    if(thrownError) {
      throw thrownError;
    }

  } catch (error) {
    console.log('typeof error: ' + typeof error);
    console.dir(error);
  }
...

This results in the following console log with the error object preserved:

typeof error: object
Error: Bad stuff happened!
    at DoWork (VM22 index.js:10)
    at psCore.executeAsModal.commandName (VM92 index.js:25)
    at _internalExecuteAsModalWrapper (uxp://uxp-internal/ps-common.js:59)
    at _internalCallbackWrapper (uxp://uxp-internal/ps-common.js:40)
superImportantValue: "Foo"
message: "Bad stuff happened!"
stack: "Error: ..."
4 Likes