Implements error handling middleware for Express and FastAPI: centralized handlers, logging, and consistent responses.
Agentic Workflow: Read -> Reason -> Act (api-error-express-middleware)
You are Api Error Express Middleware (backend) — a sub-agent that Reads, Reasons, and Acts via allowed-tools.
1. Read — backend context for api-error-express-middleware
- Domain: Implements error handling middleware for Express and FastAPI: centralized handlers, logging, and consistent responses.
- express-middleware: Add centralized error middleware to Express APIs —
npm install http-errors
- fastapi-handlers: Register exception handlers in FastAPI with consistent bodies —
pip install fastapi
- Check
knowledge and prerequisites: node.js, python, openapi
2. Reason — think for api-error-express-middleware
- For
express-middleware: Add centralized error middleware to Express APIs — decide which checks to run
- For
fastapi-handlers: Register exception handlers in FastAPI with consistent bodies — decide which checks to run
- Evaluate trust/policy:
kdesk trust + kdesk doctor patterns for your inputs
3. Act — execute with api-error-express-middleware tools
- Tools:
Glob, Grep, Read, Bash (see frontmatter tools/allowed-tools)
- Use
safe_path for any write; record evidence (paths, checksums)
- Fingerprint:
api-error-express-middleware:41e8fb8e
API Error (Implementation)
Implements centralized error handling in Node and Python APIs.
When to Use
- Errors returned ad hoc from handlers
- Stack traces leaking in responses
- Adding structured error logging
Real Commands
# Express
npm install http-errors express-async-errors
node -e "const h=require('http-errors');const e=h(422,'bad input');console.log(e.status)"
# FastAPI
python -c "from fastapi import HTTPException;print(HTTPException(status_code=422,detail={'code':'VALIDATION_1001'}))"
# Probe
curl -s http://localhost:3000/api/boom -w '\n%{http_code}'
Middleware Shape
app.use((err, req, res, next) => {
res.status(err.status || 500).json({
code: err.code || 'INTERNAL',
title: err.message,
status: err.status || 500,
instance: req.originalUrl
});
});
Testing
Assert every route returns the same error body shape.
Best Practices
- Log the stack, never expose it
- Map unknown errors to 500 with INTERNAL code
Capabilities
express-middleware
Add centralized error middleware to Express APIs
Parameters:
status (string): HTTP status
code (string): Error code
Commands:
npm install http-errors
node -e "const h=require('http-errors');const e=h(404,'missing');console.log(e.status,e.message)"
npm install express-async-errors
node -e "require('express-async-errors');console.log('async handlers patched')"
curl -s http://localhost:3000/api/boom -w '\n%{http_code}'
Examples:
- node -e "const h=require('http-errors');const e=h(422,'bad input',{code:'VALIDATION_1001'});console.log(e.status,e.expose)"
- curl -s http://localhost:3000/api/boom -w '\n%{http_code}'
- npm install http-errors express-async-errors
fastapi-handlers
Register exception handlers in FastAPI with consistent bodies
Parameters:
status (string): HTTP status code
detail (string): Error detail
Commands:
pip install fastapi
python -c "from fastapi import FastAPI,HTTPException;print(HTTPException(status_code=404,detail='missing'))"
python -c "import uvicorn;print('uvicorn ok')"
curl -s http://localhost:8000/api/missing -w '\n%{http_code}'
python -c "from fastapi.responses import JSONResponse;r=JSONResponse({'detail':'x'},status_code=400);print(r.status_code)"
Examples:
- python -c "from fastapi import HTTPException;print(HTTPException(status_code=422,detail={'code':'VALIDATION_1001'}))"
- curl -s http://localhost:8000/api/missing -w '\n%{http_code}'
- python -c "from fastapi.responses import JSONResponse;print(JSONResponse({'detail':'x'},400).status_code)"
References
1---2name: "api-error-express-middleware"3description: "Implements error handling middleware for Express and FastAPI: centralized handlers, logging, and consistent responses. Use when working with express middleware, fastapi handlers or when the user mentions express middleware, fastapi handlers."4license: "MIT"5compatibility: "Requires node.js, python, openapi, postman. Needs network access."6metadata:7 source: https://skillmd.com/skills/opknock/api-error-express-middleware8 author: Kdesk9 version: 2.0.010 category: backend11allowed-tools: "Glob Grep Read Bash(curl:*) Bash(node:*) Bash(npm:*) Bash(pip:*) Bash(python:*)"12---1314Implements error handling middleware for Express and FastAPI: centralized handlers, logging, and consistent responses.1516## Agentic Workflow: Read -> Reason -> Act (api-error-express-middleware)1718You are **Api Error Express Middleware** (backend) — a sub-agent that **Reads, Reasons, and Acts** via `allowed-tools`.1920### 1. Read — backend context for `api-error-express-middleware`21- Domain: Implements error handling middleware for Express and FastAPI: centralized handlers, logging, and consistent responses.22- **express-middleware**: Add centralized error middleware to Express APIs — `npm install http-errors`23- **fastapi-handlers**: Register exception handlers in FastAPI with consistent bodies — `pip install fastapi`24- Check `knowledge` and `prerequisites: node.js, python, openapi`2526### 2. Reason — think for `api-error-express-middleware`27- For `express-middleware`: Add centralized error middleware to Express APIs — decide which checks to run28- For `fastapi-handlers`: Register exception handlers in FastAPI with consistent bodies — decide which checks to run29- Evaluate trust/policy: `kdesk trust` + `kdesk doctor` patterns for your inputs3031### 3. Act — execute with `api-error-express-middleware` tools32- Tools: `Glob`, `Grep`, `Read`, `Bash` (see frontmatter `tools`/`allowed-tools`)33- Use `safe_path` for any write; record evidence (paths, checksums)34- Fingerprint: `api-error-express-middleware:41e8fb8e`3536# API Error (Implementation)3738Implements centralized error handling in Node and Python APIs.3940## When to Use41- Errors returned ad hoc from handlers42- Stack traces leaking in responses43- Adding structured error logging4445## Real Commands4647```bash48# Express49npm install http-errors express-async-errors50node -e "const h=require('http-errors');const e=h(422,'bad input');console.log(e.status)"5152# FastAPI53python -c "from fastapi import HTTPException;print(HTTPException(status_code=422,detail={'code':'VALIDATION_1001'}))"5455# Probe56curl -s http://localhost:3000/api/boom -w '\n%{http_code}'57```5859## Middleware Shape6061```js62app.use((err, req, res, next) => {63 res.status(err.status || 500).json({64 code: err.code || 'INTERNAL',65 title: err.message,66 status: err.status || 500,67 instance: req.originalUrl68 });69});70```7172## Testing73Assert every route returns the same error body shape.7475## Best Practices76- Log the stack, never expose it77- Map unknown errors to 500 with INTERNAL code7879## Capabilities8081### express-middleware82Add centralized error middleware to Express APIs8384**Parameters:**85- `status` (string): HTTP status86- `code` (string): Error code8788**Commands:**89- `npm install http-errors`90- `node -e "const h=require('http-errors');const e=h(404,'missing');console.log(e.status,e.message)"`91- `npm install express-async-errors`92- `node -e "require('express-async-errors');console.log('async handlers patched')"`93- `curl -s http://localhost:3000/api/boom -w '\n%{http_code}'`9495**Examples:**96- node -e "const h=require('http-errors');const e=h(422,'bad input',{code:'VALIDATION_1001'});console.log(e.status,e.expose)"97- curl -s http://localhost:3000/api/boom -w '\n%{http_code}'98- npm install http-errors express-async-errors99100### fastapi-handlers101Register exception handlers in FastAPI with consistent bodies102103**Parameters:**104- `status` (string): HTTP status code105- `detail` (string): Error detail106107**Commands:**108- `pip install fastapi`109- `python -c "from fastapi import FastAPI,HTTPException;print(HTTPException(status_code=404,detail='missing'))"`110- `python -c "import uvicorn;print('uvicorn ok')"`111- `curl -s http://localhost:8000/api/missing -w '\n%{http_code}'`112- `python -c "from fastapi.responses import JSONResponse;r=JSONResponse({'detail':'x'},status_code=400);print(r.status_code)"`113114**Examples:**115- python -c "from fastapi import HTTPException;print(HTTPException(status_code=422,detail={'code':'VALIDATION_1001'}))"116- curl -s http://localhost:8000/api/missing -w '\n%{http_code}'117- python -c "from fastapi.responses import JSONResponse;print(JSONResponse({'detail':'x'},400).status_code)"118119## References120- [Express Error Handling](https://expressjs.com/en/guide/error-handling.html)121- [FastAPI Errors](https://fastapi.tiangolo.com/tutorial/handling-errors/)