Errors not shown - How to workaround non-existent `onunhandledrejection`

It would be a garganutan loss to the entire ecosystem if prolific UXP developers such as you, who are paragons of community engagement, abandoned UXP. We’ve been aware of this issue (you and I’ve also discussed the onerror issue on similar lines). Here’s why we’re inhibited by the available APIs and can’t support this in UXP.

V8, the underlying UXP Javascript engine does provide a way to hook up callbacks for figuring out unhandled promise rejections via setPromiseRejectCallback() but UXP in Photoshop(or InDesign) doesn’t use V8 directly but via an ABI-stable (which is of paramount importance in the world of C++) API surface called Node-API. This API interface provides no such avenues to register any callback with V8. This prevents UXP from being able to handle your exact requirements. It’s not the answer that you were probably expecting but unless Node-API allows such APIs or allows accessing the underlying V8 isolate in a safe well-defined manner, this stands, as of today, as a limitation of the UXP platform.

Since you did mention AI, instead of switching over to it, I could hazard a suggestion of having it write the (yes I know!) ugly try-catch blocks or you could try refactoring your code to handle errors with utility functions as something on the lines of

const handleError = fn => async (...params) => {
  try {
    await fn(...params);
  } catch (error) {
    console.error('Error:', error);
  }
};

and then if you have some code like

const riskyOperation = async (throwError) => {
  if (throwError) {
    throw new Error('Oops!');
  } else {
    console.log('Operation succeeded!');
  }
};

you could wrap all such calls into

doOperation = handleError(riskyOperation);

and then call into them

doOperation(true);  // This *will* log an error
doOperation(false); // This will log 'Operation succeeded!'

Obviously not an ideal solution and I can’t think of all possible issues with such an approach but given large codebases, this could possibly alleviate the pain of wrapping every promise in a try-catch block. I don’t think anything is palatable but given what UXP is, one needs to operate within the constraints of the architecture of the system.

2 Likes