2016-04-26 49 views
0

我想在服務器端使用自定義js文件(ServerLogger)記錄錯誤。在ServerLogger文件我有我自己的方法稱爲錯誤,這將使http調用和在服務器端記錄錯誤。

但我得到一個上面說的,當我訪問我自己的錯誤方法below.Googled但沒有得到多少eslint錯誤help.Kindly幫助

angular.module('sample').config(function ($provide) { 

// Adding main exception catch block for the entire application. 
$provide.decorator('$exceptionHandler', ['$injector', 
    function ($injector) { 
     // Inject the logger for logging the exception or error in serverside. 

     return (exception, cause) => 
      $injector.get('ServerLogger').error(exception, cause); 
    }]); 

}); 

ServerLogger

'use strict'; 

(() => { 

/** 
* @ngdoc class 
* @name ServerLogger 
* @description 
* Responsible for logging the exception/errors in the server side. 
*/ 
class ServerLogger { 

    constructor($http, $log, ResourceIdService, ConfigParameters) { 
     this.$http = $http; 
     this.$log = $log; 
     this.resourceIdService = ResourceIdService; 
     this.lebenServiceUrl = ConfigParameters.getLebenServiceUrl(); 
    } 

    /** 
    * @ngdoc method 
    * @name logErrors 
    * @methodOf apdCommon.class:ServerLogger 
    * @description 
    * Responsible for logging the exception/errors in the server side. 
    * 
    * @param {object} exception - thrown exception from the application 
    * @param {object} cause - root cause of the exception 
    * 
    * @returns {object} promise 
    */ 
    error(exception, cause) { 
     return this.resourceIdService.gettingResourceId() 
      .then(resourceId => 
      this.lebenServiceUrl 
      + (resourceId ? '/' + resourceId : '') + '/log') 
      .then(requestUrl => this.$http.post(requestUrl, 
       getLogReqPayload(exception, cause)) 
       .catch((error) => { 
        this.$log.error('Logger: ' + 
         'Failed to log the angular exception details in server side' 
         + error); 
       })); 
    } 

} 

/** 
* @ngdoc method 
* @name getLogReqPayload 
* @methodOf apdCommon.class:ServerLogger 
* @description 
* Returns the formatted log message as a request payload for logging service call. 
* 
* @param {object} exception - thrown exception from the application 
* @param {object} cause - root cause of the exception 
* 
* @returns {object} returns the formatted log request payload 
*/ 
function getLogReqPayload(exception, cause) { 
    if (angular.isDefined(cause)) { 
     exception.message += ' (caused by "' + cause + '")'; 
    } 
    var message = exception.message; 
    if (!message) { 
     message = exception; 
    } 
    var logReqPayload = { 
     'uiTimestamp': new Date(), 
     'uiThread': 'Angular Exception:Uncaught Exception', 
     'applicationVersion': 'SPA' + ':' + '1.0.0', 
     'message': { 
      'logMessage': angular.toJson(message), 
      'stacktrace': printStackTrace({e: exception}) 
     } 
    }; 
    return logReqPayload; 
} 

angular.module('comon').service('ServerLogger', ServerLogger); 
})(); 
+2

正如它說,使用'then'像'。然後(函數成功(){},功能錯誤(拒絕){});' – Walfrat

+0

我不使用$ http.error方法,我想從我自己的ServerLogger類調用我自己的錯誤方法。 – user1645290

+0

向我們顯示您的ServerLogger然後 – Walfrat

回答

0

這是怎麼了,我終於訪問了錯誤方法。

$provide.decorator('$exceptionHandler', ['$injector', 
    function ($injector) { 
     return (exception, cause) => { 
      $injector.invoke((ServerLogger, ServerErrorLogMessageFormatter) => { 
       ServerLogger.error(
        ServerErrorLogMessageFormatter.formatServerErrorLogMessage(
         exception, cause)); 
      }); 
     }; 
    }]);