简体   繁体   English

使用VS 2013在Typescript中装饰AngularExceptionHandler服务

[英]Decorating angular exceptionHandler service in Typescript using VS 2013

Using Typescript and Visual Studio 2013 使用Typescript和Visual Studio 2013

I want to decorate the angularjs $exceptionHandler and I'm not quite sure of the correct way to do it. 我想装饰angularjs $ exceptionHandler,但我不确定这样做的正确方法。 I have decorated $log and it works fine. 我已经装饰了$ log,并且工作正常。

My configuration: 我的配置:

MyAngularModule.config(['$provide',($provide) => {
    $provide.decorator('$log', ['$delegate', function ($delegate) {
        var myLog: MyLogService.LogService = new MyLogService.LogService();
        return myLog;
    }]);
}]);

MyLogService.ts MyLogService.ts

export = Example;
module Example {
    export class LogService implements ng.ILogService{ 
        constructor() {}
        public assertEmpty = () => { };
        public reset = () => { };
        public info: ng.ILogCall = ...all my code...
    }
}

Now when I try doing a similar thing with $exceptionHandler: 现在,当我尝试使用$ exceptionHandler做类似的事情时:

$provide.decorator('$log', ['$delegate', function ($delegate) {
        var myExHandler: MyExHandler.ExHandlerService = new MyExHandler.ExHandlerService();
        return myExHandler;
    }]);

The interface definition for exceptionHandler in the type files is: 类型文件中exceptionHandler的接口定义为:

interface IExceptionHandlerService {
    (exception: Error, cause?: string): void;
}   

How do I code this similar to the way I did in decorating the log service? 如何编写类似于装饰日志服务的方式的代码?

I tried: 我试过了:

export = Example;
module Example {
    export class ExHandlerService implements ng.IExceptionHandlerService{ 
        constructor(anException: Error, aCause?: string) {}

    }
}

And Visual Studio is complaining that the interface is implemented wrong. 而且Visual Studio抱怨接口实现错误。 I don't really understand what (exception: Error, cause?: string): void; 我不太了解(异常:错误,原因?:字符串):void; means, is it the constructor? 就是构造函数吗? The typings file does show a name, just this anonymous method definition. 类型文件确实显示了一个名称,只是这个匿名方法的定义。

I don't understand how I even approach this in the decorate call (ie. where do the error and cause parameters come from and how do I keep the default implementation operating as well?) 我什至不知道如何在decorate调用中实现这一点(即错误和原因参数从何而来以及如何使默认实现也保持运行?)

Thanks in advance. 提前致谢。

Update : 更新

Considering in angular.d.ts: 在angular.d.ts中考虑:

interface IExceptionHandlerService {
    (exception: Error, cause?: string): void;
}

I guess my root confusion is how this interface would be implemented in Typescript?? 我想我的根本困惑是如何在Typescript中实现此接口?

Ok, I found that interfaces can define a function type in this case. 好的,我发现在这种情况下接口可以定义函数类型。 That created confusion with the way I have viewed interfaces typically. 这与我通常查看界面的方式产生了混淆。 It seems I can't use an "implements" in this case? 在这种情况下,我似乎无法使用“实现”?

I think I've found my own answer. 我想我已经找到了自己的答案。 In any case here is how I decorated the angular exception handler service in Typescript. 无论如何,这就是我在Typescript中装饰角度异常处理程序服务的方式。

export class MyExceptionHandlerService { 


    private _defaultExceptionHandlerService: ng.IExceptionHandlerService;


    constructor(paramDelegate: ng.IExceptionHandlerService) {

        this._defaultExceptionHandlerService = paramDelegate;
    }

    public exception: ng.IExceptionHandlerService = (paramException: Error, paramCause?: string): void => {

        console.error("MY ERROR HANDLER: " + paramException.message)

        this._defaultExceptionHandlerService(paramException, paramCause);

    }



}

And I use it from my angular config section like this: 我从我的角度配置部分使用它,如下所示:

$provide.decorator('$exceptionHandler', ['$delegate', function ($delegate: ng.IExceptionHandlerService) {


                var localExceptionHandler: MyHandler.ExceptionHandlerService = new MyHandler.ExceptionHandlerService($delegate);

                return localExceptionHandler.exception;
            }]);

It's working as expected for me. 它按我的预期工作。

You could have also done something like this without using objects...not sure if this is good or not. 您也可以在不使用对象的情况下做类似的事情……不确定这是否好。 Feedback would be awesome! 反馈会很棒!

((): void => {
    'use strict';

    angular
        .module('app.blocks')
        .config(config);

    config.$inject = ['$provide'];

    function config($provide: ng.auto.IProvideService): void {
        $provide.decorator('$exceptionHandler', extendExceptionHandler);
    }

    extendExceptionHandler.$inject = ['$delegate', 'app.blocks.logger'];
    function extendExceptionHandler(
        $delegate: any,
        logger: app.blocks.ILogFactory): Function {

        return (exception: Error, cause?: string, source? : string) => {
            $delegate(exception, cause);
            logger.log(exception.message, { exception: exception, cause: cause }, source, 'error');
        }
    }
})();

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

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