Browse Source

fix: handle UnexpectedEof error in ShopRenter proxy TLS connection #55

Claude 5 months ago
parent
commit
0264b42afa
1 changed files with 22 additions and 4 deletions
  1. 22 4
      supabase/functions/shoprenter-proxy/index.ts

+ 22 - 4
supabase/functions/shoprenter-proxy/index.ts

@@ -78,9 +78,20 @@ async function makeHttp11Request(
     let responseData = ''
     let responseData = ''
 
 
     while (true) {
     while (true) {
-      const n = await conn.read(buffer)
-      if (n === null) break
-      responseData += decoder.decode(buffer.subarray(0, n))
+      try {
+        const n = await conn.read(buffer)
+        if (n === null) break
+        responseData += decoder.decode(buffer.subarray(0, n))
+      } catch (e) {
+        // Handle UnexpectedEof during read - connection closed by server
+        // This is expected when server sends Connection: close
+        if (e instanceof Deno.errors.UnexpectedEof ||
+            (e instanceof Error && e.name === 'UnexpectedEof')) {
+          console.log('[HTTP/1.1] Connection closed by server (expected with Connection: close)')
+          break
+        }
+        throw e
+      }
     }
     }
 
 
     console.log('[HTTP/1.1] Received response length:', responseData.length)
     console.log('[HTTP/1.1] Received response length:', responseData.length)
@@ -129,7 +140,14 @@ async function makeHttp11Request(
     try {
     try {
       conn.close()
       conn.close()
     } catch (e) {
     } catch (e) {
-      console.error('[HTTP/1.1] Error closing connection:', e)
+      // Gracefully handle connection closure errors
+      // UnexpectedEof during close means server already closed the connection
+      if (e instanceof Deno.errors.UnexpectedEof ||
+          (e instanceof Error && e.name === 'UnexpectedEof')) {
+        console.log('[HTTP/1.1] Connection already closed by server')
+      } else {
+        console.error('[HTTP/1.1] Error closing connection:', e)
+      }
     }
     }
   }
   }
 }
 }