forked from parse-community/parse-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathError.js
More file actions
46 lines (41 loc) · 1.95 KB
/
Copy pathError.js
File metadata and controls
46 lines (41 loc) · 1.95 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
import defaultLogger from './logger';
/**
* Creates a sanitized error that hides detailed information from clients
* while logging the detailed message server-side.
*
* @param {number} errorCode - The Parse.Error code (e.g., Parse.Error.OPERATION_FORBIDDEN)
* @param {string} detailedMessage - The detailed error message to log server-side
* @param {object} config - Parse Server config with enableSanitizedErrorResponse
* @param {string} [sanitizedMessage='Permission denied'] - The sanitized message to return to clients
* @returns {Parse.Error} A Parse.Error with sanitized message
*/
function createSanitizedError(errorCode, detailedMessage, config, sanitizedMessage = 'Permission denied') {
// On testing we need to add a prefix to the message to allow to find the correct call in the TestUtils.js file
if (process.env.TESTING) {
defaultLogger.error('Sanitized error:', detailedMessage);
} else {
defaultLogger.error(detailedMessage);
}
return new Parse.Error(errorCode, config?.enableSanitizedErrorResponse !== false ? sanitizedMessage : detailedMessage);
}
/**
* Creates a sanitized error from a regular Error object
* Used for non-Parse.Error errors (e.g., Express errors)
*
* @param {number} statusCode - HTTP status code (e.g., 403)
* @param {string} detailedMessage - The detailed error message to log server-side
* @returns {Error} An Error with sanitized message
*/
function createSanitizedHttpError(statusCode, detailedMessage, config) {
// On testing we need to add a prefix to the message to allow to find the correct call in the TestUtils.js file
if (process.env.TESTING) {
defaultLogger.error('Sanitized error:', detailedMessage);
} else {
defaultLogger.error(detailedMessage);
}
const error = new Error();
error.status = statusCode;
error.message = config?.enableSanitizedErrorResponse !== false ? 'Permission denied' : detailedMessage;
return error;
}
export { createSanitizedError, createSanitizedHttpError };