typeoneerror
(Benjamin Borowski)
14 augustus 2018 om 22:40
1
I was having a bit of a hard time getting “errors” responses out of the discourse_api. I made a quick PR to address this. I’m hoping someone might put some eyeballs on this as a quick sanity check. Is there a way to do what I’m doing without this PR? More details in the PR description.
Thanks!
master ← oki-doki:response-to-errors
merged 01:00AM - 15 Aug 18 UTC
I added the faraday `env` response to `handle_error` error initialization. I was… finding that passing response.env[:body] to the "message" slot of the discourse errors automatically called "to_s" on the response. So if you wanted to use `{ errors: [' Name has already been taken'] }` as a hash, you'd have to additionally parse that string (`"{\"errors\"=>[\"Name has already been taken\"]}"`).
With this change, this is possible:
```ruby
begin
@client.create_group(args)
rescue DiscourseApi::UnprocessableEntity => error
# Name has already been taken
puts error.response.body['errors'].first
end
```
where previously `error.message` would've been `"{\"errors\"=>[\"Name has already been taken\"]}"` which you would've had to parse with `eval` or something similar (see: https://stackoverflow.com/questions/1667630/how-do-i-convert-a-string-object-into-a-hash-object)
Left in the original first param for backwards compatibility.
If there's a different way to accomplish this, I'd love to hear of it. All tests pass but this could probably use some additional eyeballs.
4 likes
blake
(Blake Erickson)
15 augustus 2018 om 18:34
2
Just marking that this was merged in and a new gem version was cut.
4 likes