how to remove too much map_error ? #2765
Answered
by
lpil
lost22git
asked this question in
Questions & support
-
let assert Ok(r) =
response.body
|> json.decode(dynamic.dict(of: dynamic.string, to: dynamic.dynamic))
|> result.map_error(fn(e) { hackney.Other(dynamic.from(e)) })
|> result.then(fn(it) {
it
|> dict.get("json")
|> result.map_error(fn(e) { hackney.Other(dynamic.from(e)) })
})
|> result.then(fn(it) {
it
|> dynamic.dict(dynamic.string, dynamic.dynamic)
|> result.map_error(fn(e) { hackney.Other(dynamic.from(e)) })
}) |
Beta Was this translation helpful? Give feedback.
Answered by
lpil
Mar 19, 2024
Replies: 2 comments 2 replies
-
are there anyhow ? |
Beta Was this translation helpful? Give feedback.
0 replies
-
You could use one decoder instead of doing it in steps: response.body
|> json.decode(dynamic.field("json", of: dynamic.string)
|> result.map_error(fn(e) { hackney.Other(dynamic.from(e)) }) You could also make your error type more precise by not using Dynamic. This would make it more concise and also easier to work with. It's very rare that you should have a Dynamic from anywhere but user input. response.body
|> json.decode(dynamic.field("json", of: dynamic.string)
|> result.map_error(JsonError) |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
lost22git
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You could use one decoder instead of doing it in steps:
You could also make your error type more precise by not using Dynamic. This would make it more concise and also easier to work with. It's very rare that you should have a Dynamic from anywhere but user input.