https://serverless-stack.com/ logo
#help
Title
# help
e

Emil Styrke

02/22/2022, 10:24 AM
Minor issue, but I'm running an AppSync API with Python for the resolvers. When I run local dev and get a resolver crash, the output from the API doesn't show the real error, but some crash in the JS handler code:
Copy code
{
  "data": {
    "events": null
  },
  "errors": [
    {
      "path": [
        "events"
      ],
      "data": null,
      "errorType": "Runtime.UnhandledPromiseRejection",
      "errorInfo": null,
      "locations": [
        {
          "line": 28,
          "column": 3,
          "sourceName": null
        }
      ],
      "message": "TypeError: Cannot read property 'join' of undefined"
    }
  ]
}
Is this expected?
The real Python error shows in the "sst start" console, so I'm not too bothered by this. 🙂
n

Noah D

02/22/2022, 10:31 AM
Hey Emil, I primarily use typescript with sst so might not be too much of a help here, but if that error is coming from a JS handler you have made. you should check for where you call
join
on. My suspicion is that it will be on an object which can be undefined if the circumstance isn't correct, and youll need to handle situations where it is undefined. For example my lambdas
request.requestContext.authorizer
can be undefined so I have to make sure it exists by doing something like the following when i get
userIds
to ensure it is defined before using it elsewhere
Copy code
export const getUserId = (event: EndpointEvent): string => {
  if (!event.requestContext.authorizer || !event.requestContext.authorizer.iam) {
    throw new Error('Not authorized');
  }
  const userId = event.requestContext.authorizer.iam.cognitoIdentity.identityId;
  if (!userId) {
    throw new Error('Not Authorized');
  }
  return userId;
};
f

Frank

02/22/2022, 5:21 PM
Thanks @Noah D
@Emil Styrke it seems AppSync is trying to format the Lambda error and ran into this error. Do you get the same API response if u
sst deploy
the errorous Lambda code?
e

Emil Styrke

02/23/2022, 7:18 PM
Thanks @Noah D, but I don't have any typescript handlers of my own. @Frank if I `sst deploy`I get the Python error in the response as I expected. I suspect some of the live dev wrapper code is throwing that error.
Likely culprit (in the lambda function code that gets deployed by live dev):
Copy code
// handle response error
    if (responseError) {
      // Note: Do not throw. If error is thrown, errorType becomes
      //       "Runtime.UnhandledPromiseRejection". We need to preserve the
      //       original error type.
      const e = new Error();
      e.name = responseError.errorType;
      e.message = responseError.errorMessage;
      e.stack = responseError.stackTrace.join("\n");
      _ref.callback(e);
    }
The line number doesn´t match the error, but I bet that responseError.stackTrace is undefined.
Digging further: this is what the payload looks like when I pick it from cloudwatch and decode it:
Copy code
{"responseError":{"errorMessage":"\'user_id\'","errorType":"KeyError"}}
So indeed no
stackTrace
key there.
f

Frank

02/23/2022, 8:13 PM
Ah! Thanks for digging into this. Putting in a fix.
e

Emil Styrke

02/23/2022, 8:14 PM
f

Frank

02/23/2022, 8:14 PM
Even better.
e

Emil Styrke

02/23/2022, 8:15 PM
I still don't get the stack trace in the graphql error response, but I suspect that is just how AppSync works.
f

Frank

02/23/2022, 8:15 PM
I see. what response do u get after this fix?
e

Emil Styrke

02/23/2022, 8:37 PM
Copy code
"errors": [
    {
      "path": [
        "events",
        3,
        "participants",
        0,
        "participant"
      ],
      "data": null,
      "errorType": "KeyError",
      "errorInfo": null,
      "locations": [
        {
          "line": 37,
          "column": 7,
          "sourceName": null
        }
      ],
      "message": "'user_id'"
    }
The location seems to refer to the offending field specifier in the GraphQL query document.
f

Frank

02/23/2022, 9:43 PM
I see. Can I bother you for what the actual error looks like after u deploy?
I’m going to create an GitHub issue and document this.
e

Emil Styrke

02/24/2022, 9:14 AM
Unfortunately I don't have the same error after deploy (this is early stages, and I don't have connectivity to the RDS database yet), but the error I do get looks very similar (no stack trace):
Copy code
{
  "data": {
    "events": null
  },
  "errors": [
    {
      "path": [
        "events"
      ],
      "data": null,
      "errorType": "OperationalError",
      "errorInfo": null,
      "locations": [
        {
          "line": 28,
          "column": 3,
          "sourceName": null
        }
      ],
      "message": "(2003, \"Can't connect to MySQL server on '[host]' (timed out)\")"
    }
  ]
}
f

Frank

02/27/2022, 9:16 AM
Thanks @Emil Styrke. Opened an issue to track this https://github.com/serverless-stack/serverless-stack/issues/1438
e

Emil Styrke

03/16/2022, 9:00 AM
Oh, sorry, I don't think I was clear enough. The difference in error message is due to the fact that I don't have RDS connectivity in the deployed environment. So after the previous fix, everything seems to be behaving as expected. 👍
f

Frank

03/22/2022, 6:22 AM
Ah I see. Thanks for the clarification!
9 Views