For displaying the specific error message, instead of a generic unauthorized message, you will need to manage the API response properly in your .then() block rather than depending on the .catch() for this case.
Why?
Your API returns a JSON error response with status 200 or a non-error HTTP status) even when the ride is not found, so .then() is executed, not .catch(). The .catch() runs only on network errors or non-2xx HTTP status code.
How to fix?
Modify your .then() to check if the response keeps an error message to handle it accordingly, for example:
export const authUser = (rideId, action) => {
return (dispatch, getState) => {
const postData = {
consumer: "Rapi",
classId: rideId,
action: action,
};
return Api.post('/api/xx/yy/auth', postData)
.then((response) => {
if (response.errorCode) {
// Handle error response from API
console.warn("Error:", response.message);
return Promise.reject(new Error(response.message)); // to trigger catch if needed
}
const authorized = response.authorized === true;
if (authorized) {
console.log("User is authorized");
return true;
} else {
console.warn("User is NOT authorized");
return false;
}
})
.catch((error) => {
console.error("Authorization failed:", error.message || error);
return false;
});
};
};