简体   繁体   English

Firefox不显示自定义错误消息

[英]Firefox doesn't show custom error messages

I'm trying to create an extendable error class in CoffeeScript with this code: 我正在尝试使用以下代码在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

When I try to throw one of these errors in Firefox using this code: 当我尝试使用此代码在Firefox中抛出其中一个错误时:

throw new ExtendableError('An error message');

I only get [object Object] printed to the console. 我只将[object Object]打印到控制台。

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 . 我得到打印到控制台的所需错误消息: Error: An error message

It should be noted that both, Error.toString() and ExtendableError.toString() work correctly. 应该注意, Error.toString()ExtendableError.toString()可以正常工作。 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. 我在Chrome中测试了相同的代码而没有任何问题,我在谷歌搜索我们的代码没有运气。

Any ideas? 有任何想法吗?

Update 1: 更新1:

Someone asked me to include the generated JavaScript code. 有人要我包含生成的JavaScript代码。 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);

I put your file in lib/Error.coffee . 我把你的文件放在lib / Error.coffee中 Then I converted to Javascript: 然后我转换为Javascript:

 coffee --compile --output dist lib

It created the file dist/Error.js . 它创建了文件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: 我在Linux中使用Firefox 46.0.1进行了一些测试,但我没有发现任何问题,请查看我的屏幕截图:

Firefox with Inspect Element Firefox与Inspect元素

Firefox检查Element

Firefox with Firebug Firefox与Firebug

Firefox与Firebug

Chrome

It very similar in Chrome. 它在Chrome中非常相似。 Chrome示例

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

I have the following code written in ES6 to make extendable Errors, and it works on Chrome, Firefox and Opera. 我在ES6中编写了以下代码来制作可扩展的错误,它适用于Chrome,Firefox和Opera。

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

So i think the problem is how coffeescript is compiling your code. 所以我认为问题是coffeescript如何编译你的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM