Can't sent an error to the login page #13788
-
Hi everyone. My issue might seem a bit basic/trivial, but I can't seem to find a solution and since I'm not well versed in backend related stuff I'm asking here. Inside the api folder, I'm trying to send a response with an error inside the catch after I put an invalid username/password, and the error seems to be ok, but when I try to send it and use said error in the handler of the login page I don't get it (although I do get a response). Api/Login.js
/login.js
fetchJson.js (this one is from the with-iron-session example)
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The code looks correct, you should be able to do something like this: setMessage(error.data.message); You're returning the error info as const data = await response.json();
if (response.ok) {
return data;
}
// This shows the 401 unauthorized response without the error I sent in the api/login page
console.log('fetcher', response);
const error = new Error(response.statusText);
error.response = response;
error.data = data; If your custom error was thrown, then the error data is in
|
Beta Was this translation helpful? Give feedback.
The code looks correct, you should be able to do something like this:
You're returning the error info as
json
, then it's being set toerror.data
here:If your custom error was thrown, then the error data is in
error.data
.error.message
would beresponse.statusText
, which in the case of your custom error won't have anything important.