Customize the 404 error messagev1.3+
Configure the plugin to transform 404 responses and add a custom error message. See Transform a 404 response message for a full how-to guide with this example.
Prerequisites
-
untrusted_lua
must be set to eitheron
orsandbox
in yourkong.conf
file for this plugin to work.
Add this section to your kong.yaml
configuration file:
kong.yaml
Copied!
_format_version: "3.0"
plugins:
- name: exit-transformer
config:
handle_unknown: true
functions:
- return function(status, body, headers) if status == 404 then local new_body
= { error = true, status = status, message = "This is not the Route you are
looking for", } return status, new_body, headers else return status, body, headers
end end
Make the following request:
curl -i -X POST http://localhost:8001/plugins/ \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
--data '
{
"name": "exit-transformer",
"config": {
"handle_unknown": true,
"functions": [
"return function(status, body, headers) if status == 404 then local new_body = { error = true, status = status, message = \"This is not the Route you are looking for\", } return status, new_body, headers else return status, body, headers end end"
]
}
}
'
Copied!
Make the following request:
curl -X POST https://{region}.api.konghq.com/v2/control-planes/{controlPlaneId}/core-entities/plugins/ \
--header "accept: application/json" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $KONNECT_TOKEN" \
--data '
{
"name": "exit-transformer",
"config": {
"handle_unknown": true,
"functions": [
"return function(status, body, headers) if status == 404 then local new_body = { error = true, status = status, message = \"This is not the Route you are looking for\", } return status, new_body, headers else return status, body, headers end end"
]
}
}
'
Copied!
Make sure to replace the following placeholders with your own values:
-
region
: Geographic region where your Kong Konnect is hosted and operates. -
controlPlaneId
: Theid
of the control plane. -
KONNECT_TOKEN
: Your Personal Access Token (PAT) associated with your Konnect account.
See the Konnect API reference to learn about region-specific URLs and personal access tokens.
echo "
apiVersion: configuration.konghq.com/v1
kind: KongClusterPlugin
metadata:
name: exit-transformer
namespace: kong
annotations:
kubernetes.io/ingress.class: kong
labels:
global: 'true'
config:
handle_unknown: true
functions:
- return function(status, body, headers) if status == 404 then local new_body =
{ error = true, status = status, message = 'This is not the Route you are looking
for', } return status, new_body, headers else return status, body, headers end
end
plugin: exit-transformer
" | kubectl apply -f -
Copied!
Prerequisite: Configure your Personal Access Token
terraform {
required_providers {
konnect = {
source = "kong/konnect"
}
}
}
provider "konnect" {
personal_access_token = "$KONNECT_TOKEN"
server_url = "https://us.api.konghq.com/"
}
Copied!
Add the following to your Terraform configuration to create a Konnect Gateway Plugin:
resource "konnect_gateway_plugin_exit_transformer" "my_exit_transformer" {
enabled = true
config = {
handle_unknown = true
functions = ["return function(status, body, headers) if status == 404 then local new_body = { error = true, status = status, message = \"This is not the Route you are looking for\", } return status, new_body, headers else return status, body, headers end end"]
}
control_plane_id = konnect_gateway_control_plane.my_konnect_cp.id
}
Copied!