Server plugin for KrakenD that allows configuring route-based rules to:
- Enforce required response fields (returning an error if missing)
- Inject default values for absent fields
- Extract and propagate backend errors (
error_*) when required fields are missing
The plugin works as a server middleware.
For configured routes it:
- Intercepts the aggregated JSON response
- If response is not
2xxor not JSON → passes it through untouched - If JSON and successful:
- Injects default fields (if missing)
- Validates required fields
- If required field is missing:
- Looks for
error_*keys in the response - Sorts them
- Returns the first backend error
- If no backend error found → returns
500
- Looks for
{
"plugin/http-server": {
"name": ["onliner/krakend-fallback"],
"onliner/krakend-fallback": {
"routes": [
{
"path": "/products/{product}/positions",
"required": ["product"],
"default": {
"positions": []
}
}
]
}
}
}Path template using {param} syntax.
Example:
/products/{product}/positions
List of top-level JSON keys that must exist in the final aggregated response.
If any key is missing:
- Plugin searches for
error_*fields - If found → returns the first backend error
- If not found → returns
500 Internal Server Error
Map of fields to inject if they are missing.
Defaults are only applied for successful 2xx JSON responses.
Example:
"default": {
"positions": [],
"shops": null
}Backend errors must follow this structure inside the aggregated response:
{
"error_1": {
"http_status_code": 500,
"http_body": "{\"message\":\"backend failed\"}",
"http_body_encoding": "application/json"
}
}http_status_code– HTTP status to returnhttp_body– raw response bodyhttp_body_encoding– Content-Type header (optional, defaults totext/plain)
If multiple error_* keys exist, the plugin:
- Sorts them lexicographically
- Returns the first one
- Plugin modifies only successful
2xxJSON responses - Original headers are preserved when modifying response
Content-Lengthis recalculated when body is modified- Thread-safe and race-tested