Manage the errors
Traefik Hub API Gateway provides pieces of middleware to manage errors:
- It allows retrying the request roundtrip to the backends before considering it in error
- It allows displaying a custom error page.
Retry Requests in Error
The example shows how to configure Traefik Hub API Gateway to send a request 4 times to a backend before returning an error.
- Middleware Retry
- IngressRoute
- Service & Deployment
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: test-retry
spec:
retry:
attempts: 4
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
name: applications-apigateway-headers
namespace: apps
spec:
entryPoints:
- websecure
routes:
- match: Path(`/retry`)
kind: Rule
services:
- name: whoami
port: 80
middlewares:
- name: test-retry
kind: Deployment
apiVersion: apps/v1
metadata:
name: whoami
namespace: apps
spec:
replicas: 1
selector:
matchLabels:
app: whoami
template:
metadata:
labels:
app: whoami
spec:
containers:
- name: whoami
image: traefik/whoami
---
apiVersion: v1
kind: Service
metadata:
name: whoami
namespace: apps
labels:
app: whoami
spec:
type: ClusterIP
ports:
- port: 80
name: whoami
selector:
app: whoami
Display a Custom Error Page
The example shows how to display the page error-404.html
when the backend returns a HTTP Status 404:
- Middleware ErrorPage
- IngressRoute
- ErrorPage Service & Deployment
- Service & Deployment
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: error-page
spec:
errors:
status:
- "404"
- "500-599"
query: '/{status}.html'
service:
name: error-page
port: "http"
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
name: applications-apigateway-headers
namespace: apps
spec:
entryPoints:
- websecure
routes:
- match: Path(`/errors`)
kind: Rule
services:
- name: whoami
port: 80
middlewares:
- name: error-page
Pod that displays error pages
apiVersion: apps/v1
kind: Deployment
metadata:
name: error-page
spec:
replicas: 1
selector:
matchLabels:
app: error-page
template:
metadata:
labels:
app: error-page
spec:
containers:
- name: error-page
image: ghcr.io/tarampampam/error-pages
ports:
- name: http
containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: error-page
spec:
selector:
app: error-page
ports:
- name: http
port: 8080
targetPort: http
kind: Deployment
apiVersion: apps/v1
metadata:
name: whoami
namespace: apps
spec:
replicas: 1
selector:
matchLabels:
app: whoami
template:
metadata:
labels:
app: whoami
spec:
containers:
- name: whoami
image: traefik/whoami
---
apiVersion: v1
kind: Service
metadata:
name: whoami
namespace: apps
labels:
app: whoami
spec:
type: ClusterIP
ports:
- port: 80
name: whoami
selector:
app: whoami
Related Content
- See the Retry middleware options in the dedicated section.
- See the ErrorPages middleware options in the dedicated section.
- See how to modify the requests and the responses in the dedicated section.