想要向客户端控制台发送 AWS lambda 错误 [英] Want to get an AWS lambda error to client console

查看:31
本文介绍了想要向客户端控制台发送 AWS lambda 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取从 Lambda 传递的错误消息以显示在我的客户端上.基本上我从我的客户端调用一个 API 网关端点,它执行一个 lambda.这个函数有一个特定的条件,如果这个条件失败,我想向我的客户发送一条特定的消息.这是我目前拥有的.

I'm trying to get an error message passed from Lambda to show up on my client. Basically I call an API Gateway endpoint from my client, which executes a lambda. There is a certain condition in this function and if this condition fails I want to send a specific message to my client. This is what I currently have.

图书馆:

export function failure(body) {
    return buildResponse(500, body);
}

function buildResponse(statusCode, body) {
    return {
        statusCode: statusCode,
        headers: {
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Credentials': true
        },
        body: body
    };
}

和实际功能:

export async function main(event, context) {

    const data = JSON.parse(event.body);

    const saleCount = await getSaleCount();

    if (count >= 25) {
        // This is the error i'm referring to
        return failure({ status: false, error: 'My error' });
    }

    const params = {
        TableName: process.env.salesTable,
        Item: {
            foo: bar
        }
    };

    try {
        await dynamoDbLib.call('put', params);
        return success(params.Item);
    } catch(e) {
        console.log(e);
        return failure({ status: false });
    }
}

async function getSaleCount(foo) {
    // do stuff and return value
    return bar;
}

我似乎无法在前端收到此错误中的消息.有一个 try-catch 子句,错误肯定会被 catch 捕获,但错误只是显示为

I can't seem to get the message in this error on the frontend. There is a try-catch clause and the error is definitely being picked up by the catch, but the error simply displays as

Error: Request failed with status code 500
    at createError (createError.js:16)
    at settle (settle.js:17)
    at XMLHttpRequest.handleLoad (xhr.js:59)

此外,我实际上可以在网络"选项卡上看到该消息作为回复:

Furthermore, I can actually see the message as a response on my Network tab:

{"status": "false", "error": "My error"}

这是一个 Lambda 代理集成,所以我尝试按照此页面上的方法进行操作:https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html含义:

It is a Lambda Proxy integration, so I've tried following the methods on this page: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html Meaning:

// This returns me a 502 on the frontend
throw new Error('My error');

// This also doesn't allow me to access the message
return {statusCode: 400, body: 'My error'};

我现在完全不知所措,所以我非常感谢您的任何意见.非常感谢!

I'm at an absolute loss now so I'd really appreciate any input. Many thanks!

推荐答案

就我而言,e.response.data 为空,因为我得到的响应不是 json 而是文本.但在我的网络响应中,我可以看到该消息.

In my case the e.response.data was null, because the response I was getting was not a json but a text. But in my network response I could see the message.

我的解决方案是在请求对象中添加一个 responseType = "text".之后,我可以在数据属性中看到一些文本.

My solution was to add a responseType = "text" in the request object. After that I could see some text in the data attribute.

responseType 允许的值为arraybuffer"、blob"、document"、json"或text";如果未指定,则默认为json".

Allowed values for responseType are "arraybuffer", "blob", "document", "json" or "text"; and it defaults to "json" if not specified.

您可以在此处阅读更多信息:https://docs.amplify.aws/lib/restapi/fetch/q/platform/js#accessing-query-parameters--body-in-lambda-proxy-function

You can read more here: https://docs.amplify.aws/lib/restapi/fetch/q/platform/js#accessing-query-parameters--body-in-lambda-proxy-function

这篇关于想要向客户端控制台发送 AWS lambda 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆