AppSync JavaScript Resolvers util.error Not Working

1

Hello,

I have just started using the javaScript AppSync resolvers after using VTL for years. One issue I am coming across is the util.error(...) or util.appendError(...) utility functions appear to have no effect.

My set-up currently includes a function to call and http data source, and a query which uses this function. My function calls an API Gateway REST api with a GET request. The response mapping template simply returns the parsed JSON, and if the response was not 200, call appendError.

import { util } from '@aws-appsync/utils';

export function request(ctx) {
    return {
        method: 'GET',
        params: {
            headers: {
                'Content-Type': 'application/json',
                'X-Api-Key': '<redacted>'
            }
        },
        resourcePath: '/dev/<redacted>/' + ctx.stash.id + '.json'
    };
}

export function response(ctx) {
    if (ctx.result.statusCode !== 200){
        return util.appendError(ctx.result.body, ctx.result.statusCode);
    }
    else {
        return JSON.parse(ctx.result.body);
    }
}

My query is as follows:


import { util } from '@aws-appsync/utils';


export function request(ctx) {

    ctx.stash.id = ctx.args.id

    return {};
}

export function response(ctx) {
    return ctx.result;
}

Part of the the ResponseFunctionEvaluation in CloudWatch is as follows:

"context": {
        "arguments": {
            "id": "testr"
        },
        "result": {
            "headers": {
                "content-length": "38",
                "Content-Type": "application/json",
                ...
                "X-Cache": "Error from cloudfront"
            },
            "statusCode": 404,
            "body": "{\"message\": \"That key does not exist\"}"
        },
        "prev": {
            "result": {}
        },
        "stash": {
            "id": "testr"
        },
        "outErrors": []
    },
"fieldInError": false,
"errors": [],

And part of the AfterResponseFunctionEvaluation:

"context": {
        "arguments": {
            "id": "testr"
        },
        "prev": {},
        "stash": {
            "id": "testr"
        },
        "outErrors": []
    },
"fieldInError": false,
"errors": [],

VTL resolvers appear to work correctly. I am not sure what I am doing wrong here?

1 Antwort
1

Haha of course just minutes after asking this question I solved the issue thanks to this page.

Specifically, I needed to replace

return util.appendError(ctx.result.body, ctx.result.statusCode);

with

return util.appendError(ctx.result.body, `${ctx.result.statusCode}`);

If the second parameter is not a string, it seems no error gets logged at all, which led to hours of confusion.

austint
beantwortet vor 10 Monaten
  • I, too, lost hours to this same error. It's a silent failure. Even the @aws-appsync/base linter plugin did not catch it.

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen