不推荐使用$ http错误。使用then或catch而不是angular / no-http-callback

时间:2016-04-26 07:18:33

标签: angularjs eslint

我正在尝试使用自定义js文件(ServerLogger)在服务器端记录错误。在ServerLogger文件中,我有自己的方法,称为错误,它将进行http调用并在服务器端记录错误。

但是当我访问我自己的错误方法时,我得到一个上面说的eslint错误,如下所示.Googled但没有得到很多帮助。求助

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);
})();

1 个答案:

答案 0 :(得分:0)

这是怎么回事,我终于访问了错误方法。

SELECT expand(bothV('TestClassEdge')) FROM TestClassEdge