Amplify Rest API PUT Request is throwing message : unknown error
Before opening, please confirm:
- [X] I have searched for duplicate or closed issues and discussions.
- [X] I have read the guide for submitting bug reports.
- [X] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
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
Expected behavior
I am not getting backend errors properly this is the response of API
Reproduction steps
Update any PUT request that's has an error
Code Snippet
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
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?
Hi @chrisbonifacio ,
It is a restricted PUT request
This is how I am configuring my endPoints and auth
What I am doing is verifying validation Requests i.e. "400 Bad Request" and "500 Internal Error"
API Response is 400 with handled response
But in Catch of PUT Request, I am getting an Unknown Error
"aws-amplify": "^6.0.13",
@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 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...
It SHOULD return the same response that is API is returning..
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.
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.
Similar to if I check the payloads of my POST and PUT request, which both show the full failed response payload
POST (400 response)
PUT (400 response)
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
Payload
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.
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
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!
@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 , thank you! with this error response shape I'm able to retrieve error message on client side by accessing error.message field.
Hi @chrisbonifacio , Sorry for the late response, Is there any way we can get an error response other than the message keyword.?