Spaces:
Running
Running
export const errorHandler = (err, req, res, next) => { | |
console.error('Error:', err); | |
// JWT错误 | |
if (err.name === 'JsonWebTokenError') { | |
return res.status(401).json({ error: 'Invalid token' }); | |
} | |
if (err.name === 'TokenExpiredError') { | |
return res.status(401).json({ error: 'Token expired' }); | |
} | |
// GitHub API错误 | |
if (err.response && err.response.status) { | |
const status = err.response.status; | |
const message = err.response.data?.message || 'GitHub API error'; | |
if (status === 401) { | |
return res.status(500).json({ | |
error: 'GitHub authentication failed', | |
details: 'GitHub token may be invalid or expired', | |
suggestion: 'Check GitHub token configuration' | |
}); | |
} | |
if (status === 403) { | |
return res.status(500).json({ | |
error: 'GitHub access forbidden', | |
details: 'Insufficient permissions or rate limit exceeded', | |
suggestion: 'Check repository permissions or wait for rate limit reset' | |
}); | |
} | |
if (status === 404) { | |
return res.status(404).json({ | |
error: 'Resource not found in GitHub', | |
details: message, | |
suggestion: 'Check if the repository or file exists' | |
}); | |
} | |
if (status === 422) { | |
return res.status(413).json({ | |
error: 'File too large for GitHub', | |
details: message, | |
suggestion: 'Try reducing file size or splitting into smaller files' | |
}); | |
} | |
return res.status(500).json({ | |
error: `GitHub API error: ${message}`, | |
details: `HTTP ${status}: ${message}`, | |
suggestion: 'Check GitHub service status or try again later' | |
}); | |
} | |
// Axios 网络错误 | |
if (err.code === 'ECONNREFUSED' || err.code === 'ENOTFOUND') { | |
return res.status(503).json({ | |
error: 'Service unavailable', | |
details: 'Cannot connect to GitHub API', | |
suggestion: 'Check network connection or GitHub service status' | |
}); | |
} | |
if (err.code === 'ETIMEDOUT') { | |
return res.status(504).json({ | |
error: 'Request timeout', | |
details: 'GitHub API request timed out', | |
suggestion: 'Try again with smaller data or check network connection' | |
}); | |
} | |
// 自定义应用错误 | |
if (err.message.includes('Too many slides failed to save')) { | |
return res.status(500).json({ | |
error: 'Partial save failure', | |
details: err.message, | |
suggestion: 'Some slides could not be saved. Check individual slide content and try again.', | |
partialFailure: true | |
}); | |
} | |
if (err.message.includes('Failed to save PPT')) { | |
return res.status(500).json({ | |
error: 'PPT save failed', | |
details: err.message, | |
suggestion: 'Check PPT content and try saving again. Consider reducing file size if needed.' | |
}); | |
} | |
// 默认错误 | |
const isDevelopment = process.env.NODE_ENV === 'development'; | |
res.status(500).json({ | |
error: isDevelopment ? err.message : 'Internal server error', | |
details: isDevelopment ? err.stack : 'An unexpected error occurred', | |
suggestion: 'If the problem persists, please contact support', | |
timestamp: new Date().toISOString() | |
}); | |
}; |