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.
Add this section to your declarative configuration file:
_format_version:"3.0"plugins:-name:exit-transformerconfig:handle_unknown:truefunctions:-return function(status, body, headers) if status == 404 then local new_body= { error = true, status = status, message = "This is not the Route you arelooking for", } return status, new_body, headers else return status, body, headersend 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"
]
}
}
'
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"
]
}
}
'
Make sure to replace the following placeholders with your own values:
region: Geographic region where your Kong Konnect is hosted and operates.
controlPlaneId: The id 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/v1kind:KongClusterPluginmetadata:name:exit-transformernamespace:kongannotations:kubernetes.io/ingress.class:konglabels:global:'true'config:handle_unknown:truefunctions:-return function(status, body, headers) if status == 404 then local new_body ={ error = true, status = status, message = 'This is not the Route you are lookingfor', } return status, new_body, headers else return status, body, headers endendplugin:exit-transformer"|kubectlapply-f-
Prerequisite: Configure your Personal Access Token
Add the following to your Terraform configuration to create a Konnect Gateway Plugin:
resource"konnect_gateway_plugin_exit_transformer""my_exit_transformer"{enabled=trueconfig={handle_unknown=truefunctions=["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}