Cristóbal Ganter
Cristóbal Ganter

Reputation: 119

Firefox doesn't show custom error messages

I'm trying to create an extendable error class in CoffeeScript with this code:

class @ExtendableError extends Error
  constructor: (message = '') ->
    super message

    Object.defineProperty @, 'message',
      configurable: true
      enumerable : false
      value : message
      writable : true

    Object.defineProperty @, 'name',
      configurable: true
      enumerable : false
      value : @.constructor.name
      writable : true

    Object.defineProperty @, 'stack',
      configurable: true
      enumerable : false
      value : (new Error(message)).stack
      writable : true

When I try to throw one of these errors in Firefox using this code:

throw new ExtendableError('An error message');

I only get [object Object] printed to the console.

When I throw a built in error:

throw new Error('An error message');

I get the desired error message printed to the console: Error: An error message.

It should be noted that both, Error.toString() and ExtendableError.toString() work correctly. So I have absolutely no clue what's going on.

I tested the same code in Chrome without problems and I've literally searched for ours in Google without luck.

Any ideas?

Update 1:

Someone asked me to include the generated JavaScript code. So here it is:

// Generated by CoffeeScript 1.10.0
(function() {
  var extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
    hasProp = {}.hasOwnProperty;

  this.ExtendableError = (function(superClass) {
    extend(ExtendableError, superClass);

    function ExtendableError(message) {
      if (message == null) {
        message = '';
      }
      ExtendableError.__super__.constructor.call(this, message);
      Object.defineProperty(this, 'message', {
        configurable: true,
        enumerable: false,
        value: message,
        writable: true
      });
      Object.defineProperty(this, 'name', {
        configurable: true,
        enumerable: false,
        value: this.constructor.name,
        writable: true
      });
      Object.defineProperty(this, 'stack', {
        configurable: true,
        enumerable: false,
        value: (new Error(message)).stack,
        writable: true
      });
    }

    return ExtendableError;

  })(Error);

}).call(this);

Upvotes: 3

Views: 738

Answers (2)

THernandez03
THernandez03

Reputation: 1

I have the following code written in ES6 to make extendable Errors, and it works on Chrome, Firefox and Opera.

class CustomError extends Error{ constructor(msg){ super(); console.log(`CustomError: ${msg}`); } }

So i think the problem is how coffeescript is compiling your code.

Upvotes: 0

Troncador
Troncador

Reputation: 3536

I put your file in lib/Error.coffee. Then I converted to Javascript:

 coffee --compile --output dist lib

It created the file dist/Error.js.

Then I ran your code with this simple page:

 <!DOCTYPE html>
 <html>
 <body>
   <script src="dist/Error.js"></script>
   <script>
     throw new ExtendableError('example error');
   </script>
 </body>
 </html>

I did some test with Firefox 46.0.1 in Linux and I didn't find any problem, look my screenshoots:

Firefox with Inspect Element

Firefox inspect Element

Firefox with Firebug

Firefox with Firebug

Chrome

It very similar in Chrome. Chrome Example

I guess the problem is in another part of your code, maybe you're catching the exception and you're doing something with it.

If the problem persist in your Firefox installation and your think it is related with the version of the browser, you can use BrowserStack to test your code with many different versions of the browser and many different Operative System.

Upvotes: 2

Related Questions