Skip to content

Commit 7d68f1a

Browse files
nleroy917Anush008
andauthored
Improve elastic/open search error handling (#166)
* Improve elastic/open search error handling Signed-off-by: Nathan LeRoy <nleroy917@users.noreply.github.com> * chore: Lint Signed-off-by: Anush008 <anushshetty90@gmail.com> --------- Signed-off-by: Nathan LeRoy <nleroy917@users.noreply.github.com> Signed-off-by: Anush008 <anushshetty90@gmail.com> Co-authored-by: Nathan LeRoy <nleroy917@users.noreply.github.com> Co-authored-by: Anush008 <anushshetty90@gmail.com>
1 parent 51b721e commit 7d68f1a

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

cmd/migrate_from_elasticsearch.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,13 @@ func (r *MigrateFromElasticsearchCmd) countElasticsearchDocuments(ctx context.Co
139139
return 0, fmt.Errorf("failed to decode count response: %w", err)
140140
}
141141

142+
if res.IsError() {
143+
return 0, handleElasticOpenSearchHTTPError(res.StatusCode, result, "Elasticsearch")
144+
}
145+
142146
count, ok := result["count"].(float64)
143147
if !ok {
144-
return 0, fmt.Errorf("invalid count response format")
148+
return 0, fmt.Errorf("invalid count response format - expected 'count' field, got: %v", result)
145149
}
146150

147151
return int64(count), nil
@@ -432,6 +436,10 @@ func (r *MigrateFromElasticsearchCmd) searchWithPagination(ctx context.Context,
432436
return nil, fmt.Errorf("failed to decode search response: %w", err)
433437
}
434438

439+
if res.IsError() {
440+
return nil, handleElasticOpenSearchHTTPError(res.StatusCode, searchResp, "Elasticsearch")
441+
}
442+
435443
hitsContainer, ok := searchResp["hits"].(map[string]any)
436444
if !ok {
437445
return nil, fmt.Errorf("invalid response format: missing hits container")

cmd/migrate_from_opensearch.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,13 @@ func (r *MigrateFromOpenSearchCmd) countOpenSearchDocuments(ctx context.Context,
136136
return 0, fmt.Errorf("failed to decode count response: %w", err)
137137
}
138138

139+
if res.StatusCode >= 400 {
140+
return 0, handleElasticOpenSearchHTTPError(res.StatusCode, result, "OpenSearch")
141+
}
142+
139143
count, ok := result["count"].(float64)
140144
if !ok {
141-
return 0, fmt.Errorf("invalid count response format")
145+
return 0, fmt.Errorf("invalid count response format - expected 'count' field, got: %v", result)
142146
}
143147

144148
return int64(count), nil
@@ -410,6 +414,10 @@ func (r *MigrateFromOpenSearchCmd) searchWithPagination(ctx context.Context, cli
410414
return nil, fmt.Errorf("OpenSearch error: %v", errorInfo)
411415
}
412416

417+
if res.StatusCode >= 400 {
418+
return nil, handleElasticOpenSearchHTTPError(res.StatusCode, searchResp, "OpenSearch")
419+
}
420+
413421
hitsContainer, ok := searchResp["hits"].(map[string]any)
414422
if !ok {
415423
return nil, fmt.Errorf("invalid response format: missing hits container")

cmd/utils.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,17 @@ func arbitraryIDToUUID(id string) *qdrant.PointId {
147147
deterministicUUID := uuid.NewSHA1(uuid.NameSpaceURL, []byte(id))
148148
return qdrant.NewIDUUID(deterministicUUID.String())
149149
}
150+
151+
// Provides helpful error messages for HTTP responses from ElasticSearch/OpenSearch.
152+
func handleElasticOpenSearchHTTPError(statusCode int, responseBody map[string]any, source string) error {
153+
if statusCode == 401 {
154+
return fmt.Errorf("failed to authenticate with %s (status 401): please verify your credentials (username, password, or API key)", source)
155+
}
156+
157+
// Check if response contains error details
158+
if errorInfo, ok := responseBody["error"]; ok {
159+
return fmt.Errorf("%s returned error (status %d): %v", source, statusCode, errorInfo)
160+
}
161+
162+
return fmt.Errorf("%s request failed with status %d", source, statusCode)
163+
}

0 commit comments

Comments
 (0)