This is an automated email from the ASF dual-hosted git repository.

LinkinStars pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/answer-plugins.git

commit fcb18405a3c2718586a20ba4cf386b6f73933b81
Author: hgaol <[email protected]>
AuthorDate: Wed Jul 8 14:06:02 2026 +0000

    fix(weaviate): only retry without auth on 401/403 errors
    
    ensureClass failures triggered an anonymous-access retry for any error
    when an API key was set, which could silently downgrade to anonymous
    access on transient/network/schema errors. Gate the retry on an actual
    authentication error (WeaviateClientError with a 401 or 403 status code).
---
 vector-search-weaviate/weaviate.go | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/vector-search-weaviate/weaviate.go 
b/vector-search-weaviate/weaviate.go
index 342a5ce..b45c9bd 100644
--- a/vector-search-weaviate/weaviate.go
+++ b/vector-search-weaviate/weaviate.go
@@ -24,7 +24,9 @@ import (
        "crypto/sha1"
        "embed"
        "encoding/json"
+       "errors"
        "fmt"
+       "net/http"
        "strconv"
        "strings"
        "sync"
@@ -35,6 +37,7 @@ import (
        "github.com/segmentfault/pacman/log"
        "github.com/weaviate/weaviate-go-client/v4/weaviate"
        "github.com/weaviate/weaviate-go-client/v4/weaviate/auth"
+       "github.com/weaviate/weaviate-go-client/v4/weaviate/fault"
        "github.com/weaviate/weaviate-go-client/v4/weaviate/graphql"
        "github.com/weaviate/weaviate/entities/models"
 )
@@ -439,10 +442,11 @@ func (e *VectorSearchEngine) ConfigReceiver(config 
[]byte) error {
        e.client = client
 
        // Create/ensure the class with vector configuration.
-       // If it fails with auth error and API key was set, retry without auth 
(anonymous access).
+       // If it fails with an auth error (401/403) and an API key was set, 
retry
+       // without auth in case the server is running in anonymous-access mode.
        if err := e.ensureClass(context.Background()); err != nil {
-               if conf.APIKey != "" {
-                       log.Debugf("weaviate: ensureClass failed with API key 
auth, retrying without auth (anonymous access)")
+               if conf.APIKey != "" && isAuthError(err) {
+                       log.Debugf("weaviate: ensureClass failed with auth 
error, retrying without auth (anonymous access)")
                        cfg.AuthConfig = nil
                        client, err = weaviate.NewClient(cfg)
                        if err != nil {
@@ -460,6 +464,17 @@ func (e *VectorSearchEngine) ConfigReceiver(config []byte) 
error {
        return nil
 }
 
+// isAuthError reports whether err is a Weaviate client error carrying a 401 or
+// 403 status code, i.e. an authentication/authorization failure.
+func isAuthError(err error) bool {
+       var clientErr *fault.WeaviateClientError
+       if errors.As(err, &clientErr) {
+               return clientErr.StatusCode == http.StatusUnauthorized ||
+                       clientErr.StatusCode == http.StatusForbidden
+       }
+       return false
+}
+
 // ensureClass creates the Weaviate class if it doesn't exist.
 func (e *VectorSearchEngine) ensureClass(ctx context.Context) error {
        log.Debugf("weaviate: checking if class %s exists", className)

Reply via email to