重构 更规范性的文件组织
This commit is contained in:
1 parent
59abbf30d9
commit
ea6c87ad25
53 files changed
+3274
-2110
No files matched your search
@@ -0,0 +1,97 @@
|
||||
/**
|
||||
* 统一错误处理中间件
|
||||
*/
|
||||
|
||||
/**
|
||||
* 应用错误类
|
||||
*/
|
||||
class AppError extends Error {
|
||||
constructor(message, statusCode = 500, code = 'INTERNAL_ERROR') {
|
||||
super(message);
|
||||
this.statusCode = statusCode;
|
||||
this.code = code;
|
||||
this.isOperational = true;
|
||||
Error.captureStackTrace(this, this.constructor);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证错误
|
||||
*/
|
||||
class ValidationError extends AppError {
|
||||
constructor(message, field = null) {
|
||||
super(message, 400, 'VALIDATION_ERROR');
|
||||
this.field = field;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 资源未找到错误
|
||||
*/
|
||||
class NotFoundError extends AppError {
|
||||
constructor(resource, id = null) {
|
||||
super(
|
||||
`${resource} not found${id ? `: ${id}` : ''}`,
|
||||
404,
|
||||
'NOT_FOUND'
|
||||
);
|
||||
this.resource = resource;
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 冲突错误
|
||||
*/
|
||||
class ConflictError extends AppError {
|
||||
constructor(message) {
|
||||
super(message, 409, 'CONFLICT');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 错误处理中间件
|
||||
*/
|
||||
const errorHandler = (err, req, res, next) => {
|
||||
// 操作性错误(预期的错误)
|
||||
if (err.isOperational) {
|
||||
return res.status(err.statusCode).json({
|
||||
success: false,
|
||||
error: {
|
||||
code: err.code,
|
||||
message: err.message,
|
||||
...(err.field && { field: err.field })
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 编程错误(未预期的错误)
|
||||
console.error('💥 Unexpected Error:', err);
|
||||
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: {
|
||||
code: 'INTERNAL_ERROR',
|
||||
message: process.env.NODE_ENV === 'development'
|
||||
? err.message
|
||||
: 'Internal server error',
|
||||
...(process.env.NODE_ENV === 'development' && { stack: err.stack })
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* 异步路由处理器包装
|
||||
*/
|
||||
const asyncHandler = (fn) => (req, res, next) => {
|
||||
Promise.resolve(fn(req, res, next)).catch(next);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
AppError,
|
||||
ValidationError,
|
||||
NotFoundError,
|
||||
ConflictError,
|
||||
errorHandler,
|
||||
asyncHandler
|
||||
};
|
||||
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* 请求参数验证中间件
|
||||
*/
|
||||
|
||||
const { ValidationError } = require('./errorHandler');
|
||||
|
||||
/**
|
||||
* 验证必需参数
|
||||
*/
|
||||
function validateRequired(fields) {
|
||||
return (req, res, next) => {
|
||||
const source = req.method === 'GET' ? req.query : req.body;
|
||||
|
||||
for (const field of fields) {
|
||||
if (!source[field]) {
|
||||
return next(new ValidationError(`${field} is required`, field));
|
||||
}
|
||||
}
|
||||
|
||||
next();
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证素材库 ID
|
||||
*/
|
||||
function validateLibraryId(req, res, next) {
|
||||
const libraryId = req.params.id || req.query.libraryId || req.body.libraryId;
|
||||
|
||||
if (!libraryId) {
|
||||
return next(new ValidationError('Library ID is required', 'libraryId'));
|
||||
}
|
||||
|
||||
req.libraryId = libraryId;
|
||||
next();
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证分页参数
|
||||
*/
|
||||
function validatePagination(req, res, next) {
|
||||
const { offset, limit } = req.query;
|
||||
|
||||
if (offset !== undefined) {
|
||||
const offsetNum = parseInt(offset, 10);
|
||||
if (isNaN(offsetNum) || offsetNum < 0) {
|
||||
return next(new ValidationError('Invalid offset', 'offset'));
|
||||
}
|
||||
req.pagination = { offset: offsetNum };
|
||||
}
|
||||
|
||||
if (limit !== undefined) {
|
||||
const limitNum = parseInt(limit, 10);
|
||||
if (isNaN(limitNum) || limitNum < 1 || limitNum > 500) {
|
||||
return next(new ValidationError('Invalid limit (1-500)', 'limit'));
|
||||
}
|
||||
req.pagination = { ...req.pagination, limit: limitNum };
|
||||
}
|
||||
|
||||
next();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
validateRequired,
|
||||
validateLibraryId,
|
||||
validatePagination
|
||||
};
|
||||
Reference in new issue
Block a user