使用 AngularJs v1.5.7,我在尝试记录异常时遇到了一些麻烦。
问题似乎与异常的类型有关。
该对象在某种程度上不是具有属性的普通对象。
这是一个代码示例,其中包含我迄今为止尝试过的内容以及每次尝试的结果。
MyApp.config(['$provide', function ($provide)
{
$provide.decorator('$exceptionHandler', ['$delegate', 'logsService', function ($delegate, logsService)
{
return function (exception, cause)
{
$delegate(exception, cause);
try
{
//Attempt #1 : throw because exception isn't an object.
logsService.addLog(exception);
//Attempt #2 : Log the hardcoded object with both properties
logsService.addLog({a:'Some test', b:'Ok'});
//Attempt #3 : log an empty object because exception has no property
var log = {};
for (var property in exception)
{
if (object.hasOwnProperty(property))
{
log[property] = exception[property];
}
}
logsService.addLog(log);
//Attempt #4 : Log the message, but doesn't log all possible properties that exception could have.
logsService.addLog({ message: exception.message});
}
catch (ignore) { }
};
}]);
}]);
注意:我无法更改logsService
.
到目前为止,我在谷歌上没有找到任何关于此的信息。
为什么exception
没有属性以及如何解决此限制?
编辑 :
我想我已经指出了更多的问题。异常对象可能不干净并且具有无法克隆的功能或其他东西。这会在日志服务中出现以下错误。
DataCloneError:无法在“IDBObjectStore”上执行“放置”`
Error和Function对象不能被结构化克隆算法复制;尝试这样做将引发 DATA_CLONE_ERR 异常。
//Attempt #5 : log an empty object because exception has no property.
var cleanedException = JSON.parse(angular.toJson(exception));
logsService.addLog(cleanedException);