throw new Error() doesn't propagate?!

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: Tom, Kukurykus

undavide

throw new Error() doesn't propagate?!

Post by undavide »

Hi,
I was writing a demo code for a friend, showing the use of throwing errors to stop a process.
In particular, this one make use of a ProgressBar window (by X, I guess)

Code: Select allvar createProgressWindow = function(title, message, hasCancelButton) {
  var win;
  if (title == null) {
    title = "Work in progress";
  }
  if (message == null) {
    message = "Please wait...";
  }
  if (hasCancelButton == null) {
    hasCancelButton = false;
  }
  win = new Window("palette", "" + title, undefined);
  win.bar = win.add("progressbar", {
    x: 20,
    y: 12,
    width: 300,
    height: 20
  }, 0, 100);
  win.stMessage = win.add("statictext", {
    x: 10,
    y: 36,
    width: 320,
    height: 20
  }, "" + message);
  win.stMessage.justify = 'center';
  if (hasCancelButton) {
    win.cancelButton = win.add('button', undefined, 'Cancel');
    win.cancelButton.onClick = function() {
      //win.close();
      throw new Error('User canceled the pre-processing!');
    };
  }
  this.reset = function(message) {
    win.bar.value = 0;
    win.stMessage.text = message;
    return win.update();
  };
  this.updateProgress = function(perc, message) {
    if (perc != null) {
      win.bar.value = perc;
    }
    if (message != null) {
      win.stMessage.text = message;
    }
    return win.update();
  };
  this.close = function() {
    return win.close();
  };
  win.center(win.parent);
  return win.show();
};


var showDialog = function() {   
    var w = new Window('dialog', "Crash test");
   w.stTitle = w.add('statictext', undefined, "3, 2, 1, ready!");
   w.btnOK = w.add('button', undefined, "RUN");
   w.btnOK.onClick = function() {
      w.close(1);
   }
    this.runDlg = function() {
        return w.show();
    }
}

var postProcess = function() {
   var p = new createProgressWindow ("Wait", "Please wait!", true)
   for (var i = 1; i <= 10; i++) {
      $.writeln("Doing background work... " + i * 10);
        $.sleep(1000);
      p.updateProgress(i * 10);
   }
}

try {
    if (new showDialog().runDlg() == 1) { postProcess() }
} catch (e) {
    alert("User stopped the routine, quitting with error:\n" + e.message)
}

What I don't get is why the Error (fired by the ProgressBar button) doesn't propagate. I've bound an extra $.writeln("CLICK"), and it's traced in the console, yet the new Error isn't seen at all (OSX, ESTK, CS6 and CC)
I'm possibly missing something really obvious here, please shed some light!
Thank you

Davide
http://www.davidebarranca.com
xbytor

throw new Error() doesn't propagate?!

Post by xbytor »

I can't say for sure, but the fact that it is a "progressbar" may change the way Error propagation is handled. The 'onClick' method is being called in a separate thread (sort of) while your code is doing it's sleep. There is no way to percolate the exception from one thread to the other.

If this is in fact the case, you may want to do something like "win.exception = new Error('User canceled the pre-processing!');" in cancelButton.onClick
and checking for win.exception in updateProgress and throwing it there.

Hope this helps.
undavide

throw new Error() doesn't propagate?!

Post by undavide »

Thanks X!
Did you mean "the fact that it is a 'palette'" or exactly "the fact it is a 'progressbar'"?
As far as I understand, the .exception property of Window is a custom one - so I'm allowed to extend native Objects this way, am I right?
Thank you for the help! By the way, the Palette isn't that much responsive when the loop is going on - I tried few different ways to code that example (also on ESTK) but I had problems giving focus to the Palette, which stayed in the background. Life is never boring when scripting for PS

Davide
xbytor

throw new Error() doesn't propagate?!

Post by xbytor »

undavide wrote:Thanks X!
Did you mean "the fact that it is a 'palette'" or exactly "the fact it is a 'progressbar'"?

Yes. I meant "palette".

As far as I understand, the .exception property of Window is a custom one - so I'm allowed to extend native Objects this way, am I right?


The Window object does not have a .exception property, so you shouldn't have a problem with it. The only time I typically add my own properties or functions in PSJS is with the ScriptUI framework.

Thank you for the help! By the way, the Palette isn't that much responsive when the loop is going on - I tried few different ways to code that example (also on ESTK) but I had problems giving focus to the Palette, which stayed in the background. Life is never boring when scripting for PS


It may be the sleep() call itself that is causing the problem. If you're running real code, you may find that the responsiveness improves.
undavide

throw new Error() doesn't propagate?!

Post by undavide »

Hello X,
it works the way you have suggested!
At first, I did understand to check for the win.exception in the postProcess loop (which is unpractical in the real world), while, as you actually wrote, the Error thrown by the updateProgress is correctly caught.
Still a bit puzzling, but it works!
Thank you for the help,

Davide