Reputation: 99
I am using open-api Generator (https://openapi-generator.tech), specifically the typescript-axios generator to autocreate an typescript Api interface to my fastAPI backend.
However when I use this api and my backend returns an HttpException with a code (e.g. 409) and a detailed message about the error, e.g.
raise HTTPException(status_code=409, detail="Tag with same name already exists for this entry!")
only the status code is available in the error object returned by the Typescript Api, the message seems to become overwritten with a standard message. This is the error object I get from the API:
{
"message": "Request failed with status code 404",
"name": "AxiosError",
"config": {
"transitional": {
"silentJSONParsing": true,
"forcedJSONParsing": true,
"clarifyTimeoutError": false
},
"transformRequest": [
null
],
"transformResponse": [
null
],
"timeout": 0,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
"maxContentLength": -1,
"maxBodyLength": -1,
"env": {
"FormData": null
},
"headers": {
"Accept": "application/json, text/plain, */*"
},
"method": "get",
"url": "XXXXX"
},
"code": "ERR_BAD_REQUEST",
"status": 404
}
and this is the Javascript code interacting with the API
import { DefaultApi } from "./api/backend";
const apiInstance = new DefaultApi(null, "http://localhost:8000");
// Base fetching hook
const useApi = (apiMethod) => {
const [data, setData] = useState(null);
const [message, setMessage] = useState(null);
const [timestamp, setTimestamp] = useState(null);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState(null);
const interact = async (...args) => {
try {
setIsLoading(true);
const response = await apiInstance[apiMethod](...args);
setData(response.data.data);
setMessage(response.data.message);
setTimestamp(response.data.timestamp);
console.debug(`Current response is: ${JSON.stringify(response, null, 2)}`);
setError(null);
} catch (error) {
console.error(`Error fetching data: ${JSON.stringify(error, null, 2)}`);
setError(error.toString());
} finally {
setIsLoading(false); // Set loading to false regardless of success or error
}
};
return { data, message, timestamp, interact, isLoading, error };
};
So the detailed message from the backend is there in the "response" object, but that is not available when an error occurred of course, and it not in the "error" object.
How can I achieve to obtain the detailed error message from the server? Thanks for any help!
Upvotes: 0
Views: 186