Custom Error doesn't inherit properties

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

undavide

Custom Error doesn't inherit properties

Post by undavide »

Hello,
Error objects have several properties:

Code: Select alltry {throw new Error("message") }
catch(e) { for (prop in e) {$.writeln(prop + ": " + e[prop])} }

// number: 1
// fileName: (Script4)
// line: 1
// source: try {throw new Error("message") }
// catch(e) { for (prop in e) {$.writeln(prop + ": " + e[prop])} }
// start: 0
// end: 0
// message: message
// name: Error
// description: message


I can't get why if I create a custom error object which inherits from the Error() object:

Code: Select allfunction MyError(message) {   
    this.message = message;
}
myError.prototype =  new Error();

This one doesn't inherit any property:

Code: Select allfunction MyError(message) {   
    this.message = message;
}
MyError.prototype =  new Error();

try {throw new MyError("Argh!") }
catch(e) { for (prop in e) {$.writeln(prop + ": " + e[prop])} }

// message: Argh!

I wonder why. Any suggestion?
Thank you!

Davide Barranca
http://www.davidebarranca.com

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

Mike Hale

Custom Error doesn't inherit properties

Post by Mike Hale »

I understanding of oop is limited. But what you are trying to do doesn't need a subclass.
Code: Select allvar MyError = function(message){return new Error(message);};// get an instance of Error
MyError.type = "custom";// create a new property and assign value
try {
    throw MyError('myCustomErrorMesg');//call the instance
}catch(e) {
        for (prop in e) {
            $.writeln(prop + ": " + e[prop]);
    }
}
undavide

Custom Error doesn't inherit properties

Post by undavide »

Thanks Mike,
as a workaround it... works
(yet the custom error should inherit from Error - this is possibly another Extendscript bug?)
Best

Davide
Mikaeru

Custom Error doesn't inherit properties

Post by Mikaeru »

undavide wrote:this is possibly another Extendscript bug?
Another problem I ran into with custom errors, at least up to CS4, is that all error types: Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError, appear to be "synonyms" for each other:

Code: Select allvar myError;

myError = new Error ();
alert (myError instanceof Error);            // true
alert (myError instanceof ReferenceError);   // true (!)
alert (myError instanceof SyntaxError);      // true (!)

myError = new ReferenceError ();
alert (myError instanceof Error);            // true (!)
alert (myError instanceof ReferenceError);   // true
alert (myError instanceof SyntaxError);      // true (!)

myError = new SyntaxError ();
alert (myError instanceof Error);            // true (!)
alert (myError instanceof ReferenceError);   // true (!)
alert (myError instanceof SyntaxError);      // true
undavide

Custom Error doesn't inherit properties

Post by undavide »

Thanks for adding extra info Mikaeru!
Is there any hope that Extendscript will undergo some revision (also to keep it consistent with latest ECMA specs)?!

Davide
Mike Hale

Custom Error doesn't inherit properties

Post by Mike Hale »

I could be wrong but it is my understanding that Error, EvalError, RangeError, etc. are values found in the Error.name property. And that property seems to be set by the Error.number property.
Code: Select alltry {throw new Error(15) }
catch(e) { for (prop in e) {$.writeln(prop + ": " + e[prop])} }
/* console output
number: 15
fileName: (Script1)
line: 1
source: try {throw new Error(15) }
catch(e) { for (prop in e) {$.writeln(prop + ": " + e[prop])} }
start: 0
end: 0
message: Try without catch or finally
name: SyntaxError
description: Try without catch or finally
*/
And you can use the other arguments to override some properties
Code: Select alltry {throw new Error(15,"CustomMessage",25,"[native code]") }
catch(e) { for (prop in e) {$.writeln(prop + ": " + e[prop])} }
/* console output
number: 15
fileName: (Script1)
line: 25
source: [native code]
start: 0
end: 0
message: CustomMessage
name: SyntaxError
description: CustomMessage
*/