-
Notifications
You must be signed in to change notification settings - Fork 127
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
Handled invalid formatter function props being passed #212
base: master
Are you sure you want to change the base?
Handled invalid formatter function props being passed #212
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fix makes sense, but there are some mistakes that need to be fixed.
Thanks!
|
||
try { | ||
// Call the formatter to see if it behaves correctly | ||
const result = formatter(value, unit, suffix, then, nextFormatter, now) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const result = formatter(value, unit, suffix, then, nextFormatter, now) | |
const nextFormatter = defaultFormatter.bind(null, value, unit, suffix) | |
const result = formatter(value, unit, suffix, then, nextFormatter, now) |
|
||
const nextFormatter = (value, unit, suffix) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const nextFormatter = (value, unit, suffix) => { | |
const formatter = (value, unit, suffix) => { |
return ( | ||
<Komponent {...spreadProps} title={passDownTitle}> | ||
{formatter(value, unit, suffix, then, nextFormatter, now)} | ||
{/* {formatter(value, unit, suffix, then, nextFormatter, now)} */} | ||
{nextFormatter(value, unit, suffix)} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{nextFormatter(value, unit, suffix)} | |
{formatter(value, unit, suffix)} |
Problems
When the formatter prop is provided as a value that is not a function (e.g., null, undefined, or a non-callable object), the component raises an exception indicating that "formatter is not a function." This results in a stack trace being logged, which exposes the internal error and disrupts the user experience.
When a valid function is provided to the formatter prop, if that function throws an error during execution, it leads to an unhandled exception that crashes the application. This prevents the TimeAgo component from rendering as intended, impacting the overall functionality and user interface.
Solutions
Implemented a check to determine if the formatter is a function. If it is not, a warning is logged, and the component falls back to using the defaultFormatter. This ensures that the component can still render valid output without crashing.
Wrapped the call to the formatter in a try-catch block to catch any errors that may arise during its execution. If an error is thrown, a warning is logged, and the component gracefully falls back to the defaultFormatter.
Ensured all test cases pass