Closed dadaguai-git closed 3 months ago
Looking at the source code, it seems that the convention is that the information that the body must return is in the message
export const parseJsonError: ErrorParser = async (response?: HttpResponse) => {
if (!response || response.statusCode < 300) {
return;
}
const body = await parseJsonBody(response);
const sanitizeErrorCode = (rawValue: string | number): string => {
const [cleanValue] = rawValue.toString().split(/[\,\:]+/);
if (cleanValue.includes('#')) {
return cleanValue.split('#')[1];
}
return cleanValue;
};
const code = sanitizeErrorCode(
response.headers['x-amzn-errortype'] ??
body.code ??
body.__type ??
'UnknownError'
);
const message = body.message ?? body.Message ?? 'Unknown error';
const error = new Error(message);
return Object.assign(error, {
name: code,
$metadata: parseMetadata(response),
});
};
But if that's the case, I don't think we should constrain the user body's key, right?
Hi @dadaguai-git I believe this is a duplicate of another open issue. We have a fix merged and is only pending release. Please track this issue for updates: https://github.com/aws-amplify/amplify-js/issues/12730
The fix for this issue has been released with Amplify JS v6.0.15. Please let us know if you're run into any other issues! Associated documentation can be found here: https://docs.amplify.aws/react/build-a-backend/restapi/fetch-data/#access-http-response-from-errors
Thank you for your contribution!
I am having issue where amplify throws error response of
UnknownError: Unknown error
at parseJsonError (json.mjs:34:19)
at async parseRestApiServiceError (serviceError.mjs:26:28)
at async job (createCancellableOperation.mjs:35:23)
when the response has content-type of "text/plain; charset=utf-8" Amplify Version: v6.0.30
Or are this discussion just for AWS error handling? If I return my own errors from api should I read the error content from error._response
instead?
Before opening, please confirm:
JavaScript Framework
Vue
Amplify APIs
REST API
Amplify Version
v6
Amplify Categories
api
Backend
Amplify CLI
Environment information
pnpm run dev
Describe the bug
It's not a bug, it's a question I have, sorry for using issue for questions
I'm using a custom hook function for api requests, but I'm having a problem. If the request reports a 400 error, I can't get the body content returned by the backend after accepting the error using .catch.
The request accepts the 400 code returned by the backend, how to get the body
Expected behavior
Want to get the content of the body returned by the backend
Reproduction steps
The code is shown below
Code Snippet
Log output
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