Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get error message #377

Open
christian-hess-94 opened this issue Sep 4, 2020 · 9 comments
Open

How to get error message #377

christian-hess-94 opened this issue Sep 4, 2020 · 9 comments

Comments

@christian-hess-94
Copy link

christian-hess-94 commented Sep 4, 2020

I have a java rest api (it's old) and currently it send some text messages based on the authentication status of the information sent in the request.

When using pure fetch (with sagas) I manage to retrieve this message by running await error.text() (inside a catch(error))

However, when using redux-request with the fetch-driver, I get an error as follows:

Uncaught (in promise) TypeError: Failed to execute 'text' on 'Response': body stream already read at log

This is the code:

When clicking a button, execute this function:

const log = async() => {
    const { data, error } = await dispatch(login("/login", { user, password})); //dispatch a "login" action
    console.log("data: ", data); //if the data matches with a user I get the data back just fine
    const errorMessage = error.text(); //this is the line that errors out if the data doesn't 
    console.log("errorMessage: ", errorMessage); //this doesn't get called because of the error on the above line
}

This is my "login" action:

export const login = (url, body) => {
  return ({
    type: 'LOGIN',
    request: {
      url, //the URL is appended to the baseURL set in the driver
      body: JSON.stringify(body),
      method: 'post',
      headers
    }
  })
}

The request is fired just fine. Again, If I input a correct combination of "user" and "password" I get the data back just fine. I just need to know how to get the error messages back.

Also, in my Redux DevTools, the LOGIN, LOGIN_SUCCESS and LOGIN_FAILED actions are fired just fine.

@klis87
Copy link
Owner

klis87 commented Sep 4, 2020

@christian-hess-94 Try to inspect error.data, do you see anything there? If not, we will need to extend fetch driver to allow error messages other than json.

@christian-hess-94
Copy link
Author

@klis87 Thanks for replying will check it right now and get back to you

@klis87
Copy link
Owner

klis87 commented Sep 4, 2020

@christian-hess-94 no problem. Btw, this is the problematic line - https://github.com/klis87/redux-requests/blob/master/packages/redux-requests-fetch/src/fetch-api-driver.js#L44

I assumed error response will use always JSON, but that might be too simplified approach. As a last resort, we will introduce new attr like errorResponseType - will work like responseType but for errors.

@christian-hess-94
Copy link
Author

Well, when callind error.json() the error is the same but it reads as TypeError: Failed to execute >'json'< on 'Response': body stream already read at log

@christian-hess-94
Copy link
Author

I'm assuming this means that the json() command was executed before, but when running something like JSON.stringify(error) it returns an empty object

@klis87
Copy link
Owner

klis87 commented Sep 4, 2020

So we need this errorResponseType then, maybe you would like to work on PR if you have some time? :)

@christian-hess-94
Copy link
Author

I'm still trying to grasp it all,just started using the package today. Maybe some day in the future or during a weekend (currently having a lot of work, i'm renovting the entire ecosystem of my company [practically alone!])

I'm using React and I noticed you have added a couple of facilitators for it. Does the package support the usage of hooks?

@christian-hess-94
Copy link
Author

Problem is i'm at home right now and am not able to connect to the company's VPN (for w/e reason) so i'm unable to fire the requests. I think i'm only gonna be able to check in on this properly on Tuesday

@klis87
Copy link
Owner

klis87 commented Sep 4, 2020

Ok, no problem, maybe I will have some time before.

Alternatively you could try axios driver, actually syntax is quite similar and with using this library it doesnt really matter you use axios or fetch.

Regarding React, yes, hooks are supported, for example https://redux-requests.klisiczynski.com/docs/guides/usage-with-react#usequery

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants