AlinsRan opened a new pull request, #13343:
URL: https://github.com/apache/apisix/pull/13343

   ## Summary
   
   This PR introduces the `exit-transformer` plugin and extends 
`core/response.lua` with an exit callback mechanism to support it.
   
   ## Description
   
   The `exit-transformer` plugin intercepts APISIX exit responses (plugin 
rejections, route-not-found, auth failures, etc.) and transforms them using 
user-defined Lua functions before the response is sent to the client.
   
   **Use cases:**
   - Normalize error response format across all upstream and gateway errors
   - Remap status codes (e.g. translate `401` → `200` for legacy clients)
   - Inject custom headers or bodies into error responses
   - Apply uniform error envelope format across all routes
   
   ## Changes
   
   ### `apisix/core/response.lua`
   
   Two additions to support the callback mechanism:
   
   1. **`exit_insert_callback(func, conf)`** — registers a per-request callback 
in `ngx.ctx`. Each callback has the signature `(code, body, headers, conf) → 
(code, body, headers)`.
   
   2. **`resp_exit` with callback path** — when callbacks are registered, 
`resp_exit` builds a structured `(code, body, headers)` representation from the 
variadic arguments, runs all callbacks in order, then sends the transformed 
response. The original variadic fast path is preserved when no callbacks are 
present, maintaining full backward compatibility.
   
   ### `apisix/plugins/exit-transformer.lua`
   
   New plugin. In the `rewrite` phase, each configured Lua function is 
registered as an exit callback. Functions are cached by an LRU cache to avoid 
repeated `load()` calls.
   
   ## Files changed
   
   - `apisix/core/response.lua` — add `exit_insert_callback` and callback path 
in `resp_exit`
   - `apisix/plugins/exit-transformer.lua` — plugin implementation
   - `t/plugin/exit-transformer.t` — test cases
   - `docs/en/latest/plugins/exit-transformer.md` — English documentation
   - `docs/zh/latest/plugins/exit-transformer.md` — Chinese documentation
   - `apisix/cli/config.lua` — register plugin in default list (priority 
9999999, first to run)
   - `docs/en/latest/config.json` / `docs/zh/latest/config.json` — add to 
sidebar


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to