Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add customizable log level for 'username already exists' error #9336

Open
wants to merge 23 commits into
base: alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c3e9106
feat: Add customizable log level for 'username already exists' error
KrishDave1 Oct 10, 2024
a54e2c6
feat: Made the suggested changes
KrishDave1 Oct 11, 2024
ae26647
fix: Added 2 log levels for Testing and followed new ParseServerConfi…
KrishDave1 Oct 12, 2024
8db9580
Merge branch 'alpha' into feat-branch
mtrezza Oct 12, 2024
da1785c
fix: Added a loop in test to remove redundant code
KrishDave1 Oct 12, 2024
1bedc3c
Merge branch 'feat-branch' of https://github.com/KrishDave1/parse-ser…
KrishDave1 Oct 12, 2024
63b4f9b
feat: Added the both the tests inside one testcase
KrishDave1 Oct 12, 2024
d582e56
Simplified the code further
KrishDave1 Oct 12, 2024
6c21038
refactor
mtrezza Oct 13, 2024
a557e78
consider default level
mtrezza Oct 13, 2024
0a5aa42
fix typo
mtrezza Oct 13, 2024
f537c2c
add warnjs
mtrezza Oct 13, 2024
8e4ac7c
Merge branch 'alpha' into feat-branch
mtrezza Oct 13, 2024
5ec7c82
Removed the extra logger from RestWrite.js
KrishDave1 Oct 15, 2024
a14f5f2
Merge branch 'alpha' into feat-branch
mtrezza Oct 15, 2024
386d2c1
fix: Usernames will always be unique for testing
KrishDave1 Oct 16, 2024
f028be7
Merge branch 'feat-branch' of https://github.com/KrishDave1/parse-ser…
KrishDave1 Oct 16, 2024
dff8140
Merge branch 'alpha' into feat-branch
mtrezza Oct 16, 2024
3587fb8
un-fdescribe
mtrezza Oct 16, 2024
9f3c82a
fit
mtrezza Oct 16, 2024
f1bc586
refactor
mtrezza Oct 16, 2024
2a03838
Check again for info
KrishDave1 Oct 16, 2024
3451ebf
Merge branch 'alpha' into feat-branch
mtrezza Oct 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions spec/ParseUser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4429,4 +4429,32 @@ describe('allowClientClassCreation option', () => {
// Need to set it back to true to avoid other test fails
defaultConfiguration.allowClientClassCreation = true;
});

it('should respect custom log level for username already exists error', async () => {
const loggerController = {
mtrezza marked this conversation as resolved.
Show resolved Hide resolved
error: jasmine.createSpy('error'),
warn: jasmine.createSpy('warn'),
info: jasmine.createSpy('info')
};
await reconfigureServer({
logEvents: {
usernameAlreadyExists: 'warn'
},
loggerController
});
const user = new Parse.User();
user.setUsername('existingUser');
user.setPassword('password');
await user.signUp();
const duplicateUser = new Parse.User();
duplicateUser.setUsername('existingUser');
duplicateUser.setPassword('password');
try {
await duplicateUser.signUp();
} catch (error) {
expect(error.code).toBe(Parse.Error.USERNAME_TAKEN);
expect(loggerController.warn).toHaveBeenCalled();
expect(loggerController.error).not.toHaveBeenCalled();
}
});
});
6 changes: 6 additions & 0 deletions src/Options/docs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/Options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ export interface ParseServerOptions {
verbose: ?boolean;
/* Sets the level for logs */
logLevel: ?string;
/* Options for customizing log levels for specific events
mtrezza marked this conversation as resolved.
Show resolved Hide resolved
:DEFAULT: {} */
logEvents: ?LogEventsOptions;
mtrezza marked this conversation as resolved.
Show resolved Hide resolved
/* (Optional) Overrides the log levels used internally by Parse Server to log events.
:DEFAULT: {} */
logLevels: ?LogLevels;
Expand Down Expand Up @@ -636,3 +639,11 @@ export interface LogLevels {
*/
cloudFunctionError: ?string;
}

export interface LogEventsOptions {
/* Log level for the "username already exists" error when trying to sign up.
Possible values: 'info', 'error', 'verbose', or false to disable logging.
If not specified, the default behavior (logging as an error) will be used.
:DEFAULT: undefined */
usernameAlreadyExists: ?string | boolean;
}
14 changes: 13 additions & 1 deletion src/RestWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -1557,10 +1557,22 @@ RestWrite.prototype.runDatabaseOperation = function () {

// Quick check, if we were able to infer the duplicated field name
if (error && error.userInfo && error.userInfo.duplicated_field === 'username') {
throw new Parse.Error(
//Instead of directly throwing the error, we will give error based on logEvent
mtrezza marked this conversation as resolved.
Show resolved Hide resolved
const usernameError = new Parse.Error(
Parse.Error.USERNAME_TAKEN,
'Account already exists for this username.'
);

// Check for custom log level
const logLevel = this.config.logEvents && this.config.logEvents.usernameAlreadyExists;
if (logLevel) {
// Use custom log level
this.config.loggerController[logLevel](JSON.stringify(usernameError));
} else if (logLevel !== false) {
// Use default error logging if not explicitly disabled
this.config.loggerController.error(JSON.stringify(usernameError));
}
throw usernameError;
mtrezza marked this conversation as resolved.
Show resolved Hide resolved
}

if (error && error.userInfo && error.userInfo.duplicated_field === 'email') {
Expand Down