$http 错误已弃用。使用 then 或 catch 代替 angular/no-http-callback

$http error is deprecated. Use then or catch instead angular/no-http-callback

我正在尝试使用自定义 js 文件 (ServerLogger) 在服务器端记录错误。在 ServerLogger 文件中,我有自己的方法 error,它将进行 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);
})();

就这样,终于访问到了错误方法

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