-
Notifications
You must be signed in to change notification settings - Fork 83
[FSSDK-11035] add logger factory and tests #985
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
Merged
Merged
Changes from 4 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { describe, it, expect, vi } from 'vitest'; | ||
|
||
import { DefaultErrorNotifier } from './error_notifier'; | ||
import { OptimizelyError } from './optimizly_error'; | ||
|
||
const mockMessageResolver = (prefix = '') => { | ||
return { | ||
resolve: vi.fn().mockImplementation((message) => `${prefix} ${message}`), | ||
}; | ||
} | ||
|
||
describe('DefaultErrorNotifier', () => { | ||
it('should call the error handler with the error if the error is not an OptimizelyError', () => { | ||
const errorHandler = { handleError: vi.fn() }; | ||
const messageResolver = mockMessageResolver(); | ||
const errorNotifier = new DefaultErrorNotifier(errorHandler, messageResolver); | ||
|
||
const error = new Error('error'); | ||
errorNotifier.notify(error); | ||
|
||
expect(errorHandler.handleError).toHaveBeenCalledWith(error); | ||
}); | ||
|
||
it.only('should resolve the message of an OptimizelyError before calling the error handler', () => { | ||
raju-opti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const errorHandler = { handleError: vi.fn() }; | ||
const messageResolver = mockMessageResolver('err'); | ||
const errorNotifier = new DefaultErrorNotifier(errorHandler, messageResolver); | ||
|
||
const error = new OptimizelyError('test %s', 'one'); | ||
errorNotifier.notify(error); | ||
|
||
expect(errorHandler.handleError).toHaveBeenCalledWith(error); | ||
expect(error.message).toBe('err test one'); | ||
}); | ||
}); |
raju-opti marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { errorResolver } from "../message/message_resolver"; | ||
import { ErrorHandler } from "./error_handler"; | ||
import { DefaultErrorNotifier } from "./error_notifier"; | ||
|
||
const errorNotifierSymbol = Symbol(); | ||
|
||
export type OpaqueErrorNotifier = { | ||
[errorNotifierSymbol]: unknown; | ||
}; | ||
|
||
export const createErrorNotifier = (errorHandler: ErrorHandler): OpaqueErrorNotifier => { | ||
return { | ||
[errorNotifierSymbol]: new DefaultErrorNotifier(errorHandler, errorResolver), | ||
} | ||
} | ||
|
||
export const extractErrorNotifier = (errorNotifier: OpaqueErrorNotifier): DefaultErrorNotifier => { | ||
return errorNotifier[errorNotifierSymbol] as DefaultErrorNotifier; | ||
} |
raju-opti marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { describe, it, expect, vi } from 'vitest'; | ||
|
||
import { ErrorReporter } from './error_reporter'; | ||
|
||
import { OptimizelyError } from './optimizly_error'; | ||
|
||
const mockMessageResolver = (prefix = '') => { | ||
return { | ||
resolve: vi.fn().mockImplementation((message) => `${prefix} ${message}`), | ||
}; | ||
} | ||
|
||
describe('ErrorReporter', () => { | ||
it('should call the logger and errorNotifier with the first argument if it is an Error object', () => { | ||
const logger = { error: vi.fn() }; | ||
const errorNotifier = { notify: vi.fn() }; | ||
const errorReporter = new ErrorReporter(logger as any, errorNotifier as any); | ||
|
||
const error = new Error('error'); | ||
errorReporter.report(error); | ||
|
||
expect(logger.error).toHaveBeenCalledWith(error); | ||
expect(errorNotifier.notify).toHaveBeenCalledWith(error); | ||
}); | ||
|
||
it('should create an OptimizelyError and call the logger and errorNotifier with it if the first argument is a string', () => { | ||
const logger = { error: vi.fn() }; | ||
const errorNotifier = { notify: vi.fn() }; | ||
const errorReporter = new ErrorReporter(logger as any, errorNotifier as any); | ||
|
||
errorReporter.report('message', 1, 2); | ||
|
||
expect(logger.error).toHaveBeenCalled(); | ||
const loggedError = logger.error.mock.calls[0][0]; | ||
expect(loggedError).toBeInstanceOf(OptimizelyError); | ||
expect(loggedError.baseMessage).toBe('message'); | ||
expect(loggedError.params).toEqual([1, 2]); | ||
|
||
expect(errorNotifier.notify).toHaveBeenCalled(); | ||
const notifiedError = errorNotifier.notify.mock.calls[0][0]; | ||
expect(notifiedError).toBeInstanceOf(OptimizelyError); | ||
expect(notifiedError.baseMessage).toBe('message'); | ||
expect(notifiedError.params).toEqual([1, 2]); | ||
}); | ||
}); |
raju-opti marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1 @@ | ||
/** | ||
* Copyright 2021-2022, 2024, Optimizely | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import configValidator from './utils/config_validator'; | ||
import defaultErrorHandler from './plugins/error_handler'; | ||
import * as enums from './utils/enums'; | ||
import Optimizely from './optimizely'; | ||
import { createNotificationCenter } from './notification_center'; | ||
import { OptimizelyDecideOption, Client, Config } from './shared_types'; | ||
import * as commonExports from './common_exports'; | ||
|
||
/** | ||
* Creates an instance of the Optimizely class | ||
* @param {ConfigLite} config | ||
* @return {Client|null} the Optimizely client object | ||
* null on error | ||
*/ | ||
const createInstance = function(config: Config): Client | null { | ||
try { | ||
configValidator.validate(config); | ||
|
||
const optimizelyOptions = { | ||
clientEngine: enums.JAVASCRIPT_CLIENT_ENGINE, | ||
...config, | ||
}; | ||
|
||
const optimizely = new Optimizely(optimizelyOptions); | ||
return optimizely; | ||
} catch (e: any) { | ||
config.logger?.error(e); | ||
return null; | ||
} | ||
}; | ||
|
||
export { | ||
defaultErrorHandler as errorHandler, | ||
enums, | ||
createInstance, | ||
OptimizelyDecideOption, | ||
}; | ||
|
||
export * from './common_exports'; | ||
|
||
export default { | ||
...commonExports, | ||
errorHandler: defaultErrorHandler, | ||
enums, | ||
createInstance, | ||
OptimizelyDecideOption, | ||
}; | ||
|
||
export * from './export_types' | ||
const msg = 'not used'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.