github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/chat/flip/errors.go (about) 1 package flip 2 3 import ( 4 "errors" 5 "fmt" 6 "strings" 7 8 chat1 "github.com/keybase/client/go/protocol/chat1" 9 ) 10 11 type Error struct { 12 NoCommitments []Player 13 NoReveals []Player 14 BadCommitments []Player 15 Duplicates []Player 16 } 17 18 func (e *Error) addNoCommitment(p Player) { 19 e.NoCommitments = append(e.NoCommitments, p) 20 } 21 22 func (e *Error) addNoReveal(p Player) { 23 e.NoReveals = append(e.NoReveals, p) 24 } 25 26 func (e *Error) addBadCommitment(p Player) { 27 e.BadCommitments = append(e.BadCommitments, p) 28 } 29 30 func (e *Error) addDuplicate(p Player) { 31 e.Duplicates = append(e.Duplicates, p) 32 } 33 34 func (e Error) IsNil() bool { 35 return len(e.NoCommitments)+len(e.NoReveals)+len(e.BadCommitments) == 0 36 } 37 38 func (e Error) format(out []string, what string, players []Player) []string { 39 if len(players) == 0 { 40 return out 41 } 42 var playerStrings []string 43 for _, p := range players { 44 playerStrings = append(playerStrings, string(p)) 45 } 46 s := fmt.Sprintf("Players %s: %s", what, strings.Join(playerStrings, ",")) 47 return append(out, s) 48 } 49 50 func (e Error) Error() string { 51 var parts []string 52 parts = e.format(parts, "without commitments", e.NoCommitments) 53 parts = e.format(parts, "without reveals", e.NoReveals) 54 parts = e.format(parts, "with bad commitments", e.BadCommitments) 55 parts = e.format(parts, "with duplicated IDs", e.Duplicates) 56 return fmt.Sprintf("Errors in flip: %s", strings.Join(parts, ";")) 57 } 58 59 type GameAlreadyStartedError struct { 60 G GameMetadata 61 } 62 63 func (g GameAlreadyStartedError) Error() string { 64 return fmt.Sprintf("Game already started: %s", g.G) 65 } 66 67 type GameFinishedError struct { 68 G GameMetadata 69 } 70 71 func (g GameFinishedError) Error() string { 72 return fmt.Sprintf("Game is finished: %s", g.G) 73 } 74 75 type TimeoutError struct { 76 G GameMetadata 77 Stage Stage 78 } 79 80 type GameAbortedError struct{} 81 82 func (g GameAbortedError) Error() string { 83 return "game was aborted before it yielded a result or any reveals" 84 } 85 86 func (t TimeoutError) Error() string { 87 return fmt.Sprintf("Game %s timed out in stage: %d", t.G, t.Stage) 88 } 89 90 type BadMessageForStageError struct { 91 G GameMetadata 92 MessageType MessageType 93 Stage Stage 94 } 95 96 func (b BadMessageForStageError) Error() string { 97 return fmt.Sprintf("Message received (%s) was for wrong stage (%s) for game %s", b.G, b.MessageType, b.Stage) 98 } 99 100 type BadVersionError Version 101 102 func (b BadVersionError) Error() string { 103 return fmt.Sprintf("Bad version %d: can only handle V1", b) 104 } 105 106 type BadUserDeviceError struct { 107 Expected UserDevice 108 Actual UserDevice 109 } 110 111 func (b BadUserDeviceError) Error() string { 112 return "Bad user device; didn't match expectations" 113 } 114 115 type DuplicateRegistrationError struct { 116 G GameMetadata 117 U UserDevice 118 } 119 120 func (d DuplicateRegistrationError) Error() string { 121 return fmt.Sprintf("User %s registered more than once in game %s", d.U.ToKey(), d.G) 122 } 123 124 type DuplicateCommitmentCompleteError struct { 125 G GameMetadata 126 U UserDevice 127 } 128 129 func (d DuplicateCommitmentCompleteError) Error() string { 130 return fmt.Sprintf("Initiator announced a duplicate commitment user %s in game %s", d.U.ToKey(), d.G) 131 } 132 133 type WrongSenderError struct { 134 G GameMetadata 135 Expected UserDevice 136 Actual UserDevice 137 } 138 139 func (w WrongSenderError) Error() string { 140 return fmt.Sprintf("For game %s, wrong message sender; wanted %s but got %s", 141 w.G, w.Expected.ToKey(), w.Actual.ToKey()) 142 } 143 144 type BadRevealError struct { 145 G GameMetadata 146 U UserDevice 147 } 148 149 func (b BadRevealError) Error() string { 150 return fmt.Sprintf("In game %s, bad reveal for %s", b.G, b.U.ToKey()) 151 } 152 153 type DuplicateRevealError struct { 154 G GameMetadata 155 U UserDevice 156 } 157 158 func (d DuplicateRevealError) Error() string { 159 return fmt.Sprintf("In game %s, duplicated reveal for %s", d.G, d.U.ToKey()) 160 } 161 162 type NoRevealError struct { 163 G GameMetadata 164 U UserDevice 165 } 166 167 func (n NoRevealError) Error() string { 168 return fmt.Sprintf("In game %s, no reveal fo %s", n.G, n.U.ToKey()) 169 } 170 171 type BadLocalClockError struct { 172 G GameMetadata 173 } 174 175 func (b BadLocalClockError) Error() string { 176 return fmt.Sprintf("Cannot particpate in game %s due to local clock skew", b.G) 177 } 178 179 type BadLeaderClockError struct { 180 G GameMetadata 181 } 182 183 func (b BadLeaderClockError) Error() string { 184 return fmt.Sprintf("Cannot particpate in game %s due to leader's clock skew", b.G) 185 } 186 187 type GameReplayError struct { 188 G chat1.FlipGameID 189 } 190 191 func (g GameReplayError) Error() string { 192 return fmt.Sprintf("GameID was replayed: %s", g.G) 193 } 194 195 type AbsenteesError struct { 196 Absentees []UserDevice 197 } 198 199 func (l AbsenteesError) Error() string { 200 return fmt.Sprintf("Flip failed! Some users didn't reveal in time (%+v)", l.Absentees) 201 } 202 203 type GameShutdownError struct { 204 G GameMetadata 205 } 206 207 func (g GameShutdownError) Error() string { 208 return fmt.Sprintf("Game was shutdown before it completed: %s", g.G) 209 } 210 211 type BadChannelError struct { 212 G GameMetadata 213 C chat1.ConversationID 214 } 215 216 func (b BadChannelError) Error() string { 217 return fmt.Sprintf("Data for game %s came in on wrong channel (%s)", b.G, b.C) 218 } 219 220 type UnforwardableMessageError struct { 221 G GameMetadata 222 } 223 224 func (u UnforwardableMessageError) Error() string { 225 return fmt.Sprintf("Refusing to forward a mesasge that isn't forwardable (%s)", u.G) 226 } 227 228 type BadMessageError struct { 229 G GameMetadata 230 T MessageType 231 } 232 233 func (b BadMessageError) Error() string { 234 return fmt.Sprintf("Bad message of type %d received for game %s", b.T, b.G) 235 } 236 237 type BadFlipTypeError struct { 238 G GameMetadata 239 T FlipType 240 } 241 242 func (b BadFlipTypeError) Error() string { 243 return fmt.Sprintf("Bad flip type %d for game %s", b.T, b.G) 244 } 245 246 var ErrBadData = errors.New("rejecting bad data, likely due to a nil field") 247 248 type BadGameIDError struct { 249 G GameMetadata 250 I chat1.FlipGameID 251 } 252 253 func (b BadGameIDError) Error() string { 254 return fmt.Sprintf("Bad game ID (%s) on incoming message for %s", b.I, b.G) 255 } 256 257 type CommitmentMismatchError struct { 258 G GameMetadata 259 U UserDevice 260 } 261 262 func (c CommitmentMismatchError) Error() string { 263 return fmt.Sprintf("Commitment wasn't correct for user %s in game %s", c.U, c.G) 264 } 265 266 type CommitmentCompleteSortError struct { 267 G GameMetadata 268 } 269 270 func (c CommitmentCompleteSortError) Error() string { 271 return "Commitment list wasn't sorted properly; the leader is cheating!" 272 } 273 274 type BadCommitmentCompleteHashError struct { 275 G GameMetadata 276 U UserDevice 277 } 278 279 func (b BadCommitmentCompleteHashError) Error() string { 280 return fmt.Sprintf("Commitment complete hash error for game %s by user %s", b.G, b.U) 281 } 282 283 type RevealTooLateError struct { 284 G GameMetadata 285 U UserDevice 286 } 287 288 func (b RevealTooLateError) Error() string { 289 return fmt.Sprintf("Reveal from %s for get %s arrived too late", b.G, b.U) 290 } 291 292 type ReplayError struct { 293 s string 294 } 295 296 func NewReplayError(s string) ReplayError { 297 return ReplayError{s: s} 298 } 299 300 func (r ReplayError) Error() string { 301 return "" 302 } 303 304 type DuplicateCommitmentError struct{} 305 306 func (d DuplicateCommitmentError) Error() string { 307 return "Duplicated commitment, something is fishy" 308 }