后端: 添加统一错误处理中间件
This commit is contained in:
parent
cc24894cf8
commit
6fa87784a9
@ -4,6 +4,7 @@ const authRoutes = require('./routes/auth');
|
|||||||
const fileRoutes = require('./routes/files');
|
const fileRoutes = require('./routes/files');
|
||||||
const shareRoutes = require('./routes/share');
|
const shareRoutes = require('./routes/share');
|
||||||
const syncRoutes = require('./routes/sync');
|
const syncRoutes = require('./routes/sync');
|
||||||
|
const errorHandler = require('./middleware/errorHandler');
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const PORT = process.env.PORT || 3000;
|
const PORT = process.env.PORT || 3000;
|
||||||
@ -17,6 +18,9 @@ app.use('/api/files', fileRoutes);
|
|||||||
app.use('/api/share', shareRoutes);
|
app.use('/api/share', shareRoutes);
|
||||||
app.use('/api/sync', syncRoutes);
|
app.use('/api/sync', syncRoutes);
|
||||||
|
|
||||||
|
// Error handler
|
||||||
|
app.use(errorHandler);
|
||||||
|
|
||||||
// Health check
|
// Health check
|
||||||
app.get('/api/health', (req, res) => {
|
app.get('/api/health', (req, res) => {
|
||||||
res.json({ status: 'ok' });
|
res.json({ status: 'ok' });
|
||||||
|
|||||||
57
backend/src/middleware/errorHandler.js
Normal file
57
backend/src/middleware/errorHandler.js
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
// 统一错误处理中间件
|
||||||
|
const errorHandler = (err, req, res, next) => {
|
||||||
|
console.error('Error:', err);
|
||||||
|
|
||||||
|
// JWT 错误
|
||||||
|
if (err.name === 'JsonWebTokenError') {
|
||||||
|
return res.status(401).json({
|
||||||
|
success: false,
|
||||||
|
error: 'Invalid token',
|
||||||
|
code: 'INVALID_TOKEN'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (err.name === 'TokenExpiredError') {
|
||||||
|
return res.status(401).json({
|
||||||
|
success: false,
|
||||||
|
error: 'Token expired',
|
||||||
|
code: 'TOKEN_EXPIRED'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Multer 文件上传错误
|
||||||
|
if (err.code === 'LIMIT_FILE_SIZE') {
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
error: 'File too large',
|
||||||
|
code: 'FILE_TOO_LARGE'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (err.code === 'LIMIT_FILE_COUNT') {
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
error: 'Too many files',
|
||||||
|
code: 'TOO_MANY_FILES'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 数据库错误
|
||||||
|
if (err.code === 'SQLITE_CONSTRAINT') {
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
error: 'Data constraint violation',
|
||||||
|
code: 'CONSTRAINT_VIOLATION'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 默认错误
|
||||||
|
const statusCode = err.statusCode || 500;
|
||||||
|
res.status(statusCode).json({
|
||||||
|
success: false,
|
||||||
|
error: err.message || 'Internal server error',
|
||||||
|
code: err.code || 'INTERNAL_ERROR'
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = errorHandler;
|
||||||
Loading…
Reference in New Issue
Block a user