-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy patherrorhandler.go
More file actions
1050 lines (938 loc) · 35.4 KB
/
Copy patherrorhandler.go
File metadata and controls
1050 lines (938 loc) · 35.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package server_v1
import (
"context"
"errors"
"fmt"
"net"
"strings"
"github.com/couchbase/gocbcorex/cbmgmtx"
"github.com/couchbase/gocbcorex/cbqueryx"
"github.com/couchbase/gocbcorex/cbsearchx"
"github.com/couchbase/gocbcorex/memdx"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/runtime/protoiface"
epb "google.golang.org/genproto/googleapis/rpc/errdetails"
)
/*
INVALID_ARGUMENT - The client sent a value which could never be considered correct.
FAILED_PRECONDITION - Something is in a state making the request invalid, retrying _COULD_ help.
OUT_OF_RANGE - More specific version of FAILED_PRECONDITION, useful to allow clients to iterate results until hitting this.
UNAUTHENTICATED - Occurs when credentials are not sent for something that must be authenticated.
PERMISSION_DENIED - Credentials were sent, but are insufficient.
NOT_FOUND - More specific version of FAILED_PRECONDITION where the document must exist, but does not.
ABORTED - Operation was running but was unambiguously aborted, must be retried at a higher level than at the request level.
ALREADY_EXISTS - More specific version of FAILED_PRECONDITION where the document must not exist, but does.
RESOURCE_EXHAUSTED - Indicates that some transient resource was exhausted, this could be quotas, limits, etc... Implies retriability.
CANCELLED - Happens when a client explicitly cancels an operation.
DATA_LOSS - Indicates that data was lost unexpectedly and the operation cannot succeed.
UNKNOWN - Specified when no information about the cause of the error is available, otherwise INTERNAL should be used.
INTERNAL - Indicates any sort of error where the protocol wont provide parseable details to the client.
NOT_IMPLEMENTED - Indicates that a feature is not implemented, either a whole RPC, or possibly an option.
UNAVAILABLE - Cannot access the resource for some reason, clients can generate this if the server isn't available.
DEADLINE_EXCEEDED - Timeout occurred while processing.
*/
type ErrorHandler struct {
Logger *zap.Logger
Debug bool
}
func (e ErrorHandler) tryAttachStatusDetails(st *status.Status, details ...protoiface.MessageV1) *status.Status {
// try to attach the details
if newSt, err := st.WithDetails(details...); err == nil {
return newSt
}
// if we failed to attach the details, just return the original error
return st
}
func (e ErrorHandler) tryAttachExtraContext(st *status.Status, baseErr error) *status.Status {
if baseErr == nil {
return st
}
var memdSrvErr *memdx.ServerErrorWithContext
if errors.As(baseErr, &memdSrvErr) {
parsedCtx := memdSrvErr.ParseContext()
if parsedCtx.Ref != "" {
st = e.tryAttachStatusDetails(st, &epb.RequestInfo{
RequestId: parsedCtx.Ref,
})
}
}
if e.Debug {
st = e.tryAttachStatusDetails(st, &epb.DebugInfo{
Detail: baseErr.Error(),
})
}
return st
}
func (e ErrorHandler) NewInternalStatus() *status.Status {
st := status.New(codes.Internal, "An internal error occurred.")
return st
}
func (e ErrorHandler) NewUnknownStatus(baseErr error) *status.Status {
var memdErr *memdx.ServerError
if errors.As(baseErr, &memdErr) {
st := status.New(codes.Unknown, fmt.Sprintf("An unknown memcached error occurred (status: %d).", memdErr.Status))
st = e.tryAttachExtraContext(st, baseErr)
return st
}
var queryErr *cbqueryx.ServerErrors
if errors.As(baseErr, &queryErr) {
var queryErrDescs []string
for _, querySubErr := range queryErr.Errors {
queryErrDescs = append(queryErrDescs, fmt.Sprintf("%d - %s", querySubErr.Code, querySubErr.Msg))
}
st := status.New(codes.Unknown,
fmt.Sprintf("An unknown query error occurred (descs: %s).", strings.Join(queryErrDescs, "; ")))
st = e.tryAttachExtraContext(st, baseErr)
return st
}
var searchErr *cbsearchx.ServerError
if errors.As(baseErr, &searchErr) {
st := status.New(codes.Unknown,
fmt.Sprintf("An unknown search error occurred (status: %d).", searchErr.StatusCode))
st = e.tryAttachExtraContext(st, baseErr)
return st
}
var serverErr *cbmgmtx.ServerError
if errors.As(baseErr, &serverErr) {
st := status.New(codes.Unknown,
fmt.Sprintf("An unknown server error occurred (status: %d).", serverErr.StatusCode))
st = e.tryAttachExtraContext(st, baseErr)
return st
}
st := status.New(codes.Unknown, "An unknown error occurred.")
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewBucketMissingStatus(baseErr error, bucketName string) *status.Status {
st := status.New(codes.NotFound,
fmt.Sprintf("Bucket '%s' was not found.",
bucketName))
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "bucket",
ResourceName: bucketName,
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewBucketExistsStatus(baseErr error, bucketName string) *status.Status {
st := status.New(codes.AlreadyExists,
fmt.Sprintf("Bucket '%s' already existed.",
bucketName))
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "bucket",
ResourceName: bucketName,
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewBucketFlushDisabledStatus(baseErr error, bucketName string) *status.Status {
st := status.New(codes.FailedPrecondition,
fmt.Sprintf("Flush is disabled for bucket '%s'.",
bucketName))
st = e.tryAttachStatusDetails(st, &epb.PreconditionFailure{
Violations: []*epb.PreconditionFailure_Violation{{
Type: "FLUSH_DISABLED",
Subject: bucketName,
Description: "",
}},
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewBucketInvalidArgStatus(baseErr error, msg string, bucketName string) *status.Status {
if msg == "" {
msg = "invalid argument"
}
st := status.New(codes.InvalidArgument, msg)
var sErr *cbmgmtx.ServerInvalidArgError
if errors.As(baseErr, &sErr) {
st = status.New(codes.InvalidArgument, fmt.Sprintf("invalid argument: %s - %s", sErr.Argument, sErr.Reason))
}
if baseErr != nil {
st = e.tryAttachExtraContext(st, baseErr)
}
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "bucket",
ResourceName: bucketName,
Description: "",
})
return st
}
func (e ErrorHandler) NewBucketAccessDeniedStatus(baseErr error, bucketName string) *status.Status {
msg := "No permissions to perform bucket management operation."
st := status.New(codes.PermissionDenied, msg)
st = e.tryAttachStatusDetails(
st, &epb.ResourceInfo{
ResourceType: "bucket",
ResourceName: bucketName,
Description: "",
})
return st
}
func (e ErrorHandler) NewCollectionInvalidArgStatus(baseErr error, msg string, bucket, scope, collection string) *status.Status {
if msg == "" {
msg = "invalid argument"
}
st := status.New(codes.InvalidArgument, msg)
var sErr *cbmgmtx.ServerInvalidArgError
if errors.As(baseErr, &sErr) {
st = status.New(codes.InvalidArgument, fmt.Sprintf("invalid argument: %s - %s", sErr.Argument, sErr.Reason))
}
if baseErr != nil {
st = e.tryAttachExtraContext(st, baseErr)
}
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "collection",
ResourceName: fmt.Sprintf("%s/%s/%s", bucket, scope, collection),
Description: "",
})
return st
}
func (e ErrorHandler) NewScopeMissingStatus(baseErr error, bucketName, scopeName string) *status.Status {
st := status.New(codes.NotFound,
fmt.Sprintf("Scope '%s' not found in '%s'.",
scopeName, bucketName))
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "scope",
ResourceName: fmt.Sprintf("%s/%s", bucketName, scopeName),
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewCollectionMissingStatus(baseErr error, bucketName, scopeName, collectionName string) *status.Status {
st := status.New(codes.NotFound,
fmt.Sprintf("Collection '%s' not found in '%s/%s'.",
collectionName, bucketName, scopeName))
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "collection",
ResourceName: fmt.Sprintf("%s/%s/%s", bucketName, scopeName, collectionName),
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewScopeExistsStatus(baseErr error, bucketName, scopeName string) *status.Status {
st := status.New(codes.AlreadyExists,
fmt.Sprintf("Scope '%s' already existed in '%s'.",
scopeName, bucketName))
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "scope",
ResourceName: fmt.Sprintf("%s/%s", bucketName, scopeName),
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewCollectionExistsStatus(baseErr error, bucketName, scopeName, collectionName string) *status.Status {
st := status.New(codes.AlreadyExists,
fmt.Sprintf("Collection '%s' already existed in '%s/%s'.",
collectionName, bucketName, scopeName))
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "collection",
ResourceName: fmt.Sprintf("%s/%s/%s", bucketName, scopeName, collectionName),
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewQueryIndexMissingStatus(baseErr error, indexName, bucketName, scopeName, collectionName string) *status.Status {
var path string
if bucketName != "" {
path = bucketName
if scopeName != "" {
path = path + "/" + scopeName
if collectionName != "" {
path = path + "/" + collectionName
}
}
}
var msg string
if indexName == "" {
msg = "Query index not found."
} else {
msg = fmt.Sprintf("Query index '%s' not found in '%s'.", indexName, path)
}
st := status.New(codes.NotFound, msg)
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "queryindex",
ResourceName: indexName,
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewQueryIndexExistsStatus(baseErr error, indexName, bucketName, scopeName, collectionName string) *status.Status {
var path string
if bucketName != "" {
path = bucketName
if scopeName != "" {
path = path + "/" + scopeName
if collectionName != "" {
path = path + "/" + collectionName
}
}
}
var msg string
if indexName == "" {
msg = "Query index already existed."
} else {
msg = fmt.Sprintf("Query index '%s' already existed in '%s'.", indexName, path)
}
st := status.New(codes.AlreadyExists, msg)
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "queryindex",
ResourceName: indexName,
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewQueryIndexNotBuildingStatus(baseErr error, bucketName, scopeName, collectionName, indexName string) *status.Status {
st := status.New(codes.FailedPrecondition,
fmt.Sprintf("Cannot wait for index '%s' in '%s/%s/%s' to be ready as it is still deferred.",
indexName, bucketName, scopeName, collectionName))
st = e.tryAttachStatusDetails(st, &epb.PreconditionFailure{
Violations: []*epb.PreconditionFailure_Violation{{
Type: "NOT_BUILDING",
Subject: fmt.Sprintf("%s/%s/%s/%s", bucketName, scopeName, collectionName, indexName),
Description: "",
}},
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewQueryIndexAuthenticationFailureStatus(baseErr error, bucketName, scopeName, collectionName string) *status.Status {
var path string
var resource string
if bucketName != "" {
path = bucketName
resource = "bucket"
if scopeName != "" {
path = path + "/" + scopeName
resource = "scope"
if collectionName != "" {
path = path + "/" + collectionName
resource = "collection"
}
}
}
msg := fmt.Sprintf("Insufficient permissions to perform query index operation against %s.", path)
st := status.New(codes.PermissionDenied, msg)
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: resource,
ResourceName: path,
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewQueryIndexInvalidArgumentStatus(baseErr error, indexName, msg string) *status.Status {
st := status.New(codes.InvalidArgument, msg)
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "queryindex",
ResourceName: indexName,
Description: "",
})
if baseErr != nil {
e.tryAttachExtraContext(st, baseErr)
}
return st
}
func (e ErrorHandler) NewSearchServiceNotAvailableStatus(baseErr error, indexName string) *status.Status {
st := status.New(codes.Unimplemented, "Search service is not available.")
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "searchIndex",
ResourceName: indexName,
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewSearchIndexMissingStatus(baseErr error, indexName string) *status.Status {
st := status.New(codes.NotFound,
fmt.Sprintf("Search index '%s' not found.",
indexName))
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "searchindex",
ResourceName: indexName,
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewSearchIndexExistsStatus(baseErr error, indexName string) *status.Status {
st := status.New(codes.AlreadyExists,
fmt.Sprintf("Search index '%s' already existed.",
indexName))
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "searchindex",
ResourceName: indexName,
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewSearchIndexNameInvalidStatus(baseErr error, indexName string) *status.Status {
st := status.New(codes.InvalidArgument,
fmt.Sprintf("Name for search index '%s' is invalid.",
indexName))
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "searchindex",
ResourceName: indexName,
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewSearchIndexNameTooLongStatus(baseErr error, indexName string) *status.Status {
st := status.New(codes.InvalidArgument,
fmt.Sprintf("Search index name '%s' is too long.",
indexName))
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "searchindex",
ResourceName: indexName,
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewSearchIndexNameEmptyStatus(baseErr error) *status.Status {
st := status.New(codes.InvalidArgument, "Must specify an index name when performing this operation.")
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "searchindex",
ResourceName: "",
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewUnknownSearchIndexTypeStatus(baseErr error, indexName string, indexType string) *status.Status {
st := status.New(codes.InvalidArgument,
fmt.Sprintf("Search index type '%s' is unknown.",
indexType))
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "searchindex",
ResourceName: indexName,
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewSearchUUIDMismatchStatus(baseErr error, indexName string) *status.Status {
st := status.New(codes.Aborted,
fmt.Sprintf("Search index '%s' already existed with a different UUID.",
indexName))
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "searchindex",
ResourceName: indexName,
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewOnlyBucketOrScopeSetStatus(baseErr error, indexName string) *status.Status {
st := status.New(codes.InvalidArgument, "Must specify both or neither of scope and bucket names.")
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "searchindex",
ResourceName: indexName,
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewSearchIndexNotReadyStatus(baseErr error, indexName string) *status.Status {
st := status.New(codes.Unavailable, "Search index is still being built, try again later.")
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "searchindex",
ResourceName: indexName,
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewIncorrectSearchSourceTypeStatus(baseErr error, indexName string, sourceType *string) *status.Status {
var sT string
if sourceType != nil {
sT = *sourceType
}
st := status.New(codes.InvalidArgument, fmt.Sprintf("'%s' is not a valid source type.", sT))
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "searchindex",
ResourceName: indexName,
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewSearchSourceNotFoundStatus(baseErr error, indexName string, sourceName *string) *status.Status {
var sN string
if sourceName != nil {
sN = *sourceName
}
st := status.New(codes.NotFound, fmt.Sprintf("Source bucket '%s' for search index was not found.", sN))
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "bucket",
ResourceName: indexName,
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewSearchIndexAuthenticationFailureStatus(baseErr error, bucketName, scopeName *string) *status.Status {
var path string
var resource string
if bucketName != nil {
path = *bucketName
resource = "bucket"
if scopeName != nil {
path = path + "/" + *scopeName
resource = "scope"
}
}
msg := fmt.Sprintf("Insufficient permissions to perform search index operation against %s.", path)
st := status.New(codes.PermissionDenied, msg)
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: resource,
ResourceName: path,
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewDocMissingStatus(baseErr error, bucketName, scopeName, collectionName, docId string) *status.Status {
st := status.New(codes.NotFound,
fmt.Sprintf("Document '%s' not found in '%s/%s/%s'.",
docId, bucketName, scopeName, collectionName))
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "document",
ResourceName: fmt.Sprintf("%s/%s/%s/%s", bucketName, scopeName, collectionName, docId),
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewDocExistsStatus(baseErr error, bucketName, scopeName, collectionName, docId string) *status.Status {
st := status.New(codes.AlreadyExists,
fmt.Sprintf("Document '%s' already existed in '%s/%s/%s'.",
docId, bucketName, scopeName, collectionName))
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "document",
ResourceName: fmt.Sprintf("%s/%s/%s/%s", bucketName, scopeName, collectionName, docId),
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewDocCasMismatchStatus(baseErr error, bucketName, scopeName, collectionName, docId string) *status.Status {
st := status.New(codes.Aborted,
fmt.Sprintf("The specified CAS for '%s' in '%s/%s/%s' did not match.",
docId, bucketName, scopeName, collectionName))
st = e.tryAttachStatusDetails(st, &epb.ErrorInfo{
Reason: "CAS_MISMATCH",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewDocConflictStatus(baseErr error, bucketName, scopeName, collectionName, docId string) *status.Status {
st := status.New(codes.Aborted,
fmt.Sprintf("Conflict resolution rejected '%s' in '%s/%s/%s'.",
docId, bucketName, scopeName, collectionName))
st = e.tryAttachStatusDetails(st, &epb.ErrorInfo{
Reason: "DOC_NEWER",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewZeroCasStatus() *status.Status {
st := status.New(codes.InvalidArgument, "CAS value cannot be zero.")
return st
}
func (e ErrorHandler) NewDocLockedStatus(baseErr error, bucketName, scopeName, collectionName, docId string) *status.Status {
st := status.New(codes.FailedPrecondition,
fmt.Sprintf("Cannot perform a write operation against locked document '%s' in '%s/%s/%s'.",
docId, bucketName, scopeName, collectionName))
st = e.tryAttachStatusDetails(st, &epb.PreconditionFailure{
Violations: []*epb.PreconditionFailure_Violation{{
Type: "LOCKED",
Subject: fmt.Sprintf("%s/%s/%s/%s", bucketName, scopeName, collectionName, docId),
Description: "",
}},
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewDocNotLockedStatus(baseErr error, bucketName, scopeName, collectionName, docId string) *status.Status {
st := status.New(codes.FailedPrecondition,
fmt.Sprintf("Cannot unlock an unlocked document '%s' in '%s/%s/%s'.",
docId, bucketName, scopeName, collectionName))
st = e.tryAttachStatusDetails(st, &epb.PreconditionFailure{
Violations: []*epb.PreconditionFailure_Violation{{
Type: "NOT_LOCKED",
Subject: fmt.Sprintf("%s/%s/%s/%s", bucketName, scopeName, collectionName, docId),
Description: "",
}},
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewDocNotNumericStatus(baseErr error, bucketName, scopeName, collectionName, docId string) *status.Status {
var st *status.Status
st = status.New(codes.FailedPrecondition,
fmt.Sprintf("Cannot perform counter operation on non-numeric document '%s' in '%s/%s/%s'.",
docId, bucketName, scopeName, collectionName))
st = e.tryAttachStatusDetails(st, &epb.PreconditionFailure{
Violations: []*epb.PreconditionFailure_Violation{{
Type: "DOC_NOT_NUMERIC",
Subject: fmt.Sprintf("%s/%s/%s/%s", bucketName, scopeName, collectionName, docId),
Description: "",
}},
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewValueTooLargeStatus(baseErr error, bucketName, scopeName, collectionName, docId string,
isExpandingValue bool) *status.Status {
var st *status.Status
if isExpandingValue {
st = status.New(codes.FailedPrecondition,
fmt.Sprintf("Updated value '%s' made value too large in '%s/%s/%s'.",
docId, bucketName, scopeName, collectionName))
st = e.tryAttachStatusDetails(st, &epb.PreconditionFailure{
Violations: []*epb.PreconditionFailure_Violation{{
Type: "VALUE_TOO_LARGE",
Subject: fmt.Sprintf("%s/%s/%s/%s", bucketName, scopeName, collectionName, docId),
Description: "",
}},
})
} else {
st = status.New(codes.InvalidArgument,
fmt.Sprintf("Value '%s' for new document was too large in '%s/%s/%s'.",
docId, bucketName, scopeName, collectionName))
}
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewDurabilityImpossibleStatus(baseErr error, bucketName string) *status.Status {
var st *status.Status
st = status.New(codes.FailedPrecondition,
fmt.Sprintf("Not enough servers to use this durability level on '%s' bucket.",
bucketName))
st = e.tryAttachStatusDetails(st, &epb.PreconditionFailure{
Violations: []*epb.PreconditionFailure_Violation{{
Type: "DURABILITY_IMPOSSIBLE",
Subject: fmt.Sprintf("%ss", bucketName),
Description: "",
}},
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewSyncWriteAmbiguousStatus(baseErr error, bucketName, scopeName, collectionName, docId string) *status.Status {
return status.New(codes.DeadlineExceeded,
fmt.Sprintf("Sync write operation on '%s' in '%s/%s/%s' timed out.",
docId, bucketName, scopeName, collectionName))
}
func (e ErrorHandler) NewCollectionNoReadAccessStatus(baseErr error, bucketName, scopeName, collectionName string) *status.Status {
st := status.New(codes.PermissionDenied,
fmt.Sprintf("No permissions to read documents from '%s/%s/%s'.",
bucketName, scopeName, collectionName))
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "collection",
ResourceName: fmt.Sprintf("%s/%s/%s", bucketName, scopeName, collectionName),
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewCollectionNoWriteAccessStatus(baseErr error, bucketName, scopeName, collectionName string) *status.Status {
st := status.New(codes.PermissionDenied,
fmt.Sprintf("No permissions to write documents into '%s/%s/%s'.",
bucketName, scopeName, collectionName))
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "collection",
ResourceName: fmt.Sprintf("%s/%s/%s", bucketName, scopeName, collectionName),
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewSdDocTooDeepStatus(baseErr error, bucketName, scopeName, collectionName, docId string) *status.Status {
st := status.New(codes.FailedPrecondition,
fmt.Sprintf("Document '%s' JSON was too deep to parse in '%s/%s/%s'.",
docId, bucketName, scopeName, collectionName))
st = e.tryAttachStatusDetails(st, &epb.PreconditionFailure{
Violations: []*epb.PreconditionFailure_Violation{{
Type: "DOC_TOO_DEEP",
Subject: fmt.Sprintf("%s/%s/%s/%s", bucketName, scopeName, collectionName, docId),
Description: "",
}},
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewSdDocNotJsonStatus(baseErr error, bucketName, scopeName, collectionName, docId string) *status.Status {
st := status.New(codes.FailedPrecondition,
fmt.Sprintf("Document '%s' was not JSON in '%s/%s/%s'.",
docId, bucketName, scopeName, collectionName))
st = e.tryAttachStatusDetails(st, &epb.PreconditionFailure{
Violations: []*epb.PreconditionFailure_Violation{{
Type: "DOC_NOT_JSON",
Subject: fmt.Sprintf("%s/%s/%s/%s", bucketName, scopeName, collectionName, docId),
Description: "",
}},
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewSdPathNotFoundStatus(baseErr error, bucketName, scopeName, collectionName, docId, sdPath string) *status.Status {
st := status.New(codes.NotFound,
fmt.Sprintf("Subdocument path '%s' was not found in '%s' in '%s/%s/%s'.",
sdPath, docId, bucketName, scopeName, collectionName))
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "path",
ResourceName: fmt.Sprintf("%s/%s/%s/%s/%s", bucketName, scopeName, collectionName, docId, sdPath),
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewSdPathExistsStatus(baseErr error, bucketName, scopeName, collectionName, docId, sdPath string) *status.Status {
st := status.New(codes.AlreadyExists,
fmt.Sprintf("Subdocument path '%s' already existed in '%s' in '%s/%s/%s'.",
sdPath, docId, bucketName, scopeName, collectionName))
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "path",
ResourceName: fmt.Sprintf("%s/%s/%s/%s/%s", bucketName, scopeName, collectionName, docId, sdPath),
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewSdPathMismatchStatus(baseErr error, bucketName, scopeName, collectionName, docId, sdPath string) *status.Status {
st := status.New(codes.FailedPrecondition,
fmt.Sprintf("Document structure implied by path '%s' did not match document '%s' in '%s/%s/%s'.",
sdPath, docId, bucketName, scopeName, collectionName))
st = e.tryAttachStatusDetails(st, &epb.PreconditionFailure{
Violations: []*epb.PreconditionFailure_Violation{{
Type: "PATH_MISMATCH",
Subject: sdPath,
Description: "",
}},
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewSdPathTooBigStatus(baseErr error, sdPath string) *status.Status {
st := status.New(codes.InvalidArgument,
fmt.Sprintf("Subdocument path '%s' is too long", sdPath))
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewSdBadValueStatus(baseErr error, sdPath string) *status.Status {
st := status.New(codes.InvalidArgument,
fmt.Sprintf("Subdocument operation content for path '%s' would invalidate the JSON if added to the document.",
sdPath))
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewSdValueOutOfRangeStatus(baseErr error, sdPath string) *status.Status {
st := status.New(codes.FailedPrecondition,
fmt.Sprintf("Counter operation content for path '%s' would put the JSON value out of range.",
sdPath))
st = e.tryAttachStatusDetails(st, &epb.PreconditionFailure{
Violations: []*epb.PreconditionFailure_Violation{{
Type: "VALUE_OUT_OF_RANGE",
Subject: sdPath,
Description: "",
}},
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewSdBadRangeStatus(baseErr error, bucketName, scopeName, collectionName, docId, sdPath string) *status.Status {
st := status.New(codes.FailedPrecondition,
fmt.Sprintf("The value at path '%s' is out of the valid range in document '%s' in '%s/%s/%s'.",
sdPath, docId, bucketName, scopeName, collectionName))
st = e.tryAttachStatusDetails(st, &epb.PreconditionFailure{
Violations: []*epb.PreconditionFailure_Violation{{
Type: "PATH_VALUE_OUT_OF_RANGE",
Subject: sdPath,
Description: "",
}},
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewSdBadDeltaStatus(baseErr error, sdPath string) *status.Status {
st := status.New(codes.InvalidArgument,
fmt.Sprintf("Subdocument counter delta for path '%s' was invalid. Delta must be a non-zero number within the range of an 64-bit signed integer.",
sdPath))
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewSdValueTooDeepStatus(baseErr error, sdPath string) *status.Status {
st := status.New(codes.InvalidArgument,
fmt.Sprintf("Subdocument operation content for path '%s' was too deep to parse.",
sdPath))
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewSdXattrUnknownVattrStatus(baseErr error, sdPath string) *status.Status {
st := status.New(codes.InvalidArgument,
fmt.Sprintf("Subdocument path '%s' references an invalid virtual attribute.", sdPath))
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewSdBadCombo(baseErr error) *status.Status {
st := status.New(codes.InvalidArgument, "Invalid subdocument combination specified.")
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewSdPathInvalidStatus(baseErr error, sdPath string) *status.Status {
st := status.New(codes.InvalidArgument,
fmt.Sprintf("Invalid subdocument path syntax '%s'.", sdPath))
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewInvalidSnappyValueError() *status.Status {
st := status.New(codes.InvalidArgument, "Failed to snappy inflate value.")
return st
}
func (e ErrorHandler) NewIllogicalCounterExpiry() *status.Status {
st := status.New(codes.InvalidArgument,
"Expiry cannot be set when the document does not exist and Initial is not set. Expiry is only applied to new documents that are created.")
return st
}
func (e ErrorHandler) NewUnsupportedFieldStatus(fieldPath string) *status.Status {
st := status.New(codes.Unimplemented,
fmt.Sprintf("The '%s' field is not currently supported", fieldPath))
return st
}
func (e ErrorHandler) NewInvalidAuthHeaderStatus(baseErr error) *status.Status {
st := status.New(codes.InvalidArgument, "Invalid authorization header format.")
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewNoAuthStatus() *status.Status {
st := status.New(codes.Unauthenticated, "You must send authentication to use this endpoint.")
return st
}
func (e ErrorHandler) NewInvalidCredentialsStatus() *status.Status {
st := status.New(codes.PermissionDenied, "Your username or password is invalid.")
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "user",
ResourceName: "",
Description: "",
})
return st
}
func (e ErrorHandler) NewInvalidCertificateStatus() *status.Status {
st := status.New(codes.PermissionDenied, "Your certificate is invalid.")
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "user",
ResourceName: "",
Description: "",
})
return st
}
func (e ErrorHandler) NewCertAuthDisabledStatus() *status.Status {
st := status.New(codes.Unauthenticated, "Client cert auth disabled on the cluster.")
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "user",
ResourceName: "",
Description: "",
})
return st
}
func (e ErrorHandler) NewUnexpectedAuthTypeStatus() *status.Status {
st := status.New(codes.InvalidArgument, "Unexpected auth type.")
return st
}
func (e ErrorHandler) NewInvalidQueryStatus(baseErr error, queryErrStr string) *status.Status {
st := status.New(codes.InvalidArgument,
fmt.Sprintf("Query parsing failed: %s", queryErrStr))
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewWriteInReadOnlyQueryStatus(baseErr error) *status.Status {
st := status.New(codes.InvalidArgument,
"Write statements cannot be used in a read-only query")
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewQueryNoAccessStatus(baseErr error) *status.Status {
st := status.New(codes.PermissionDenied,
"No permissions to query documents.")
st = e.tryAttachStatusDetails(st, &epb.ResourceInfo{
ResourceType: "user",
ResourceName: "",
Description: "",
})
st = e.tryAttachExtraContext(st, baseErr)
return st
}
func (e ErrorHandler) NewNeedIndexFieldsStatus() *status.Status {
st := status.New(codes.InvalidArgument,
"You must specify fields when creating a new index.")
return st
}
func (e ErrorHandler) NewUnavailableStatus(err error) *status.Status {
st := status.New(codes.Unavailable,
"One of the underlying services were not available.")
e.tryAttachExtraContext(st, err)
return st
}
func (e ErrorHandler) NewGenericStatus(err error) *status.Status {
// we do not attach context in these cases, since they almost always don't actually
// make it back to the client to be processed anyways.
if errors.Is(err, context.Canceled) {
e.Logger.Debug("handling canceled operation error", zap.Error(err))
return status.New(codes.Canceled, "The request was cancelled.")