Repository navigation
Expand file tree
/
Copy patherrors.js
More file actions
200 lines (178 loc) · 5.41 KB
/
Copy patherrors.js
File metadata and controls
200 lines (178 loc) · 5.41 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* Custom error classes for the Feedback Application
* Provides structured error handling with error codes and user-friendly messages
*/
import { CONFIG } from './config.js';
/**
* Base error class for all application errors
*/
export class AppError extends Error {
constructor(message, code, statusCode = 500, isOperational = true) {
super(message);
this.name = this.constructor.name;
this.code = code;
this.statusCode = statusCode;
this.isOperational = isOperational;
this.timestamp = new Date().toISOString();
Error.captureStackTrace(this, this.constructor);
}
toJSON() {
return {
error: {
name: this.name,
message: this.message,
code: this.code,
statusCode: this.statusCode,
timestamp: this.timestamp
}
};
}
}
/**
* Error for feedback submission failures
*/
export class FeedbackError extends AppError {
constructor(message, code = CONFIG.ERROR_CODES.SERVER_ERROR, statusCode = 500) {
super(message, code, statusCode);
}
}
/**
* Error for invalid event codes
*/
export class EventError extends AppError {
constructor(message, code = CONFIG.ERROR_CODES.EVENT_NOT_FOUND, statusCode = 404) {
super(message, code, statusCode);
}
}
/**
* Error for network failures
*/
export class NetworkError extends AppError {
constructor(message = 'Unable to connect to server. Check your internet connection.') {
super(message, CONFIG.ERROR_CODES.NETWORK_ERROR, 0);
}
}
/**
* Error for rate limiting
*/
export class RateLimitError extends AppError {
constructor(message = 'Too many requests. Please try again later.', retryAfter = null) {
super(message, CONFIG.ERROR_CODES.RATE_LIMIT_EXCEEDED, 429);
this.retryAfter = retryAfter;
}
}
/**
* Error for authentication failures
*/
export class AuthenticationError extends AppError {
constructor(message = 'Authentication failed. Please check your credentials.') {
super(message, CONFIG.ERROR_CODES.AUTHENTICATION_FAILED, 401);
}
}
/**
* Error for validation failures
*/
export class ValidationError extends AppError {
constructor(message, field = null) {
super(message, CONFIG.ERROR_CODES.INVALID_DATA, 400);
this.field = field;
}
}
/**
* Parse HTTP response and throw appropriate error
*/
export async function parseErrorResponse(response) {
let errorData = {};
try {
errorData = await response.json();
} catch (e) {
// Response doesn't have JSON body
}
const message = errorData.message || errorData.error?.message || 'An error occurred';
switch (response.status) {
case 400:
throw new ValidationError(message);
case 401:
throw new AuthenticationError(message);
case 404:
throw new EventError(message, CONFIG.ERROR_CODES.EVENT_NOT_FOUND, 404);
case 429:
const retryAfter = response.headers.get('Retry-After');
throw new RateLimitError(message, retryAfter);
case 500:
case 502:
case 503:
case 504:
throw new FeedbackError(
'Server error. Please try again later.',
CONFIG.ERROR_CODES.SERVER_ERROR,
response.status
);
default:
throw new AppError(message, 'UNKNOWN_ERROR', response.status);
}
}
/**
* Get user-friendly error message
*/
export function getUserFriendlyErrorMessage(error) {
if (error instanceof NetworkError) {
return {
title: 'Connection Error',
message: 'Unable to connect to the server. Please check your internet connection and try again.',
canRetry: true
};
}
if (error instanceof RateLimitError) {
const retryTime = error.retryAfter ? ` Please try again in ${error.retryAfter} seconds.` : ' Please try again later.';
return {
title: 'Too Many Requests',
message: 'You have made too many requests.' + retryTime,
canRetry: false
};
}
if (error instanceof EventError) {
return {
title: 'Event Not Found',
message: 'The feedback link appears to be invalid or expired. Please contact the event organizer.',
canRetry: false
};
}
if (error instanceof AuthenticationError) {
return {
title: 'Authentication Failed',
message: 'Invalid username or password. Please try again.',
canRetry: true
};
}
if (error instanceof ValidationError) {
return {
title: 'Invalid Data',
message: error.message,
canRetry: true
};
}
// Generic error
return {
title: 'Error',
message: error.message || 'An unexpected error occurred. Please try again.',
canRetry: true
};
}
/**
* Log error to console and telemetry
*/
export function logError(error, context = {}) {
console.error('Error:', {
name: error.name,
message: error.message,
code: error.code,
statusCode: error.statusCode,
stack: error.stack,
context
});
// Integration point for Application Insights or other logging
if (CONFIG.FEATURES.ENABLE_TELEMETRY && window.telemetry) {
window.telemetry.trackError(error, context);
}
}