-
Notifications
You must be signed in to change notification settings - Fork 140
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
[REPLAY] Avoid casting & add proper check instead of relying on try/catch #2016
Conversation
Not sure to get the extra value of removing this case from the try catch since we don't do anything more with it 🤔 |
To me handle a case with a if is more graceful than using a try/catch but it should not change much. Maybe it's simply a personal preference |
oh ok, not super fan of using try/catch either when we can avoid it. |
I can remove the try catch as well and wrap the function into a monitor if something breaks we will know and the error would be contain. WDYT? |
I don't have a lot of context on this specific code (@BenoitZugmeyer could know more), so I'd probably start by trying to understand a bit more what we were trying to achieve with this try/catch in the first place, if it is our implementation or if it comes from rrweb and depending on that see if we have a better strategy to than this try/catch. Stepping a bit back, I would wonder if it is a fight worth picking and if it worth picking it now 🙂 |
function _getCssRulesString(cssStyleSheet: CSSStyleSheet | undefined | null): string | null { | ||
if (!cssStyleSheet) { | ||
return null | ||
} catch (error) { | ||
return null | ||
} | ||
try { | ||
const rules = cssStyleSheet.rules || cssStyleSheet.cssRules | ||
if (!rules) { | ||
return null | ||
} | ||
const styleSheetCssText = Array.from(rules, getCssRuleString).join('') | ||
return switchToAbsoluteUrl(styleSheetCssText, cssStyleSheet.href) | ||
} catch (err) { | ||
if (err instanceof DOMException && err.name === 'SecurityError') { | ||
// if css is protected by CORS we cannot access cssRules see: https://www.w3.org/TR/cssom-1/#the-cssstylesheet-interface | ||
return null | ||
} | ||
throw err | ||
} | ||
} |
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.
💬 suggestion: I would reduce the try/catch
scope to be minimal, and remove the monitor()
above
function _getCssRulesString(cssStyleSheet: CSSStyleSheet | undefined | null): string | null {
if (!cssStyleSheet) {
return null
}
let rules
try {
rules = cssStyleSheet.rules || cssStyleSheet.cssRules
} catch {
// if css is protected by CORS we cannot access cssRules see: https://www.w3.org/TR/cssom-1/#the-cssstylesheet-interface
}
if (!rules) {
return null
}
const styleSheetCssText = Array.from(rules, getCssRuleString).join('')
return switchToAbsoluteUrl(styleSheetCssText, cssStyleSheet.href)
}
Or, if we want to keep a trace of unexpected exceptions:
function _getCssRulesString(cssStyleSheet: CSSStyleSheet | undefined | null): string | null {
if (!cssStyleSheet) {
return null
}
let rules
try {
rules = cssStyleSheet.rules || cssStyleSheet.cssRules
} catch (error) {
// if css is protected by CORS we cannot access cssRules see: https://www.w3.org/TR/cssom-1/#the-cssstylesheet-interface
if (!(error instanceof DOMException) || error.name !== 'SecurityError') {
addTelemetryError(error)
}
}
if (!rules) {
return null
}
const styleSheetCssText = Array.from(rules, getCssRuleString).join('')
return switchToAbsoluteUrl(styleSheetCssText, cssStyleSheet.href)
}
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.
@bcaudan what do you think? Is it worth logging unexpected exceptions here to be aware of things that are failing?
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.
Is it worth logging unexpected exceptions here to be aware of things that are failing?
ultimately, if accessing those rules
or cssRules
raises an exception, we would not be able to do much.
It could be interesting to see if there are other cases than the ones identified (for curiosity sake) but eventually we should probably just leave the catch clause empty.
Old browser do not have a constructor for CssStyleSheet
Motivation
Avoid relying on try catch when the object is undefined
Changes
Testing
I have gone over the contributing documentation.