404/401服务器状态响应的安全中断

Safe break on 404/401 server status response

本文关键字:安全 中断 响应 状态 服务器      更新时间:2023-09-26

我想在服务器返回404或401时显示一条消息。

这是我的结构:

ultimasoperationescontroller -> ultimasOperacionesService -> remoteDataService

ultimasOperacionesController:调用服务

ultimasoperactionesservice : call remoteDataService.

remoteDataService:调用服务器(服务器响应404)。

所以这是我写的请求代码的一部分:

控制器

function cargarUltimasOperacionesComplejo() {
       ultimasOperacionesService.obtenerUltimasOperacionesComplejo()
            .then(function(ultimasOperaciones) {
                vm.ultimasOperaciones = ultimasOperaciones;
            })
            .catch(mostrarError);
    }

    function mostrarError(e) {
        //HERE I WANT TO MAKE SOMETHING TO SHOW A IMAGE TELLING TO THE USER THAT WAS NOT POSSIBLE CONNECT TO THE SERVER.
    }

//funcion para obtener las últimas operaciones
    function obtenerUltimasOperacionesComplejo() {
        remoteDataService.obtenerUltimasOperacionesComplejo()
         .then(function(ultimasOperaciones) {
                return ultimasOperaciones;
            })
            .catch(mostrarError);
    }
    function mostrarError() {
        debugger;
         return $q.reject('Ups! Hubo un problema al conectarse al servidor. Intente nuevamente');
    }

RemoteDataService

function obtenerUltimasOperacionesComplejo() { 
        debugger;
        return $http
            .get(ultimasOperacionesUrl,
                 {params: {idComplejo: 1}})
            .then(function(response) {
            debugger;
                var datos = response.data;
                var status = response.status;
                if(status == 200){   
                    var exito = datos.success;
                    if(exito == 0){
                        return $q.reject(datos);
                    }
                    else if(exito == 1){
                        ultimasOperaciones = datos.ultimasOperaciones;
                        return ultimasOperaciones;
                    }
                }    
            })
            .catch(generarError);
    }
    function generarError(e){
        debugger;
        if (e.message) {
            return $q.reject(e.message);
        }
        return $q.reject('Ups! Hubo un problema al conectarse al servidor. Intente nuevamente');
    }

当我调试它时,服务响应404,然后启动函数"generarError"到remoteDataService.

最后,在UltimasOperacionesService中启动函数"generarError",之后:什么也没发生:控制器中的函数"mostrarError"从未被调用过。

我也有以下错误在控制台:

TypeError: Cannot read property 'then' of undefinedat cargarultimasoperacionescomplexjo (ultimasoperaciones.controller.js:25)

在另一个帖子中有人告诉我,这是因为在某些函数中我没有返回任何东西。

我怎样才能使错误可以到达控制器,之后我可以在HTML中做些什么?谢谢!

TypeError:无法读取未定义at的属性'then'cargarUltimasOperacionesComplejo (ultimasoperaciones.controller.js: 25)

因为ultimasOperacionesService返回$q.reject()或数据只有当remoteDataService承诺被解决(它不是一个同步操作),并且在它的值仍然未定义之前,所以ultimasOperacionesService. obtenerultimasoperacionescomplejo()不是一个你可以通过。then()或catch()处理的承诺。

如果你需要在控制器中处理promise,那么你必须从ultimasOperacionesService返回promise。

像这样:

//funcion para obtener las últimas operaciones
    function obtenerUltimasOperacionesComplejo() {
        return remoteDataService.obtenerUltimasOperacionesComplejo();
    }