Skip to content

feat(plugin): add error-page plugin#13380

Open
AlinsRan wants to merge 7 commits into
masterfrom
feat/error-page-plugin
Open

feat(plugin): add error-page plugin#13380
AlinsRan wants to merge 7 commits into
masterfrom
feat/error-page-plugin

Conversation

@AlinsRan
Copy link
Copy Markdown
Contributor

Summary

This PR introduces the error-page plugin, which allows customizing the response body and content type for HTTP error responses generated by APISIX itself (e.g., when no route matches or when the upstream is unreachable). Responses from upstream services are not intercepted.

Configuration

The plugin is configured via plugin metadata (global config, no per-route attributes needed):

{
    "enable": true,
    "error_404": {
        "body": "<html><body><h1>404 - Not Found</h1></body></html>",
        "content_type": "text/html"
    },
    "error_500": {
        "body": "{\"code\": 500, \"message\": \"Internal Server Error\"}",
        "content_type": "application/json"
    }
}

Key design points

  • Global on/off: controlled by enable in plugin metadata
  • Flexible status code support: supports any HTTP status code (100–599) via JSON Schema patternProperties, using keys like error_404, error_502, etc.
  • Custom content-type: each status code can have its own body and content_type
  • Upstream responses unaffected: only APISIX/nginx-generated error responses (where upstream_status is not set) are intercepted
  • Graceful fallback: if no custom page is configured for a status code, the default nginx/APISIX error page is used

Changes

  • apisix/plugins/error-page.lua: plugin implementation
  • conf2/config-default.yaml: register the plugin (priority: 450)
  • conf/config.yaml.example: register the plugin (priority: 450)
  • t/plugin/error-page.t: test cases
  • t/admin/plugins.t: add plugin to the expected plugin list
  • docs/en/latest/plugins/error-page.md: English documentation
  • docs/zh/latest/plugins/error-page.md: Chinese documentation
  • docs/en/latest/config.json: add to sidebar under Transformation
  • docs/zh/latest/config.json: add to sidebar under Transformation

The error-page plugin customizes the HTTP error response body and
content type for APISIX-generated error responses. It uses plugin
metadata for global configuration and intercepts error responses
generated by APISIX itself (e.g., no matching route, unreachable
upstream), leaving upstream-generated responses unaffected.

Key features:
- Global on/off switch via `enable` in plugin metadata
- Supports any HTTP status code via patternProperties (100-599)
- Configurable response body and content-type per status code
- Falls back to default nginx/APISIX error page when not configured

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dosubot dosubot Bot added size:XL This PR changes 500-999 lines, ignoring generated files. doc Documentation things enhancement New feature or request plugin labels May 17, 2026
AlinsRan and others added 6 commits May 18, 2026 04:37
- Use response_body_like eval qr// to avoid trailing newline issue
  with lookahead patterns in Test::Nginx response_body_like blocks
- Fix docs/zh/latest/config.json sidebar entry (label is '转换请求')

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The plugin list in apisix/cli/config.lua is the authoritative default
plugin list used at runtime. Adding error-page between datadog and lago
matches the priority ordering.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The enable flag required users to explicitly set enable=true after
configuring error pages, which was redundant and unintuitive. The
plugin now intercepts errors as long as a matching error_XXX entry
with a non-empty body exists in the metadata. To disable, delete
the metadata or remove the plugin from the global rule.

Update tests and docs accordingly.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
error-page requires metadata configuration to be useful, so it should
be opt-in. Keep it in cli/config.lua as a known plugin but comment it
out in config.yaml.example and config-default.yaml.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Remove error-page from the default plugin list in cli/config.lua
so the plugin is not loaded unless the user explicitly adds it to
their conf/config.yaml.

Restore conf2/config-default.yaml - error-page should not appear
there at all (not even commented out).

Remove error-page from t/admin/plugins.t expected list since it is
no longer in the default plugin set.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file does not exist in the upstream apache/apisix repository
and should not have been added.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

doc Documentation things enhancement New feature or request plugin size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant