We need to throw a 500 Internal Server Error with a custom object in the response that we will examine in NextJS. On the Spring back-end we return a ResponseEntity<CustomObject>
as below. The API call has been verified to work correctly. In Swagger, it outputs both the status and the custom object in the response correctly as expected.
return new ResponseEntity<CustomObject>(customObject, HttpStatus.INTERNAL_SERVER_ERROR);
In the NextJS Axios fetch where this is called, we do a try/catch
. The catch
only accepts a single param, error
:
try {
const response = await get<any>("/api-url/" + props.refId);
..
} catch(error) {
..
}
The Catch’s error
does not contain our returned custom object in the response. It only looks like this:
digest: '1100826754'
message: 'Request failed with status 500'
stack: 'AxiosError: Request failed with status code 500n ...'
So is it a NextJS limitation, or are we not catching correctly? Or is it impossible to have anything in the response for a 500 error?