简体   繁体   English

如何使用 aws-amplify 处理 api 错误?

[英]How to handle api errors using aws-amplify?

I'm currently trying to POST data to my aws lambda functions triggered by aws api-gateway using the aws-amplify react lib.我目前正在尝试使用aws-amplify react lib aws-amplify数据发布到由 aws api-gateway 触发的 aws lambda 函数。

Here is the code :这是代码:

API.post("snippets","snippets/", {
        body: data,
    }).then(response => response).catch(console.log(err))

In the main case, everything is OK.在主要情况下,一切正常。

But my lambda function is design to validate the input data and return a status code 400 with a returned payload looking like that :但我的 lambda 函数旨在验证输入数据并返回状态代码400 ,返回的有效负载如下所示:

{
    "errors": [
        {
            "field": "title",
            "message": "This field is required"
        }
    ]
}

I would like to catch those errors in order to display them in the frontend but aws-amplify seems to have an undocumented behavior.我想捕获这些错误以便在前端显示它们,但aws-amplify似乎有一个未记录的行为。

By default, status code 400 returned are throw with a default error message :默认情况下,返回的状态代码400会抛出默认错误消息:

Error: Request failed with status code 400
    at createError (createError.js:16)
    at settle (settle.js:18)
    at XMLHttpRequest.handleLoad (xhr.js:77)

Is there a way to get the returned payload instead of this magical error?有没有办法获得返回的有效载荷而不是这个神奇的错误?

It turns out that under the hood, aws-amplify use Axios to make http calls.事实证明,在aws-amplifyaws-amplify使用 Axios 进行 http 调用。

When using Axios, you have to console.log(error.response) : https://github.com/axios/axios/issues/960使用 Axios 时,你必须console.log(error.response)https : //github.com/axios/axios/issues/960

Here is the fix I've made :这是我所做的修复:

API.post("snippets","snippets/", {
        body: data,
    }).then(response => response).catch(error => console.log(error.response.data))

A Pull Request on the aws-amplify documentation is open : https://github.com/aws/aws-amplify/pull/633 aws-amplify文档上的拉取请求已打开: https : //github.com/aws/aws-amplify/pull/633

I also faced the similar issues, It showed the default error message "Request failed with status code 400", instead of the message that is returned from API.我也遇到了类似的问题,它显示了默认的错误消息“请求失败,状态码为 400”,而不是从 API 返回的消息。

I logged the Error object and it did not show the response attribute in it.我记录了 Error 对象,但它没有在其中显示响应属性。 But we do have response attribute.但是我们确实有响应属性。 I tried logging the Error.response and it did contain the response sent from the API.我尝试记录 Error.response,它确实包含从 API 发送的响应。

Just figured out this by going through the 'Cancel API requests' Amplify docs .只是通过浏览“取消 API 请求” Amplify 文档来解决这个问题

From what I can see this is the contents of the error object returned by the API call:从我可以看到这是API调用返回的错误对象的内容:

在此处输入图片说明

Heres what I am doing to just print out the error, obviously you would do a lot more here but its a good start.继承人我正在做的只是打印出错误,显然你会在这里做更多的事情,但这是一个好的开始。

async uploadUser(state, payload) {

const promise = API.graphql({
    query: createUser,
    variables: { input: payload },
});

try {
    await promise;
} catch (error) {
    // Print out the actual error given back to us.
    console.log(error.errors[0].message);
    
    // If the error is because the request was cancelled we can confirm here.
    if (API.isCancel(error)) {

        // handle user cancellation logic.
        console.log(error.message);
        
    }
}

Hope that helps 😃希望有帮助😃

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM