Class: AtlasRb::Middleware::RaiseOnStaleResource

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/atlas_rb/middleware/raise_on_stale_resource.rb

Overview

Translates Atlas's structured optimistic-lock conflict response into a typed Ruby exception.

Atlas surfaces an exhausted-retry (or retry-unsafe) optimistic-lock conflict as an HTTP 409 Conflict whose JSON body carries the discriminator error: "stale_resource". This middleware keys on the status + discriminator pair and raises StaleResourceError, carrying the envelope's resource_id and action through so callers' failure logs are useful without the full response.

It is intentionally narrow: any other status, or a 409 without the discriminator, passes through untouched so the caller still sees the response as a Mash (see StaleResourceError for the rationale — atlas_rb stays a thin Faraday binding and translates only the one wire signal Cerberus jobs need to discriminate on).

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ void

This method returns an undefined value.

Parameters:

  • env (Faraday::Env)

    the completed response environment.

Raises:



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/atlas_rb/middleware/raise_on_stale_resource.rb', line 26

def on_complete(env)
  return unless env.status == 409

  body = parse_json(env.body)
  return unless body.is_a?(Hash) && body["error"] == "stale_resource"

  raise AtlasRb::StaleResourceError.new(
    body["message"] || "Atlas reported a stale-resource conflict",
    resource_id: body["resource_id"],
    action: body["action"]
  )
end