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

**URL:** <https://forums.creativeclouddeveloper.com/t/bug-errors-thrown-inside-of-executeasmodal-are-being-converted-to-strings/5431>\
**Category:** UXP Plugin API\
**Tags:** bug\
**Created:** [October 31, 2022, 5:26am UTC](https://forums.creativeclouddeveloper.com/t/bug-errors-thrown-inside-of-executeasmodal-are-being-converted-to-strings/5431 "2022-10-31T05:26:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![kbsanders](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/kbsanders/32/2388_2.png) [@kbsanders](https://forums.creativeclouddeveloper.com/u/kbsanders)\
**Post date:** [October 31, 2022, 5:26am UTC](https://forums.creativeclouddeveloper.com/t/bug-errors-thrown-inside-of-executeasmodal-are-being-converted-to-strings/5431/1 "2022-10-31T05:26:38Z")

</div>

**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`:

```auto
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:

```auto
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`:

```auto
...
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:

```auto
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: ..."

```
