[dojo-contributors] Stance on custom error types

Colin Snover dojo-contributors at zetafleet.com
Mon Jun 4 17:05:09 EDT 2012


No, it doesn't. That code sucks. :)

On 04/06/2012 15:45, Bill Keese wrote:
> By "properly" you mean this chunk of code from that link?   It seems
> crazy to have 25 lines of code just to create an error subclass.  
> Does it really need to be that long?
>
> var MyError = (function() {
>    function F(){}
>    function CustomError() {
>       // correct if not called with "new"
>       var _this = (this===window) ? new F() : this,
>           tmp = Error.prototype.constructor.apply(_this,arguments)
>       ;
>       for (var i in tmp) {
>          if (tmp.hasOwnProperty(i)) _this[i] = tmp[i];
>       }
>       return _this;
>    }
>    function SubClass(){}
>    SubClass.prototype = Error.prototype;
>    F.prototype = CustomError.prototype = new SubClass();
>    CustomError.prototype.constructor = CustomError;
>
>    // add a custom method
>    CustomError.prototype.CustomField1 = function(custom_1){
>       if (custom_1 != null) this.custom_1 = custom_1;
>       return this.custom_1;
>    }
>
>    return CustomError;
> })();
>
> On Tue, Jun 5, 2012 at 12:36 AM, Bryan Forbes
> <bryan at reigndropsfall.net <mailto:bryan at reigndropsfall.net>> wrote:
>
>     As long as we're subclassing properly, I'd rather use custom error
>     types.
>
>     http://blog.getify.com/howto-custom-error-types-in-javascript/
>
>
>     Mark Wubben wrote:
>     > Dojo seems to do custom errors by adding a `dojoType` property
>     to the error instance. Examples in `dojo/request` and `dojo/io`.
>     The new Promise code however uses a subclassed Error.
>     >
>     > dojo/promise/CancelError:
>     >
>     >       function CancelError(message){
>     >               Error.captureStackTrace &&
>     Error.captureStackTrace(this, CancelError);
>     >               this.message = message || "The deferred was
>     cancelled.";
>     >               this.name <http://this.name> = "CancelError";
>     >       };
>     >       CancelError.prototype = new Error;
>     >       CancelError.prototype.constructor = CancelError;
>     >       return CancelError;
>     >
>     > I prefer subclassed errors because they're easier to instantiate
>     and test for (either using `instanceof` or checking their `name`).
>     We could even add `dojoType` properties.
>     >
>     > What is your preference?
>     >
>     > In any case, we need to standardize these across the codebase.
>     >
>     > --
>     > Mark Wubben
>     >
>     > http://novemberborn.net
>     > http://twitter.com/novemberborn
>     >
>     > _______________________________________________
>     > dojo-contributors mailing list
>     > dojo-contributors at mail.dojotoolkit.org
>     <mailto:dojo-contributors at mail.dojotoolkit.org>
>     > http://mail.dojotoolkit.org/mailman/listinfo/dojo-contributors
>
>
>     --
>     Bryan Forbes
>     http://www.reigndropsfall.net
>
>     GPG Fingerprint
>     3D7D B728 713A BB7B B8B1  5B61 3888 17E0 70CA 0F3D
>
>
>     _______________________________________________
>     dojo-contributors mailing list
>     dojo-contributors at mail.dojotoolkit.org
>     <mailto:dojo-contributors at mail.dojotoolkit.org>
>     http://mail.dojotoolkit.org/mailman/listinfo/dojo-contributors
>
>
>
>
> _______________________________________________
> dojo-contributors mailing list
> dojo-contributors at mail.dojotoolkit.org
> http://mail.dojotoolkit.org/mailman/listinfo/dojo-contributors


-- 
Colin Snover
http://zetafleet.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.dojotoolkit.org/pipermail/dojo-contributors/attachments/20120604/a81cac31/attachment-0001.htm 


More information about the dojo-contributors mailing list