Firefox无法显示自定义错误消息

时间:2016-05-23 00:48:55

标签: javascript firefox exception coffeescript console

我尝试使用以下代码在CoffeeScript中创建可扩展的错误类:

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

当我尝试使用此代码在Firefox中抛出其中一个错误时:

throw new ExtendableError('An error message');

我只会将[object Object]打印到控制台。

当我抛出内置错误时:

throw new Error('An error message');

我将所需的错误消息打印到控制台:Error: An error message

应该注意,Error.toString()ExtendableError.toString()都能正常工作。所以我完全不知道发生了什么。

我在Chrome中测试了相同的代码而没有任何问题,而且我在谷歌搜索了我们的代码而没有运气。

有什么想法吗?

更新1:

有人要我包含生成的JavaScript代码。所以这就是:

// 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);

2 个答案:

答案 0 :(得分:2)

我把你的文件放在 lib / Error.coffee 中。然后我转换为Javascript:

 coffee --compile --output dist lib

它创建了文件 dist / Error.js

然后我用这个简单的页面运行你的代码:

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

我在Linux下使用Firefox 46.0.1进行了一些测试,我没有发现任何问题,请查看我的屏幕截图:

Firefox with Inspect Element

Firefox inspect Element

Firefox with Firebug

Firefox with Firebug

在Chrome中非常相似。 Chrome Example

我想这个问题出现在代码的另一部分,也许你正在捕捉异常并且你正在做一些事情。

如果问题在Firefox安装中仍然存在,并且您认为它与浏览器版本有关,则可以使用BrowserStack使用许多不同版本的浏览器和许多不同的操作系统来测试您的代码。 / p>

答案 1 :(得分:0)

我在ES6中编写了以下代码来制作可扩展的错误,它适用于Chrome,Firefox和Opera。

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

所以我认为问题在于coffeescript如何编译你的代码。

相关问题