Bug - Amplify/Lambda reporting uninformative errors
See original GitHub issueI describe my issue at length on Stack Overflow, but I’m unsure if it is an Amplify bug, so I’m reproducing it (more briefly) here.
Basically, when I return a response from an API Gateway/Lambda with an error statusCode (>= 400), Amplify on the frontend catches the error (good), but the error has none of the information in my structured Lambda response. Instead it’s just a Network Error, with no helpful information.
As in, the below is a successful response:
{
"statusCode": 200,
"headers": {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true
},
"body": "{ /* whatever */ }"
}
And I’m returning this on the Lambda end like so:
callback(null, objectShownAbove);
But if I change that 200
to a 400
in my Lambda, the catch
on the frontend’s Amplify.get
promise is called (as it should be), and/but the error argument is a generic Network Error
that doesn’t reference my response body or anything.
Error: Network Error
at createError (createError.js:16)
at XMLHttpRequest.handleError (xhr.js:87)
Additionally, the frontend console logs some other non-specific errors (complaining about the response headers, which are identical to the success condition):
GET https://abcdefg.execute-api.us-west-2.amazonaws.com/development/my-endpoint 403 ()
Failed to load https://abcdefg.execute-api.us-west-2.amazonaws.com/development/my-endpoint: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 403.
And info about CORB issues, which doesn’t seem sensible, given that a success response works, and all that’s changed is the statusCode
number.
Cross-Origin Read Blocking (CORB) blocked cross-origin response https://abcdefg.execute-api.us-west-2.amazonaws.com/development/my-endpoint with MIME type application/json.
XHR failed loading: GET "https://abcdefg.execute-api.us-west-2.amazonaws.com/development/my-endpoint".
Is this an Amplify bug? A Lambda bug? Am I doing something wrong?
Thanks.
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (3 by maintainers)
Top GitHub Comments
This does end up depending on how you configure your API Gateway, e.g. Lambda proxy etc. If you are NOT using a Lambda proxy setup, you will need to map these error responses/codes within API Gateway, see extensive post on that here: https://aws.amazon.com/blogs/compute/error-handling-patterns-in-amazon-api-gateway-and-aws-lambda/
If you are using a Lambda Proxy, you will just return as a specific type of proxy response from Lambda: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html
This issue has been automatically locked since there hasn’t been any recent activity after it was closed. Please open a new issue for related bugs.
Looking for a help forum? We recommend joining the Amplify Community Discord server
*-help
channels or Discussions for those types of questions.