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 json response? #171

Closed
SharkFourSix opened this issue Feb 19, 2023 · 5 comments
Closed

How to get error json response? #171

SharkFourSix opened this issue Feb 19, 2023 · 5 comments

Comments

@SharkFourSix
Copy link

SharkFourSix commented Feb 19, 2023

According to this https://github.com/elbywan/wretch#catchers- , I should be able to get a response JSON object even during errors. However, the error parameter does not have a .json property. The .text property is present but having to serialize to JSON manually is very counterintuitive.

wretch("...")
  .get()
  .badRequest((err) => console.log(err.json[0].Message))
  .res();
type WretchError = Error & {
  status: number;
  response: WretchResponse;
  text?: string;
  json?: Object;
};

Perhaps there is another way that I'm not aware of?

@elbywan
Copy link
Owner

elbywan commented Feb 19, 2023

Hey @SharkFourSix,

However, the error parameter does not have a .json property. The .text property is present but having to serialize to JSON manually is very counterintuitive

You can call the .errorType method to specify the error body type. Putting .errorType("json") in the chain should populate the error.json field.

@SharkFourSix
Copy link
Author

Thanks. 👍 That worked. I was about to switch to axios because of that that little inconvenience which can build up into lots of boilerplate code 😅

@SharkFourSix
Copy link
Author

SharkFourSix commented Feb 20, 2023

Bump...

So it appears that this applies to all the error "catchers"? Is there a way to only call the parsing on demand in the handlers?

Because as it appears I have to design my API to always return a JSON response, again, something which is counterintuitive :/

I'm getting a JSON conversion error because some responses are only returning standard HTTP responses without any additional JSON data.

Can't you only parse JSON by looking at the content-type response?

@SharkFourSix SharkFourSix reopened this Feb 20, 2023
elbywan added a commit that referenced this issue Feb 20, 2023
@elbywan
Copy link
Owner

elbywan commented Feb 20, 2023

Because as it appears I have to design my API to always return a JSON response, again, something which is counterintuitive :/

Absolutely, I fully agree 👍.

Can't you only parse JSON by looking at the content-type response?

No, but this is an excellent idea! I just released v2.5.0 which will automatically deserialize the error body if the response content-type header is set to application/json, which should make it unnecessary to set .errorType in the first place if your server sets the header properly.

The error.text property will still get populated in case the parsing fails.

@SharkFourSix
Copy link
Author

Cool. That was quick too. 👍

@elbywan elbywan closed this as completed Feb 26, 2023
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