Copilot commented on code in PR #1373:
URL: https://github.com/apache/dubbo-admin/pull/1373#discussion_r2637022104


##########
ui-vue3/src/base/http/request.ts:
##########
@@ -82,8 +81,26 @@ response.use(
   (error) => {
     NProgress.done()
     // Handle error response with data
-    if (error.response?.data) {
-      const errorMsg = 
`${error.response.data.code}:${error.response.data.message}`
+    const response = error.response
+
+    // Handle 401 unauthorized
+    if (response?.status === 401) {
+      removeAuthState()
+      try {
+        const current = router.currentRoute?.value
+        const redirectPath = current?.fullPath || current?.path || '/'
+
+        if (!redirectPath.startsWith('/login')) {
+          router.push({ path: 
`/login?redirect=${encodeURIComponent(redirectPath)}` })
+        }
+      } catch (e) {
+        if (!window.location.pathname.startsWith('/login')) {
+          window.location.href = 
`/login?redirect=${encodeURIComponent(window.location.pathname)}`
+        }
+      }
+    }

Review Comment:
   After handling the 401 unauthorized error and redirecting to the login page, 
the code continues to execute and may display an error message to the user. 
This creates a poor user experience where an error toast appears briefly before 
or during the redirect. For 401 errors, displaying an error message is not 
helpful since the user is being redirected to login anyway.
   
   Add a return statement after line 101 to prevent the error message logic 
from executing for 401 responses.
   ```suggestion
       }
       if (response?.status === 401) {
         return Promise.reject(error.response?.data)
       }
   ```



##########
ui-vue3/src/base/http/request.ts:
##########
@@ -82,8 +81,26 @@ response.use(
   (error) => {
     NProgress.done()
     // Handle error response with data
-    if (error.response?.data) {
-      const errorMsg = 
`${error.response.data.code}:${error.response.data.message}`
+    const response = error.response
+
+    // Handle 401 unauthorized
+    if (response?.status === 401) {
+      removeAuthState()
+      try {
+        const current = router.currentRoute?.value
+        const redirectPath = current?.fullPath || current?.path || '/'
+
+        if (!redirectPath.startsWith('/login')) {
+          router.push({ path: 
`/login?redirect=${encodeURIComponent(redirectPath)}` })
+        }
+      } catch (e) {
+        if (!window.location.pathname.startsWith('/login')) {
+          window.location.href = 
`/login?redirect=${encodeURIComponent(window.location.pathname)}`
+        }
+      }

Review Comment:
   The catch block silently swallows any errors that occur during the router 
navigation. While there's a fallback using window.location.href, if the 
router.push fails for reasons other than router availability (e.g., navigation 
guards rejecting the navigation), the error is lost and debugging becomes 
difficult.
   
   Consider logging the caught error before falling back to 
window.location.href to aid in debugging navigation issues.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to