面试题答案
一键面试设计思路
- 封装模块调用:对每个模块的同步操作进行封装,在封装函数中使用
try...catch
块捕获错误。 - 错误分类处理:根据模块来源和错误类型,进行不同的处理逻辑。可以通过在捕获错误时判断错误类型以及模块标识来决定处理方式。
- 重试机制:对于需要重试的模块错误,实现一个重试函数,在捕获到特定错误时调用重试函数。
- 错误抛出:对于需要向上层调用者抛出特定格式错误信息的模块错误,构造并抛出新的错误。
关键代码示例
假设项目结构如下,有 moduleA
和 moduleB
两个模块,每个模块都有一个同步函数会抛出错误。
模块 A
// moduleA.js
function syncOperationA() {
// 模拟可能抛出错误的同步操作
if (Math.random() > 0.5) {
throw new Error('Module A specific error');
}
return 'Module A operation success';
}
module.exports = {
syncOperationA
};
模块 B
// moduleB.js
function syncOperationB() {
// 模拟可能抛出错误的同步操作
if (Math.random() > 0.6) {
throw new Error('Module B specific error');
}
return 'Module B operation success';
}
module.exports = {
syncOperationB
};
统一错误处理封装
// errorHandler.js
function retryOperation(func, maxRetries = 3) {
let retries = 0;
while (retries < maxRetries) {
try {
return func();
} catch (error) {
retries++;
if (retries === maxRetries) {
throw error;
}
}
}
}
function handleModuleError(moduleName, error) {
if (moduleName === 'moduleA' && error.message.includes('Module A specific error')) {
try {
return retryOperation(() => require('./moduleA').syncOperationA());
} catch (retryError) {
throw new Error(`Retry failed for module A: ${retryError.message}`);
}
} else if (moduleName === 'moduleB' && error.message.includes('Module B specific error')) {
throw new Error(`Module B error: ${error.message}`);
} else {
throw error;
}
}
function wrapper(moduleName, operation) {
try {
return operation();
} catch (error) {
return handleModuleError(moduleName, error);
}
}
module.exports = {
wrapper
};
使用示例
// main.js
const { wrapper } = require('./errorHandler');
const resultA = wrapper('moduleA', () => require('./moduleA').syncOperationA());
console.log(resultA);
const resultB = wrapper('moduleB', () => require('./moduleB').syncOperationB());
console.log(resultB);
在上述代码中:
retryOperation
函数实现了重试逻辑,最多重试maxRetries
次。handleModuleError
函数根据模块名和错误信息判断错误类型,并进行不同处理。模块 A 的特定错误进行重试,模块 B 的特定错误构造新的错误抛出。wrapper
函数封装了模块操作,捕获错误并交给handleModuleError
处理。- 在
main.js
中,通过wrapper
调用模块的同步操作,实现统一的错误处理。