forked from Coernel82/MMM-CalDAV-Tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror-handler.js
More file actions
161 lines (148 loc) · 4.67 KB
/
error-handler.js
File metadata and controls
161 lines (148 loc) · 4.67 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
/**
* Custom error class for CalDAV-related errors
*/
class CalDAVError extends Error {
/**
* Create a CalDAV error
* @param {string} message - Error message
* @param {string} code - Error code (e.g., 'AUTH_FAILED')
* @param {Object} [details={}] - Additional error details
*/
constructor(message, code, details = {}) {
super(message);
this.name = "CalDAVError";
this.code = code;
this.details = details;
}
}
/**
* Error code definitions with user-friendly messages
*/
const ERROR_CODES = {
AUTH_FAILED: {
message: "WebDAV Authentication Failed",
userMessage:
"Unauthorized - Please check your username and password. Use an app password, not your regular password!",
httpStatus: [401]
},
NOT_FOUND: {
message: "Calendar Not Found",
userMessage:
"Calendar URL not found - Check your calendar URL configuration",
httpStatus: [404]
},
NETWORK_ERROR: {
message: "Network Error",
userMessage:
"Cannot reach CalDAV server - Check your network connection and server URL",
httpStatus: [0, 500, 502, 503, 504]
},
PARSE_ERROR: {
message: "ICS Parse Error",
userMessage:
"Invalid calendar data received from server - The ICS format may be corrupted"
},
CONFIG_ERROR: {
message: "Configuration Error",
userMessage: "Invalid module configuration - Check your config.js"
},
RATE_LIMIT: {
message: "Rate Limit Exceeded",
userMessage: "Too many requests to server - Increase your updateInterval",
httpStatus: [429]
},
UNKNOWN: {
message: "Unknown Error",
userMessage:
"An unexpected error occurred - Check the console logs for details"
}
};
/**
* Map HTTP error to CalDAVError
* @param {Error} error - Original error object
* @returns {CalDAVError} Mapped CalDAV error
*
* @example
* try {
* await fetchData();
* } catch (error) {
* const caldavError = fromHttpError(error);
* console.log(caldavError.code); // 'AUTH_FAILED'
* }
*/
function fromHttpError(error) {
const status = error.status || error.response?.status || 0;
// Find matching error code by HTTP status
for (const [code, info] of Object.entries(ERROR_CODES)) {
if (info.httpStatus?.includes(status)) {
return new CalDAVError(info.message, code, {
originalError: error,
httpStatus: status
});
}
}
// Check for specific error types
if (error.name === "SyntaxError" || error.message?.includes("parse")) {
return new CalDAVError(ERROR_CODES.PARSE_ERROR.message, "PARSE_ERROR", {
originalError: error
});
}
if (error.code === "ENOTFOUND" || error.code === "ETIMEDOUT") {
return new CalDAVError(ERROR_CODES.NETWORK_ERROR.message, "NETWORK_ERROR", {
originalError: error,
errorCode: error.code
});
}
// Unknown error
return new CalDAVError("Unknown Error", "UNKNOWN", {
originalError: error
});
}
/**
* Handle error and send to frontend
* @param {Error|CalDAVError} error - Error to handle
* @param {string} moduleId - Module identifier
* @param {Function} sendErrorFn - Function to send error to frontend
*
* @example
* try {
* await getData();
* } catch (error) {
* handleError(error, moduleId, this.sendError.bind(this));
* }
*/
function handleError(error, moduleId, sendErrorFn) {
const caldavError =
error instanceof CalDAVError ? error : fromHttpError(error);
// Log to console with full details
console.error(`[MMM-CalDAV-Tasks] ${caldavError.code}:`, caldavError.message);
if (caldavError.details.originalError) {
console.error(
"[MMM-CalDAV-Tasks] Original error:",
caldavError.details.originalError
);
}
// Send user-friendly message to frontend
const userMessage =
ERROR_CODES[caldavError.code]?.userMessage || caldavError.message;
sendErrorFn(moduleId, `[MMM-CalDAV-Tasks] ${userMessage}`);
}
/**
* Create a CalDAVError from a validation error
* @param {Array<Object>} validationErrors - Validation errors from config-validator
* @returns {CalDAVError} Configuration error
*/
function fromValidationErrors(validationErrors) {
const errorMessages = validationErrors.map((e) => e.message).join("; ");
return new CalDAVError(ERROR_CODES.CONFIG_ERROR.message, "CONFIG_ERROR", {
validationErrors,
message: errorMessages
});
}
module.exports = {
CalDAVError,
ERROR_CODES,
fromHttpError,
handleError,
fromValidationErrors
};