2016-04-26 3 views
0

Ich versuche, die Fehler auf der Serverseite mit benutzerdefinierten js-Datei (ServerLogger) zu protokollieren. In ServerLogger-Datei habe ich meine eigene Methode namens Fehler, die einen http-Aufruf und protokollieren Sie den Fehler auf der Serverseite.

Aber ich bin immer ein über dem eslint Fehler, wenn ich meine eigene Error-Methode als below.Googled zuzugreifen, aber didnt viel help.Kindly erhalten helfen

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

Wie gesagt, verwenden Sie 'then' wie' .then (function Erfolg() {}, Funktionsfehler (Ablehnung) {}); ' – Walfrat

+0

I $ http.error Methode bin nicht, ich versuche, um meine eigene Fehlermethode von meiner eigenen ServerLogger-Klasse aufzurufen. – user1645290

+0

Zeigen Sie uns Ihren ServerLogger dann – Walfrat

Antwort

0

Dies ist, wie , Habe ich endlich auf die error-Methode zugegriffen.

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