elixir-plug / plug

Compose web applications with functions
https://hex.pm/packages/plug
Other
2.84k stars 582 forks source link

Docs: Improve custom body reader example in `Plug.Parsers` module #1243

Closed tonydangblog closed 2 weeks ago

tonydangblog commented 2 weeks ago

Change example to use with

Change from:

{:ok, body, conn} = Plug.Conn.read_body(conn, opts)

To:

with {:ok, body, conn} <- Plug.Conn.read_body(conn, opts) do

This ensures that if Plug.Conn.read_body/2 fails, the function will return a meaningful error tuple instead of raising a MatchError. I saw this also suggested in this Dashbit article.

Remove extra parentheses

Change from:

conn = update_in(conn.assigns[:raw_body], &[body | (&1 || [])])

To:

conn = update_in(conn.assigns[:raw_body], &[body | &1 || []])

This just removes the extra parens that the Elixir formatter would remove. :)

josevalim commented 2 weeks ago

:green_heart: :blue_heart: :purple_heart: :yellow_heart: :heart: