Skip to content

Commit cfa3978

Browse files
committed
Add try/catch
1 parent b69f1cd commit cfa3978

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

packages/ui/src/elements/LeaveWithoutSaving/usePreventLeave.tsx

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,25 @@ export const usePreventLeave = ({
8282
// check when page is about to be changed
8383
useEffect(() => {
8484
function isAnchorOfCurrentUrl(currentUrl: string, newUrl: string) {
85-
const currentUrlObj = new URL(currentUrl)
86-
const newUrlObj = new URL(newUrl)
87-
// Compare hostname, pathname, and search parameters
88-
if (
89-
currentUrlObj.hostname === newUrlObj.hostname &&
90-
currentUrlObj.pathname === newUrlObj.pathname &&
91-
currentUrlObj.search === newUrlObj.search
92-
) {
93-
// Check if the new URL is just an anchor of the current URL page
94-
const currentHash = currentUrlObj.hash
95-
const newHash = newUrlObj.hash
96-
return (
97-
currentHash !== newHash &&
98-
currentUrlObj.href.replace(currentHash, '') === newUrlObj.href.replace(newHash, '')
99-
)
85+
try {
86+
const currentUrlObj = new URL(currentUrl)
87+
const newUrlObj = new URL(newUrl)
88+
// Compare hostname, pathname, and search parameters
89+
if (
90+
currentUrlObj.hostname === newUrlObj.hostname &&
91+
currentUrlObj.pathname === newUrlObj.pathname &&
92+
currentUrlObj.search === newUrlObj.search
93+
) {
94+
// Check if the new URL is just an anchor of the current URL page
95+
const currentHash = currentUrlObj.hash
96+
const newHash = newUrlObj.hash
97+
return (
98+
currentHash !== newHash &&
99+
currentUrlObj.href.replace(currentHash, '') === newUrlObj.href.replace(newHash, '')
100+
)
101+
}
102+
} catch (err) {
103+
console.log('Unexpected exception thrown in LeaveWithoutSaving:isAnchorOfCurrentUrl', err)
100104
}
101105
return false
102106
}
@@ -135,7 +139,7 @@ export const usePreventLeave = ({
135139
}
136140
}
137141
} catch (err) {
138-
console.log('An unexpected exception was thrown in LeaveWithoutSaving:usePreventLeave', err)
142+
console.log('Unexpected exception thrown in LeaveWithoutSaving:usePreventLeave', err)
139143
}
140144
}
141145

0 commit comments

Comments
 (0)