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

Question: Anyway to get error message which is returned by server #307

Closed
buenyaminyilmaz opened this issue Feb 22, 2022 · 9 comments
Closed
Labels

Comments

@buenyaminyilmaz
Copy link

Hi there,
i am trying to use mande.
Is there any way to catch the error is given from server.
For an example:

 try {
          const response = await registerApi.post({
            email: this.email,
            password: this.password,
            passwordConfirmed: this.passwordRepeat,
          })

          resolve(response)
        } catch (error) {
          console.log('ServerError', error)
          reject(error)
        }

registerApi is a mande instance. The server returns for me i this case 422 (Unprocessable Entity) and an error message.
So how can i show this error message? console.log('ServerError', error) shows ServerError Error: Unprocessable Entity.
Thank you in advance

@wizbit
Copy link
Contributor

wizbit commented Mar 24, 2022

The error contains 2 extra items, response & data.
I would handle the server error like this

 try {
   const response = await registerApi.post({
     email: this.email,
     password: this.password,
     passwordConfirmed: this.passwordRepeat,
   })
   resolve(response)
} catch (error) {
  console.log('ServerError', await error.response.json())
  reject(error)
}

@alexey-us
Copy link

alexey-us commented Mar 30, 2022

Hello, I am also looking to catch the error from server

try {
    await this.api.post(deviceId + '/group/' + groupId + 's');
}
catch (error) {
   console.log(await error.response.json());
}

await error.response.json() - returned: Uncaught (in promise) TypeError: Failed to execute 'json' on 'Response': body stream already read

@wizbit
Copy link
Contributor

wizbit commented Mar 30, 2022

error.data might contain the data you are after.

@alexey-us
Copy link

Thanks!

@johannesronaldsson
Copy link

johannesronaldsson commented Apr 12, 2022

Interesting... so I get nothing back, while the response gives JSON back. Not quite sure what's going on here... Love the library btw.

Screenshot 2022-04-12 at 16 14 21

Screenshot 2022-04-12 at 16 13 00

Screenshot 2022-04-12 at 16 13 07

@koficoud
Copy link

@johannesronaldsson You can access to the data using: error.body

try {
  const login = await api.post(apiUri, credentials);
} catch (error) {
  console.error(error.body)
}

@posva posva added the docs label Jun 17, 2022
@bk973
Copy link

bk973 commented Aug 7, 2022

Was this issue resolved?
@buenyaminyilmaz were the responses helpful

@buenyaminyilmaz
Copy link
Author

Hi there, i will just test it out.
i switched to axios, because i was in hurry

@posva
Copy link
Owner

posva commented Feb 27, 2023

I created #378 to track

@posva posva closed this as not planned Won't fix, can't repro, duplicate, stale Feb 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants