forked from keybase/client
/
errors.go
643 lines (504 loc) · 16.3 KB
/
errors.go
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
package chat
import (
"errors"
"fmt"
"net"
"time"
"github.com/keybase/client/go/chat/types"
"github.com/keybase/client/go/ephemeral"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/chat1"
"github.com/keybase/client/go/protocol/gregor1"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/client/go/teams"
"github.com/keybase/go-framed-msgpack-rpc/rpc"
"golang.org/x/net/context"
)
var ErrChatServerTimeout = errors.New("timeout calling chat server")
var ErrDuplicateConnection = errors.New("error calling chat server")
var ErrKeyServerTimeout = errors.New("timeout calling into key server")
func NewPermanentUnboxingError(inner error) types.UnboxingError {
return PermanentUnboxingError{inner}
}
type PermanentUnboxingError struct{ inner error }
func (e PermanentUnboxingError) Error() string {
switch err := e.inner.(type) {
case EphemeralUnboxingError, NotAuthenticatedForThisDeviceError:
return err.Error()
default:
return fmt.Sprintf("Unable to decrypt chat message: %s", err.Error())
}
}
func (e PermanentUnboxingError) IsPermanent() bool { return true }
func (e PermanentUnboxingError) Inner() error { return e.inner }
func (e PermanentUnboxingError) ExportType() chat1.MessageUnboxedErrorType {
switch err := e.inner.(type) {
case VersionError:
return err.ExportType()
case EphemeralUnboxingError:
return chat1.MessageUnboxedErrorType_EPHEMERAL
case NotAuthenticatedForThisDeviceError, InvalidMACError:
return chat1.MessageUnboxedErrorType_PAIRWISE_MISSING
default:
return chat1.MessageUnboxedErrorType_MISC
}
}
func (e PermanentUnboxingError) VersionKind() chat1.VersionKind {
switch err := e.inner.(type) {
case VersionError:
return err.VersionKind()
default:
return ""
}
}
func (e PermanentUnboxingError) VersionNumber() int {
switch err := e.inner.(type) {
case VersionError:
return err.VersionNumber()
default:
return 0
}
}
func (e PermanentUnboxingError) IsCritical() bool {
switch err := e.inner.(type) {
case VersionError:
return err.IsCritical()
default:
return false
}
}
func (e PermanentUnboxingError) InternalError() string {
switch err := e.Inner().(type) {
case types.InternalError:
return err.InternalError()
default:
return err.Error()
}
}
func (e PermanentUnboxingError) ToStatus() (status keybase1.Status) {
if ee, ok := e.inner.(libkb.ExportableError); ok {
status = ee.ToStatus()
status.Desc = e.Error()
} else {
status = keybase1.Status{
Name: "GENERIC",
Code: libkb.SCGeneric,
Desc: e.Error(),
}
}
return status
}
// =============================================================================
func NewTransientUnboxingError(inner error) types.UnboxingError {
return TransientUnboxingError{inner}
}
type TransientUnboxingError struct{ inner error }
func (e TransientUnboxingError) Error() string {
return fmt.Sprintf("Unable to decrypt chat message (transient): %s", e.inner.Error())
}
func (e TransientUnboxingError) IsPermanent() bool { return false }
func (e TransientUnboxingError) Inner() error { return e.inner }
func (e TransientUnboxingError) ExportType() chat1.MessageUnboxedErrorType {
return chat1.MessageUnboxedErrorType_MISC
}
func (e TransientUnboxingError) VersionKind() chat1.VersionKind {
return ""
}
func (e TransientUnboxingError) VersionNumber() int {
return 0
}
func (e TransientUnboxingError) IsCritical() bool {
return false
}
func (e TransientUnboxingError) InternalError() string {
switch err := e.Inner().(type) {
case types.InternalError:
return err.InternalError()
default:
return err.Error()
}
}
func (e TransientUnboxingError) ToStatus() (status keybase1.Status) {
if ee, ok := e.inner.(libkb.ExportableError); ok {
status = ee.ToStatus()
status.Desc = e.Error()
} else {
status = keybase1.Status{
Name: "GENERIC",
Code: libkb.SCGeneric,
Desc: e.Error(),
}
}
return status
}
// =============================================================================
type EphemeralAlreadyExpiredError struct{}
func NewEphemeralAlreadyExpiredError() EphemeralAlreadyExpiredError {
return EphemeralAlreadyExpiredError{}
}
func (e EphemeralAlreadyExpiredError) Error() string {
return "Exploding message is expired"
}
func (e EphemeralAlreadyExpiredError) InternalError() string {
return e.Error()
}
// =============================================================================
type EphemeralUnboxingError struct {
inner ephemeral.EphemeralKeyError
}
func NewEphemeralUnboxingError(inner ephemeral.EphemeralKeyError) EphemeralUnboxingError {
return EphemeralUnboxingError{inner}
}
func (e EphemeralUnboxingError) Error() string {
return e.inner.HumanError()
}
func (e EphemeralUnboxingError) InternalError() string {
return e.inner.Error()
}
// =============================================================================
type PublicTeamEphemeralKeyError struct{}
func NewPublicTeamEphemeralKeyError() PublicTeamEphemeralKeyError {
return PublicTeamEphemeralKeyError{}
}
func (e PublicTeamEphemeralKeyError) Error() string {
return "Cannot use exploding messages for a public team."
}
// =============================================================================
type NotAuthenticatedForThisDeviceError struct{ inner ephemeral.EphemeralKeyError }
func NewNotAuthenticatedForThisDeviceError(mctx libkb.MetaContext, memberCtime *keybase1.Time,
contentCtime gregor1.Time) NotAuthenticatedForThisDeviceError {
inner := ephemeral.NewNotAuthenticatedForThisDeviceError(mctx, memberCtime, contentCtime)
return NotAuthenticatedForThisDeviceError{inner: inner}
}
func (e NotAuthenticatedForThisDeviceError) Error() string {
return e.inner.HumanError()
}
func (e NotAuthenticatedForThisDeviceError) InternalError() string {
return e.inner.Error()
}
// =============================================================================
type InvalidMACError struct{}
func NewInvalidMACError() InvalidMACError {
return InvalidMACError{}
}
func (e InvalidMACError) Error() string {
return "invalid MAC"
}
// =============================================================================
type ConsistencyErrorCode int
const (
DuplicateID ConsistencyErrorCode = iota
OutOfOrderID
InconsistentHash
IncorrectHash
)
type ChatThreadConsistencyError interface {
error
Code() ConsistencyErrorCode
}
type chatThreadConsistencyErrorImpl struct {
msg string
code ConsistencyErrorCode
}
func (e chatThreadConsistencyErrorImpl) Error() string {
return e.msg
}
func (e chatThreadConsistencyErrorImpl) Code() ConsistencyErrorCode {
return e.code
}
func NewChatThreadConsistencyError(code ConsistencyErrorCode, msg string, formatArgs ...interface{}) ChatThreadConsistencyError {
return &chatThreadConsistencyErrorImpl{
code: code,
msg: fmt.Sprintf(msg, formatArgs...),
}
}
// =============================================================================
type BoxingError struct {
Msg string
Perm bool
}
func NewBoxingError(msg string, perm bool) BoxingError {
return BoxingError{
Msg: msg,
Perm: perm,
}
}
func (e BoxingError) Error() string {
return fmt.Sprintf("encryption error: %s perm: %v", e.Msg, e.Perm)
}
func (e BoxingError) IsImmediateFail() (chat1.OutboxErrorType, bool) {
if e.Perm {
return chat1.OutboxErrorType_MISC, true
}
return 0, false
}
// =============================================================================
type RestrictedBotChannelError struct{}
func NewRestrictedBotChannelError() RestrictedBotChannelError {
return RestrictedBotChannelError{}
}
func (e RestrictedBotChannelError) Error() string {
return "bot restricted from sending to this channel"
}
func (e RestrictedBotChannelError) IsImmediateFail() (chat1.OutboxErrorType, bool) {
return chat1.OutboxErrorType_RESTRICTEDBOT, true
}
// =============================================================================
type BoxingCryptKeysError struct {
Err error
}
// Cause implements the pkg/errors Cause() method, also cloned in libkb via HumanError,
// so that we know which error to show to the human being using keybase (rather than
// for our own internal uses).
func (e BoxingCryptKeysError) Cause() error {
return e.Err
}
func NewBoxingCryptKeysError(err error) BoxingCryptKeysError {
return BoxingCryptKeysError{
Err: err,
}
}
func (e BoxingCryptKeysError) Error() string {
return fmt.Sprintf("boxing error: unable to get crypt keys: %s", e.Err.Error())
}
func (e BoxingCryptKeysError) Inner() error {
return e.Err
}
func (e BoxingCryptKeysError) IsImmediateFail() (chat1.OutboxErrorType, bool) {
if _, ok := e.Err.(libkb.IdentifySummaryError); ok {
return chat1.OutboxErrorType_IDENTIFY, true
}
return 0, false
}
// =============================================================================
type BodyHashInvalid struct{}
func (e BodyHashInvalid) Error() string {
return "chat body hash invalid"
}
type VersionError struct {
Kind string
Version int
Critical bool
}
func (e VersionError) Error() string {
return fmt.Sprintf("Unable to decrypt because current client is out of date. Please update your version of Keybase! Chat version error: [ unhandled: %s version: %d critical: %v ]", e.Kind, e.Version, e.Critical)
}
func (e VersionError) ExportType() chat1.MessageUnboxedErrorType {
if e.Critical {
return chat1.MessageUnboxedErrorType_BADVERSION_CRITICAL
}
return chat1.MessageUnboxedErrorType_BADVERSION
}
func (e VersionError) VersionKind() chat1.VersionKind {
return chat1.VersionKind(e.Kind)
}
func (e VersionError) VersionNumber() int {
return e.Version
}
func (e VersionError) IsCritical() bool {
return e.Critical
}
func NewMessageBoxedVersionError(version chat1.MessageBoxedVersion) VersionError {
return VersionError{
Kind: string(chat1.VersionErrorMessageBoxed),
Version: int(version),
Critical: true,
}
}
func NewHeaderVersionError(version chat1.HeaderPlaintextVersion,
defaultHeader chat1.HeaderPlaintextUnsupported) VersionError {
return VersionError{
Kind: string(chat1.VersionErrorHeader),
Version: int(version),
Critical: defaultHeader.Mi.Crit,
}
}
func NewBodyVersionError(version chat1.BodyPlaintextVersion, defaultBody chat1.BodyPlaintextUnsupported) VersionError {
return VersionError{
Kind: string(chat1.VersionErrorBody),
Version: int(version),
Critical: defaultBody.Mi.Crit,
}
}
// =============================================================================
type HeaderMismatchError struct {
Field string
}
var _ error = (*HeaderMismatchError)(nil)
func (e HeaderMismatchError) Error() string {
return fmt.Sprintf("chat header mismatch on %q", e.Field)
}
func NewHeaderMismatchError(field string) HeaderMismatchError {
return HeaderMismatchError{Field: field}
}
// =============================================================================
type OfflineError struct {
}
func (e OfflineError) Error() string {
return "operation failed: no connection to chat server"
}
type OfflineClient struct {
}
func (e OfflineClient) Call(ctx context.Context, method string, arg interface{},
res interface{}, timeout time.Duration) error {
return OfflineError{}
}
func (e OfflineClient) CallCompressed(ctx context.Context, method string, arg interface{},
res interface{}, ctype rpc.CompressionType, timeout time.Duration) error {
return OfflineError{}
}
func (e OfflineClient) Notify(ctx context.Context, method string, arg interface{}, timeout time.Duration) error {
return OfflineError{}
}
// =============================================================================
type DuplicateTopicNameError struct {
Conv chat1.ConversationLocal
}
func (e DuplicateTopicNameError) Error() string {
return fmt.Sprintf("channel name %s is already in use in %v",
e.Conv.GetTopicName(), e.Conv.Info.TlfName)
}
// =============================================================================
type ImpteamBadteamError struct {
Msg string
}
func (e ImpteamBadteamError) Error() string {
return fmt.Sprintf("bad iteam found in conv: %s", e.Msg)
}
// =============================================================================
type UnknownTLFNameError struct {
tlfName string
}
func NewUnknownTLFNameError(name string) UnknownTLFNameError {
return UnknownTLFNameError{
tlfName: name,
}
}
func (e UnknownTLFNameError) Error() string {
return fmt.Sprintf("unknown conversation name: %s", e.tlfName)
}
// =============================================================================
type AttachmentUploadError struct {
Msg string
Perm bool
}
func NewAttachmentUploadError(msg string, perm bool) AttachmentUploadError {
return AttachmentUploadError{
Msg: msg,
Perm: perm,
}
}
func (e AttachmentUploadError) Error() string {
return fmt.Sprintf("attachment failed to upload; %s", e.Msg)
}
func (e AttachmentUploadError) IsImmediateFail() (chat1.OutboxErrorType, bool) {
return chat1.OutboxErrorType_MISC, e.Perm
}
// =============================================================================
type SenderTestImmediateFailError struct {
}
func (e SenderTestImmediateFailError) Error() string {
return "sender test immediate fail error"
}
func (e SenderTestImmediateFailError) IsImmediateFail() (chat1.OutboxErrorType, bool) {
return chat1.OutboxErrorType_MISC, true
}
// =============================================================================
type DecryptionKeyNotFoundError struct {
generation int
kbfsEncrypted, public bool
}
func NewDecryptionKeyNotFoundError(generation int, public, kbfsEncrypted bool) DecryptionKeyNotFoundError {
return DecryptionKeyNotFoundError{
generation: generation,
kbfsEncrypted: kbfsEncrypted,
public: public,
}
}
func (e DecryptionKeyNotFoundError) Error() string {
return fmt.Sprintf("decryption key not found for generation: %v kbfsEncrypted: %v public: %v",
e.generation, e.kbfsEncrypted, e.public)
}
// =============================================================================
type OfflineErrorKind int
const (
OfflineErrorKindOnline OfflineErrorKind = iota
OfflineErrorKindOfflineBasic
OfflineErrorKindOfflineReconnect
)
func IsOfflineError(err error) OfflineErrorKind {
// Check type
switch terr := err.(type) {
case net.Error:
return OfflineErrorKindOfflineReconnect
case libkb.APINetError:
return OfflineErrorKindOfflineBasic
case OfflineError:
return OfflineErrorKindOfflineBasic
case TransientUnboxingError:
return IsOfflineError(terr.Inner())
}
// Check error itself
switch err {
case context.DeadlineExceeded:
fallthrough
case ErrChatServerTimeout:
return OfflineErrorKindOfflineReconnect
case ErrDuplicateConnection:
return OfflineErrorKindOfflineBasic
}
// Unfortunately, Go throws these without a type and they can occasionally
// propagate up. The strings were copied from
// https://golang.org/src/crypto/tls/conn.go
switch err.Error() {
case "tls: use of closed connection",
"tls: protocol is shutdown":
return OfflineErrorKindOfflineReconnect
}
return OfflineErrorKindOnline
}
func IsRekeyError(err error) (typ chat1.ConversationErrorType, ok bool) {
switch err := err.(type) {
case types.UnboxingError:
return IsRekeyError(err.Inner())
case libkb.NeedSelfRekeyError:
return chat1.ConversationErrorType_SELFREKEYNEEDED, true
case libkb.NeedOtherRekeyError:
return chat1.ConversationErrorType_OTHERREKEYNEEDED, true
default:
if teams.IsTeamReadError(err) {
return chat1.ConversationErrorType_OTHERREKEYNEEDED, true
}
}
return chat1.ConversationErrorType_NONE, false
}
// =============================================================================
type FTLError struct {
msg string
}
func NewFTLError(s string) error {
return &FTLError{msg: s}
}
func (f FTLError) Error() string {
return fmt.Sprintf("FTL Error: %s", f.msg)
}
// =============================================================================
type DevStoragePermissionDeniedError struct {
role keybase1.TeamRole
}
func NewDevStoragePermissionDeniedError(role keybase1.TeamRole) error {
return &DevStoragePermissionDeniedError{role: role}
}
func (e *DevStoragePermissionDeniedError) Error() string {
return fmt.Sprintf("role %q is not high enough", e.role)
}
// =============================================================================
type DevStorageAdminOnlyError struct {
msg string
}
func NewDevStorageAdminOnlyError(msg string) error {
return &DevStorageAdminOnlyError{msg: msg}
}
func (e *DevStorageAdminOnlyError) Error() string {
return fmt.Sprintf("found a conversation and a message, but role checking failed: %s", e.msg)
}