Skip to content

Commit 1d5a0c9

Browse files
committed
ING-1310: Improve FTS error handling
ING-1310: Return concrete err when only bucket or scope name set ING-1310: Handle indexname invalid error from server ING-1310: Handle index name too long error ING-1310: Return a concrete error when index name is blank ING-1310: Handle index not found error when updating index
1 parent d60b7b5 commit 1d5a0c9

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

cbsearchx/errors.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ var (
1010
ErrAuthenticationFailure = errors.New("auth error")
1111
ErrIndexNotFound = errors.New("index not found")
1212
ErrIndexExists = errors.New("index exists")
13+
ErrIndexNameInvalid = errors.New("index name invalid")
14+
ErrIndexNameTooLong = errors.New("index name too long")
15+
ErrIndexNameEmpty = errors.New("index name empty")
1316
ErrUnknownIndexType = errors.New("unknown index type")
1417
ErrSourceTypeIncorrect = errors.New("source type incorrect")
1518
ErrSourceNotFound = errors.New("source not found")
1619
ErrNoIndexPartitionsPlanned = errors.New("no index partitions planned")
1720
ErrNoIndexPartitionsFound = errors.New("no index partitions found")
1821
ErrUnsupportedFeature = errors.New("unsupported feature")
22+
ErrOnlyBucketOrScopeSet = errors.New("only bucket or scope name set")
1923
)
2024

2125
type contextualError struct {

cbsearchx/search.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (h Search) UpsertIndex(
125125
opts *UpsertIndexOptions,
126126
) (*UpsertIndexResponse, error) {
127127
if opts.Name == "" {
128-
return nil, errors.New("must specify index name when creating an index")
128+
return nil, ErrIndexNameEmpty
129129
}
130130
if opts.Type == "" {
131131
return nil, errors.New("must specify index type when creating an index")
@@ -139,7 +139,7 @@ func (h Search) UpsertIndex(
139139
reqURI = fmt.Sprintf("/api/index/%s", opts.Name)
140140
} else {
141141
if opts.ScopeName == "" || opts.BucketName == "" {
142-
return nil, errors.New("must specify both or neither of scope and bucket names")
142+
return nil, ErrOnlyBucketOrScopeSet
143143
}
144144
reqURI = fmt.Sprintf("/api/bucket/%s/scope/%s/index/%s", opts.BucketName, opts.ScopeName, opts.Name)
145145
}
@@ -188,15 +188,15 @@ func (h Search) DeleteIndex(
188188
opts *DeleteIndexOptions,
189189
) error {
190190
if opts.IndexName == "" {
191-
return errors.New("must specify index name when deleting an index")
191+
return ErrIndexNameEmpty
192192
}
193193

194194
var reqURI string
195195
if opts.ScopeName == "" && opts.BucketName == "" {
196196
reqURI = fmt.Sprintf("/api/index/%s", opts.IndexName)
197197
} else {
198198
if opts.ScopeName == "" || opts.BucketName == "" {
199-
return errors.New("must specify both or neither of scope and bucket names")
199+
return ErrOnlyBucketOrScopeSet
200200
}
201201
reqURI = fmt.Sprintf("/api/bucket/%s/scope/%s/index/%s", opts.BucketName, opts.ScopeName, opts.IndexName)
202202
}
@@ -235,15 +235,15 @@ func (h Search) GetIndex(
235235
opts *GetIndexOptions,
236236
) (*Index, error) {
237237
if opts.IndexName == "" {
238-
return nil, errors.New("must specify index name when getting an index")
238+
return nil, ErrIndexNameEmpty
239239
}
240240

241241
var reqURI string
242242
if opts.ScopeName == "" && opts.BucketName == "" {
243243
reqURI = fmt.Sprintf("/api/index/%s", opts.IndexName)
244244
} else {
245245
if opts.ScopeName == "" || opts.BucketName == "" {
246-
return nil, errors.New("must specify both or neither of scope and bucket names")
246+
return nil, ErrOnlyBucketOrScopeSet
247247
}
248248
reqURI = fmt.Sprintf("/api/bucket/%s/scope/%s/index/%s", opts.BucketName, opts.ScopeName, opts.IndexName)
249249
}
@@ -295,7 +295,7 @@ func (h Search) GetAllIndexes(
295295
reqURI = "/api/index/"
296296
} else {
297297
if opts.ScopeName == "" || opts.BucketName == "" {
298-
return nil, errors.New("must specify both or neither of scope and bucket names")
298+
return nil, ErrOnlyBucketOrScopeSet
299299
}
300300
reqURI = fmt.Sprintf("/api/bucket/%s/scope/%s/index", opts.BucketName, opts.ScopeName)
301301
}
@@ -353,15 +353,15 @@ func (h Search) AnalyzeDocument(
353353
opts *AnalyzeDocumentOptions,
354354
) (*DocumentAnalysis, error) {
355355
if opts.IndexName == "" {
356-
return nil, errors.New("must specify index name when analyzing a document")
356+
return nil, ErrIndexNameEmpty
357357
}
358358

359359
var reqURI string
360360
if opts.ScopeName == "" && opts.BucketName == "" {
361361
reqURI = fmt.Sprintf("/api/index/%s/analyzeDoc", opts.IndexName)
362362
} else {
363363
if opts.ScopeName == "" || opts.BucketName == "" {
364-
return nil, errors.New("must specify both or neither of scope and bucket names")
364+
return nil, ErrOnlyBucketOrScopeSet
365365
}
366366
reqURI = fmt.Sprintf("/api/index/%s.%s.%s/analyzeDoc", opts.BucketName, opts.ScopeName, opts.IndexName)
367367
}
@@ -406,15 +406,15 @@ func (h Search) GetIndexedDocumentsCount(
406406
opts *GetIndexedDocumentsCountOptions,
407407
) (uint64, error) {
408408
if opts.IndexName == "" {
409-
return 0, errors.New("must specify index name when analyzing a document")
409+
return 0, ErrIndexNameEmpty
410410
}
411411

412412
var reqURI string
413413
if opts.ScopeName == "" && opts.BucketName == "" {
414414
reqURI = fmt.Sprintf("/api/index/%s/count", opts.IndexName)
415415
} else {
416416
if opts.ScopeName == "" || opts.BucketName == "" {
417-
return 0, errors.New("must specify both or neither of scope and bucket names")
417+
return 0, ErrOnlyBucketOrScopeSet
418418
}
419419
reqURI = fmt.Sprintf("/api/bucket/%s/scope/%s/index/%s/count", opts.BucketName, opts.ScopeName, opts.IndexName)
420420
}
@@ -534,15 +534,15 @@ func (h Search) controlRequest(
534534
onBehalfOf *cbhttpx.OnBehalfOfInfo,
535535
) error {
536536
if indexName == "" {
537-
return errors.New("must specify index name")
537+
return ErrIndexNameEmpty
538538
}
539539

540540
var reqURI string
541541
if scopeName == "" && bucketName == "" {
542542
reqURI = fmt.Sprintf("/api/index/%s/%s", indexName, control)
543543
} else {
544544
if scopeName == "" || bucketName == "" {
545-
return errors.New("must specify both or neither of scope and bucket names")
545+
return ErrOnlyBucketOrScopeSet
546546
}
547547
reqURI = fmt.Sprintf("/api/bucket/%s/scope/%s/index/%s/%s", bucketName, scopeName, indexName, control)
548548
}
@@ -607,10 +607,14 @@ func (h Search) DecodeCommonError(resp *http.Response) error {
607607
var err error
608608
errText := strings.ToLower(string(bodyBytes))
609609

610-
if strings.Contains(errText, "index not found") {
610+
if strings.Contains(errText, "index not found") || strings.Contains(errText, "index missing for update") {
611611
err = ErrIndexNotFound
612612
} else if strings.Contains(errText, "index with the same name already exists") {
613613
err = ErrIndexExists
614+
} else if strings.Contains(errText, "indexname is invalid") {
615+
err = ErrIndexNameInvalid
616+
} else if strings.Contains(errText, "index name is too long") {
617+
err = ErrIndexNameTooLong
614618
} else if strings.Contains(errText, "current index uuid") && strings.Contains(errText, "did not match input uuid") {
615619
err = ErrIndexExists
616620
} else if strings.Contains(errText, "unknown indextype") {

cbsearchx/search_int_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,15 @@ func TestGetIndex(t *testing.T) {
219219

220220
t.Run("MissingIndexName", func(t *testing.T) {
221221
_, err := search.GetIndex(ctx, &cbsearchx.GetIndexOptions{})
222-
require.Equal(t, errors.New("must specify index name when getting an index"), err)
222+
require.Equal(t, cbsearchx.ErrIndexNameEmpty, err)
223223
})
224224

225225
t.Run("MissingBucketName", func(t *testing.T) {
226226
_, err := search.GetIndex(ctx, &cbsearchx.GetIndexOptions{
227227
IndexName: indexName,
228228
ScopeName: "_default",
229229
})
230-
require.Equal(t, errors.New("must specify both or neither of scope and bucket names"), err)
230+
require.Equal(t, cbsearchx.ErrOnlyBucketOrScopeSet, err)
231231
})
232232

233233
t.Run("IndexNotFound", func(t *testing.T) {
@@ -309,15 +309,15 @@ func TestDeleteIndex(t *testing.T) {
309309

310310
t.Run("IndexNameMissing", func(t *testing.T) {
311311
err := search.DeleteIndex(ctx, &cbsearchx.DeleteIndexOptions{})
312-
require.Equal(t, errors.New("must specify index name when deleting an index"), err)
312+
require.Equal(t, cbsearchx.ErrIndexNameEmpty, err)
313313
})
314314

315315
t.Run("IndexMissingScope", func(t *testing.T) {
316316
err := search.DeleteIndex(ctx, &cbsearchx.DeleteIndexOptions{
317317
IndexName: indexName,
318318
BucketName: testutilsint.TestOpts.BucketName,
319319
})
320-
require.Equal(t, errors.New("must specify both or neither of scope and bucket names"), err)
320+
require.Equal(t, cbsearchx.ErrOnlyBucketOrScopeSet, err)
321321
})
322322

323323
t.Run("SuccessScopedSearch", func(t *testing.T) {
@@ -394,7 +394,7 @@ func TestGetAllIndexes(t *testing.T) {
394394
_, err := search.GetAllIndexes(ctx, &cbsearchx.GetAllIndexesOptions{
395395
BucketName: testutilsint.TestOpts.BucketName,
396396
})
397-
require.Equal(t, errors.New("must specify both or neither of scope and bucket names"), err)
397+
require.Equal(t, cbsearchx.ErrOnlyBucketOrScopeSet, err)
398398
})
399399

400400
t.Run("SuccessScopedSearch", func(t *testing.T) {

0 commit comments

Comments
 (0)