# `Electric.Plug.ServeShapePlug`
[🔗](https://github.com/electric-sql/electric/tree/%40core/sync-service%401.6.2/packages/sync-service/lib/electric/plug/serve_shape_plug.ex#L1)

Plug pipeline for serving shape requests.

`call/2` is overridden to wrap the whole pipeline in a telemetry span, an
explicit `try`/`catch` that invokes `handle_errors/2` on uncaught errors,
and a `try`/`after` that always releases the admission control permit and
ends the OTEL span.

Error handling is inspired by `Plug.ErrorHandler`'s pattern but adapts to a
subtle limitation: `Plug.Builder` does not wrap individual plugs in
error-capturing frames, so the `conn` visible from the `catch` clause is
the one passed into `call/2` — not the accumulated conn at the point of
raise. This means we cannot rely on a `register_before_send` callback on
the error-handler's conn for admission release. Instead, `check_admission`
stashes the acquired permit in the process dictionary and the `after`
clause in `call/2` releases it — firing for success, halt, and exception
paths alike.

Using `after` (rather than `register_before_send`) is also what makes the
streaming path correct: `before_send` fires when `send_chunked` starts
streaming, not when it finishes, which would end the telemetry span before
chunk reduction completes and lose `duration` + `streaming_bytes_sent`.
The `after` clause runs only once `super(conn, opts)` returns, i.e. after
`Api.Response.send_stream/2` has synchronously drained the body.

# `init`

---

*Consult [api-reference.md](api-reference.md) for complete listing*
