amplify-js icon indicating copy to clipboard operation
amplify-js copied to clipboard

Amplify Rest API PUT Request is throwing message : unknown error

Open saharJ95 opened this issue 1 year ago • 6 comments

Before opening, please confirm:

JavaScript Framework

React

Amplify APIs

REST API

Amplify Version

v6

Amplify Categories

api

Backend

Other

Environment information

# Put output below this line


Describe the bug

I am using amplify RestAPi PUT request... I am getting this Unkonwn Error image

Expected behavior

I am not getting backend errors properly this is the response of API image

Reproduction steps

Update any PUT request that's has an error

Code Snippet

image

I am using this code fir all REST API request but PUT is behaving odd in error cases

Log output

// Put your logs below this line


aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

saharJ95 avatar Feb 11 '24 07:02 saharJ95

Hi @saharJ95 can you provide some information about your API?

For example, is it restricted only to authenticated user or does it allow guest users?

Curious if IAM is being used to authorize requests.

Are you adding any additional configurations to the API library in your Amplify.configure call?

Would you be able to provide a code example of your PUT endpoint or reproduction steps so that we can try to reproduce the issue?

Lastly, what version of aws-amplify are you using?

chrisbonifacio avatar Feb 11 '24 07:02 chrisbonifacio

Hi @chrisbonifacio , It is a restricted PUT request This is how I am configuring my endPoints and auth image

What I am doing is verifying validation Requests i.e. "400 Bad Request" and "500 Internal Error"

image API Response is 400 with handled response image

But in Catch of PUT Request, I am getting an Unknown Error

"aws-amplify": "^6.0.13",

saharJ95 avatar Feb 11 '24 08:02 saharJ95

@saharJ95 thanks for the quick response!

Is this the only endpoint that is restricted the same way and failing with a 400 status code?

The status code suggests there's something invalid about the request itself like the body vs the authorization header. Can you confirm that the data in the payload is what you'd expect in the route handler logic? Do you see any errors in the Cloudwatch logs for the lambda that handles that route?

chrisbonifacio avatar Feb 12 '24 01:02 chrisbonifacio

@chrisbonifacio Ok maybe I am not communicating my issue correctly.

I want to catch understandable API errors (400,401,500,404) in my catch block that is working fine with all other rest APIs method (GET, POST , DEL) . In PUT case if API returns 404,500,401 (Handled Responses from my backend), the catch block of PUT request gives UNKNOWN ERROR... image

It SHOULD return the same response that is API is returning..

saharJ95 avatar Feb 12 '24 06:02 saharJ95

Hi @saharJ95

So, I'm able to reproduce the behavior you're describing but I'm not sure if I understand how you're getting the full error response payload to be logged to the console. I only get UnknownError for each http request that I fail with a 400 status code.

Here are the steps I took to reproduce:

I created a REST API with express integration using the Amplify CLI. I add some routes that respond with a 400 status code and a json body.

On the front end, I just have a few buttons to make a successful POST, a failed POST, and failed PUT requests.

This is what gets logged when I catch the error for either a POST or PUT request.

Screenshot 2024-02-14 at 2 20 58 PM

Here's the Express server code for my reproduction app: https://github.com/chrisbonifacio/ampv6-next-app/blob/main/amplify/backend/function/todoFunction/src/app.js

And here's the front end code: https://github.com/chrisbonifacio/ampv6-next-app/blob/main/app/page.tsx

Is there anything I should change about my code to be similar enough to yours to reproduce the issue? Or can you provide me a small sample app that reproduces the difference in behavior?

This screenshot you provided doesn't look like what would get logged to the console when catching the error, it looks like the network payload.

image

Similar to if I check the payloads of my POST and PUT request, which both show the full failed response payload

POST (400 response) Screenshot 2024-02-14 at 2 29 41 PM

PUT (400 response)

image

chrisbonifacio avatar Feb 14 '24 19:02 chrisbonifacio

One thing that I noticed is that you are sending a msg field in your response payload. Is that consistent with your other routes? I've been sending a message field and they seem to be consumed and logged properly.

But, when I try sending a msg field instead, similar to your route, I don't get the text. Just "Unknown error".

Error caught and logged image

Payload image

So, I think now I've correctly understood and reproduced the issue finally.

I might've been a little stumped by the screenshots you shared because I assumed you were showing me what the caught and logged error SHOULD look like but now I see you were just showing me the payload for the 400 PUT request 😅 Sorry about that.

chrisbonifacio avatar Feb 14 '24 19:02 chrisbonifacio

I have a similar issue - in case if response code is 2xx, then I have no issues reading response object and retrieveng data from it, but if response contains error I can't read error message from it. I described my issue here https://stackoverflow.com/questions/78030287/aws-amplify-handling-errors-from-backend

28061986 avatar Feb 21 '24 09:02 28061986

Hi 👋 Closing this as we have not heard back from you. If you are still experiencing this issue and in need of assistance, please feel free to comment and provide us with any information previously requested by our team members so we can re-open this issue and be better able to assist you.

Thank you!

chrisbonifacio avatar Feb 22 '24 19:02 chrisbonifacio

@28061986

I have a similar issue - in case if response code is 2xx, then I have no issues reading response object and retrieveng data from it, but if response contains error I can't read error message from it. I described my issue here https://stackoverflow.com/questions/78030287/aws-amplify-handling-errors-from-backend

Your lambda needs to return a body in the response with a message field.

chrisbonifacio avatar Feb 22 '24 19:02 chrisbonifacio

@chrisbonifacio , thank you! with this error response shape I'm able to retrieve error message on client side by accessing error.message field.

28061986 avatar Feb 25 '24 10:02 28061986

Hi @chrisbonifacio , Sorry for the late response, Is there any way we can get an error response other than the message keyword.?

saharJ95 avatar Feb 29 '24 04:02 saharJ95