【问题标题】:unable to get the correct http response mapped in angular 2 code无法获得在 Angular 2 代码中映射的正确 http 响应
【发布时间】:2017-08-09 01:51:48
【问题描述】:

我正在使用 AWS API Gateway,并且我正在使用 Angular 2 代码调用我的 API,问题是当会话令牌过期并且我的 UI 代码尝试使用它时 - API Gateway 抛出 403 错误,说明“安全令牌包含在请求中已过期”..这是我在控制台中看到的 - 网络部分。

现在这个 403 错误在 Promise 代码中被捕获 => catch Handler 并且我尝试显示服务器正在发送的消息,即

{"message":"The security token included in the request is expired"}

这是问题所在 - 我无法在控制台和 Sweet Alert Box 中显示正确的错误消息。

在调试捕获处理程序接收到的错误对象时,会显示此 Json - 实际错误对象不存在的地方。

{"data":"","status":0,"statusText":"","headers":{},"config":{"method":"GET","headers":{"x-api-key":"<api-key>","Accept":"application/json","x-amz-date":"201705957Z","Authorization":"AWS4-HM256 Credential=ASIAJ66A/20170317/us-west-1/execute-api/aws4_request, SignedHeaders=accept;host;x-amz-date;x-api-key, Signature=59a2bce53fc3fc25a0dcc36c80243ae63","x-amz-security-token":"<security-token>","Content-Type":"application/json"},"timeout":0,"transformRequest":[null],"transformResponse":[null],"url":"https://api.tech.in/adviser/Suraj","data":""}}

我能够解决 API Gateway 在提供错误密码或用户名时发送的 400 错误。

注意:CORS 已启用,并且映射响应和集成有 403 错误映射并显示错误标头有 CORS。

做什么 - 解决这个问题 - 因为在 catch 处理程序中捕获的最终错误对象没有正确的 http 响应数据

这是我的代码

// this is the final Consumer of the Promise
this.http.getFundDetailedList() //***
        .then(response => this.fundDetailsList = response.data.Items)
        .catch(err => {
        swal('Error Reported', "Please login again \n"+err.data, 'error');
        console.log(this.handleError(err));
        });
    }

    // helper function for Error Handling
private handleError(error: Response | any) {
    let errMsg: string;
    console.log('Error Json '+ JSON.stringify(error)); // Wrong Error Object shown in console
    if (error instanceof Response) {
      const body = error.json() || '';
      const err = body.error || JSON.stringify(body);
      errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
    } else {
      errMsg = error.message ? error.message : JSON.stringify(error.data.message);
    }
    return errMsg;
  }


  // Calls AWS API - coded inside a http service class
  getFundDetailedList() {
    return this.credentialService.getAwsSDK().adviserFundsGet({ 'adviser': this.credentialService.$id }, {}, {});
  }

【问题讨论】:

  • 什么是安全令牌?
  • 可以添加http调用的代码吗?
  • 安全令牌被删除了一半 - 它被 AWS Securiy 和所有用户使用,......在几分钟内上传我的代码

标签: javascript angular promise cors aws-api-gateway


【解决方案1】:

对于 API 网关中的身份验证失败,CORS 标头未正确发送。这是一个已知问题,您可以在 aws 论坛上找到更多详细信息

https://forums.aws.amazon.com/thread.jspa?messageID=671237&#720951

基本上,对于某些类型的错误(身份验证失败/错误请求/api 密钥身份验证/问题调用集成),标头映射将被跳过,这意味着 CORS 标头不会在响应中返回。

对于给您带来的不便,我们深表歉意。但我们正在积极努力解决这个问题。

【讨论】:

  • 感谢您的澄清...并请求更新此帖子并提供修复此错误的估计日期。这对其他观众也会有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-14
  • 1970-01-01
  • 2017-12-20
  • 2020-05-15
  • 2020-02-05
  • 1970-01-01
  • 2018-09-14
相关资源
最近更新 更多