-
Notifications
You must be signed in to change notification settings - Fork 311
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
Add a Variant of asJson that always throws #1771
Comments
the workaround currently is to not use import sttp.client4.quick.*
def allNotes: ujson.Value =
val resp = quickRequest
.get(uri"http://localhost:8080/api/notes/all")
.send()
ujson.read(resp.body)
allNotes.arr.foreach(println) |
or you can do: import sttp.client4.quick.*
def allNotes: ujson.Value =
val resp = quickRequest
.get(uri"http://localhost:8080/api/notes/all")
.mapResponse(ujson.read(_))
.send()
.body
allNotes.arr.foreach(println) |
I think Not saying that the defaults shouldn't change, this might be the case :) |
Thanks, I didn't know about |
@adamw what do you think about adding another variant of |
@bishabosha Not sure I understand that :) Can you give an example? In general, I think always getting a |
I think my core frustration is just wanting a simple way to ignore errors for a beginner (where the only imports come from stdlib + Scala Toolkit) - so they do not need to consider The suggestion to use At the end of the day maybe this is actually a goal we shouldn't aim for and its better to actually show explicit error handling to a beginner (im unsure!). |
Another proposal - an extension method to extension [E, T](t: ResponseAs[Either[DeserializationException[E], T]])
def simple: ResponseAs[T] = t.map(_.fold(throw _, identity)) import sttp.client4.quick.*
def allNotes: ujson.Value =
val resp = quickRequest
.get(uri"http://localhost:8080/api/notes/all")
.response(asJsonAlways[ujson.Value].simple)
.send()
.body
allNotes.arr.foreach(println) |
@bishabosha wouldn't this be the same as
|
No because the |
So what would happen then if the response is a 404 or a 500 (which probably wouldn't bo formatted as a |
If that is the most likely case, then again I guess to have a consistent error model for e.g. if you were making some |
This seems as a quite specialised case, if you want to have specific JSON parsing for some status code, but not for others, I think you should simply do The default requests in sttp don't throw, they return an But coming back to the original issue, I did consider adding a |
I think then for consistency in the Scala Toolkit tutorials we should then recommend handling Either explicitly for deserialising JSON, and also to use |
Well we could have a |
version affected
4.0.0-M1
Describe the issue
To me it seems strange that
asJsonAlways
merges success and error bodies for http errors, but still wraps the body inEither
in case of deserialisation problems, which still requires the user to explicitly handle deserialisation errors.In the spirit of
import sttp.client4.quick.*
it seems it would make sense to have a variant ofasJson
that extendsasJsonAlways
but also throws in the case of deserialisation errors, so that for scripting, assuming a happy path has no friction from handling errors.e.g. imagine a script like
The text was updated successfully, but these errors were encountered: