Tôi đang triển khai trình ghi nhật ký cho toàn bộ dự án MEAN.js
của mình. Tôi có một logger phía máy chủ hoạt động tốt, và tôi đã thiết lập một điểm cuối để nhận được một yêu cầu POST với một ngoại lệ để đăng nhập một lỗi máy khách. Tôi đang sử dụng StackTrace.js
để ghi nhật ký lỗi phía máy khách, nhưng tôi gặp phải một số lỗi; bây giờ tôi đang sử dụng trình phân tích cú pháp lỗi của StackTrace và vẫn gặp lỗi. Tôi đang sử dụng một trang trí trên $ exceptionHandler góc để đạt được điều này:Sử dụng StackTrace.js với Angular.js
$provide.decorator("$exceptionHandler",
function($delegate, traceService, $log, $window) {
return function(exception, cause) {
$delegate(exception, cause);
try {
var errorMessage = exception.toString();
var stackTrace = traceService.
parse(exception).
then(function(stackFrames) {
$.ajax({
type: 'POST',
url: '/logger',
contentType: 'application/json',
data: angular.toJson({
url: $window.location.href,
message: errorMessage,
type: 'exception',
stackFrace: stackFrames,
cause: cause || ''
})
}).fail(function() {
$log.warn('POST request failed');
});
}).catch(function(error) {
$log.warn('Error StackTrace');
});
} catch (loggingError) {
$log.warn('Error server-side logging failed');
$log.log(loggingError);
}
};
});
traceService
là một dịch vụ kiễu góc đóng vai trò như là một proxy cho các chức năng/lỗi-stack-phân tích cú pháp StackTrace. traceService.parse
tương đương với ErrorStackParser.parse
. traceService
được thực hiện như:
angular.module('core').factory('traceService', function() {
return {
parse: ErrorStackParser.parse
};
});
Tôi đã đặt mã trang trí này trên bootstrap ứng dụng kiễu góc, và tôi đang thử nghiệm bằng cách ném một lỗi trên một bộ điều khiển. Khi tôi chạy ứng dụng, tôi gặp lỗi này trên bảng điều khiển dành cho khách hàng:
Error server-side logging failed
angular.js:13708 TypeError: this.parseV8OrIE is not a function
at Object.ErrorStackParser$$parse [as parse] (error-stack-parser.js:70)
at application.js:22
at invokeLinkFn (angular.js:9816)
at nodeLinkFn (angular.js:9215)
at compositeLinkFn (angular.js:8510)
at publicLinkFn (angular.js:8390)
at lazyCompilation (angular.js:8728)
at updateView (viewDirective.ts:278)
at Object.configUpdatedCallback [as configUpdated] (viewDirective.ts:226)
at configureUiView (view.ts:164)
Dường như đây là vấn đề với lỗi phân tích cú pháp; Tôi không thể tìm thấy bất kỳ bài viết nào liên quan đến vấn đề này, vì vậy tôi chắc chắn đó là điều tôi đang làm sai, cách tôi đang thử nghiệm hoặc một thứ khác. Có thể cung cấp một số cái nhìn sâu sắc về lý do tại sao điều này là không?
Sửa
tôi sửa đổi đoạn mã sau bình luận của Caramiriel. Có vẻ như tôi cần phải thêm tất cả các chức năng từ lỗi-stack-parser vào dịch vụ của tôi. Đây là traceService
:
angular.module('core').factory('traceService', function() {
return {
parse: ErrorStackParser.parse,
parseV8OrIE: ErrorStackParser.parseV8OrIE,
extractLocation: ErrorStackParser.extractLocation
};
});
Bây giờ tôi nhận được lỗi này:
TypeError: StackFrame is not a constructor
at Object.<anonymous> (http://localhost:3000/lib/error-stack-parser/error-stack-parser.js:105:24)
at Array.map (native)
at _map (http://localhost:3000/lib/error-stack-parser/error-stack-parser.js:22:26)
at Object.ErrorStackParser$$parseV8OrIE [as parseV8OrIE] (http://localhost:3000/lib/error-stack-parser/error-stack-parser.js:95:20)
at Object.ErrorStackParser$$parse [as parse] (http://localhost:3000/lib/error-stack-parser/error-stack-parser.js:70:29)
at http://localhost:3000/application.js:22:11
at invokeLinkFn (http://localhost:3000/lib/angular/angular.js:9816:9)
at nodeLinkFn (http://localhost:3000/lib/angular/angular.js:9215:11)
at compositeLinkFn (http://localhost:3000/lib/angular/angular.js:8510:13)
at publicLinkFn (http://localhost:3000/lib/angular/angular.js:8390:30)
'traceService.parse' được triển khai như thế nào? Bạn là bối cảnh ban đầu ('này') bằng cách nào đó, nhưng không rõ tại sao với mã bạn đã cung cấp. Bạn có thể phải '.bind' đối tượng gốc. – Caramiriel
Tôi đã chỉnh sửa câu hỏi để thêm thông tin. Tôi cũng đã thay đổi mã 'traceService' và bây giờ tôi nhận được một lỗi khác. –
.factory ('traceService', function() { \t return ({ \t phân tích cú pháp: ErrorStackParser.parse, \t \t extractLocation: ErrorStackParser.extractLocation, \t \t parseV8OrIE: ErrorStackParser.parseV8OrIE, \t \t parseFFOrSafari: ErrorStackParser. parseFFOrSafari, \t \t parseOpera: ErrorStackParser.parseOpera, \t \t parseOpera9: ErrorStackParser.parseOpera9, \t \t parseO pera10: ErrorStackParser.parseOpera10, \t \t parseOpera11: ErrorStackParser.parseOpera11 \t}); \t \t}) –