Not Raising on Server Side errors
Opened this issue · 1 comments
Hello,
I've been battling with an issue where the gem won't raise on a 500+ error.
Right now the gem raises on Timeout and Connection Failed I believe but If the response is a 500 or a 404 or 401 it automatically fallsback to an empty array which is not what I would expect since I'm not using the with_fallback option.
What is the best way to handle this?
Hello @djpate, great question!
Out of the box, Spyke makes no assumptions about what to do with different status codes and only cares about the content of the hash passed in via Faraday:
{ data: { id: 1, name: 'Bob' }, metadata: {}, errors: {} }
This will raise a Spyke::ResourceNotFound
if the hash doesn't contain an a primary key entry when finding a single record:
Line 30 in 9d256d4
...or an empty array if trying to find multiple and not seeing any:
Lines 36 to 38 in 9d256d4
You can tweak your Faraday middleware to define custom behavior around specific http status codes.
For example something like this:
class Middleware::ErrorHandler < Faraday::Middleware
def call(env)
@app.call(env).on_complete do
raise Spyke::ConnectionError, env[:body] unless env.success?
end
end
end
Hope that helps! Otherwise feel free to ask again 👍