File size: 3,247 Bytes
b7560a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0afb612
 
 
 
 
 
 
 
 
 
 
 
 
b7560a4
 
 
0afb612
 
 
 
 
 
 
 
 
 
 
 
 
b7560a4
 
0afb612
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7560a4
 
 
0afb612
 
b7560a4
0afb612
 
 
 
b7560a4
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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()
  });
};