-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
485 lines (434 loc) · 15 KB
/
error.rs
File metadata and controls
485 lines (434 loc) · 15 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
//! Error types for codec operations.
use std::fmt;
use schema::{ComponentId, FieldId};
/// Result type for codec operations.
pub type CodecResult<T> = Result<T, CodecError>;
/// Errors that can occur during snapshot/delta encoding/decoding.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CodecError {
/// Wire format error.
Wire(wire::DecodeError),
/// Bitstream error.
Bitstream(bitstream::BitError),
/// Output buffer is too small.
OutputTooSmall { needed: usize, available: usize },
/// Schema hash mismatch.
SchemaMismatch { expected: u64, found: u64 },
/// Limits exceeded.
LimitsExceeded {
kind: LimitKind,
limit: usize,
actual: usize,
},
/// Invalid mask data.
InvalidMask { kind: MaskKind, reason: MaskReason },
/// Invalid field value for the schema.
InvalidValue {
component: ComponentId,
field: FieldId,
reason: ValueReason,
},
/// Entities are not provided in deterministic order.
InvalidEntityOrder { previous: u32, current: u32 },
/// Section body had trailing bits after parsing.
TrailingSectionData {
section: wire::SectionTag,
remaining_bits: usize,
},
/// Unexpected section for the current packet type.
UnexpectedSection { section: wire::SectionTag },
/// Duplicate section encountered.
DuplicateSection { section: wire::SectionTag },
/// Multiple update encodings present in one packet.
DuplicateUpdateEncoding,
/// Baseline tick does not match the packet.
BaselineTickMismatch { expected: u32, found: u32 },
/// Baseline tick not found in history.
BaselineNotFound {
/// The requested baseline tick.
requested_tick: u32,
},
/// Entity not found when applying delta.
EntityNotFound {
/// The missing entity ID.
entity_id: u32,
},
/// Component not found when applying delta.
ComponentNotFound {
/// The entity ID.
entity_id: u32,
/// The missing component ID.
component_id: u16,
},
/// Duplicate entity in create section.
DuplicateEntity {
/// The duplicate entity ID.
entity_id: u32,
},
/// Entity already exists when creating.
EntityAlreadyExists {
/// The existing entity ID.
entity_id: u32,
},
/// Missing session state for compact packets.
SessionMissing,
/// Session init packet was invalid.
SessionInitInvalid,
/// Session compact header mode is unsupported.
SessionUnsupportedMode { mode: u8 },
/// Session packets arrived out of order.
SessionOutOfOrder { previous: u32, current: u32 },
}
impl CodecError {
/// Returns `true` if the error indicates the client should resync via a
/// session init + full snapshot.
#[must_use]
pub fn needs_resync(&self) -> bool {
matches!(
self,
Self::SessionMissing
| Self::SessionInitInvalid
| Self::SessionUnsupportedMode { .. }
| Self::SessionOutOfOrder { .. }
| Self::SchemaMismatch { .. }
| Self::BaselineNotFound { .. }
| Self::BaselineTickMismatch { .. }
| Self::Wire(wire::DecodeError::InvalidBaselineTick { .. })
| Self::Wire(wire::DecodeError::InvalidFlags { .. })
)
}
}
/// Specific limit that was exceeded.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LimitKind {
EntitiesCreate,
EntitiesUpdate,
EntitiesDestroy,
TotalEntitiesAfterApply,
ComponentsPerEntity,
FieldsPerComponent,
SectionBytes,
}
/// Mask validation error kinds.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MaskKind {
ComponentMask,
FieldMask { component: ComponentId },
}
/// Details for invalid mask errors.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MaskReason {
NotEnoughBits { expected: usize, available: usize },
FieldCountMismatch { expected: usize, actual: usize },
MissingField { field: FieldId },
UnknownComponent { component: ComponentId },
InvalidComponentId { raw: u16 },
InvalidFieldIndex { field_index: usize, max: usize },
ComponentPresenceMismatch { component: ComponentId },
EmptyFieldMask { component: ComponentId },
}
/// Details for invalid value errors.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ValueReason {
UnsignedOutOfRange {
bits: u8,
value: u64,
},
SignedOutOfRange {
bits: u8,
value: i64,
},
VarUIntOutOfRange {
value: u64,
},
VarSIntOutOfRange {
value: i64,
},
FixedPointOutOfRange {
min_q: i64,
max_q: i64,
value: i64,
},
TypeMismatch {
expected: &'static str,
found: &'static str,
},
}
impl fmt::Display for CodecError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Wire(e) => write!(f, "wire error: {e}"),
Self::Bitstream(e) => write!(f, "bitstream error: {e}"),
Self::OutputTooSmall { needed, available } => {
write!(f, "output too small: need {needed}, have {available}")
}
Self::SchemaMismatch { expected, found } => {
write!(
f,
"schema hash mismatch: expected 0x{expected:016X}, found 0x{found:016X}"
)
}
Self::LimitsExceeded {
kind,
limit,
actual,
} => {
write!(f, "{kind} limit exceeded: {actual} > {limit}")
}
Self::InvalidMask { kind, reason } => {
write!(f, "invalid {kind}: {reason}")
}
Self::InvalidValue {
component,
field,
reason,
} => {
write!(f, "invalid value for {component:?}:{field:?}: {reason}")
}
Self::InvalidEntityOrder { previous, current } => {
write!(f, "entity order invalid: {previous} then {current}")
}
Self::TrailingSectionData {
section,
remaining_bits,
} => {
write!(
f,
"trailing data in section {section:?}: {remaining_bits} bits"
)
}
Self::UnexpectedSection { section } => {
write!(f, "unexpected section {section:?} in full snapshot")
}
Self::DuplicateSection { section } => {
write!(f, "duplicate section {section:?} in packet")
}
Self::DuplicateUpdateEncoding => {
write!(f, "multiple update encodings present in packet")
}
Self::BaselineTickMismatch { expected, found } => {
write!(
f,
"baseline tick mismatch: expected {expected}, found {found}"
)
}
Self::BaselineNotFound { requested_tick } => {
write!(f, "baseline tick {requested_tick} not found in history")
}
Self::EntityNotFound { entity_id } => {
write!(f, "entity {entity_id} not found")
}
Self::ComponentNotFound {
entity_id,
component_id,
} => {
write!(
f,
"component {component_id} not found on entity {entity_id}"
)
}
Self::DuplicateEntity { entity_id } => {
write!(f, "duplicate entity {entity_id} in create section")
}
Self::EntityAlreadyExists { entity_id } => {
write!(f, "entity {entity_id} already exists")
}
Self::SessionMissing => {
write!(f, "session state missing for compact packet")
}
Self::SessionInitInvalid => {
write!(f, "session init packet invalid")
}
Self::SessionUnsupportedMode { mode } => {
write!(f, "session compact mode {mode} unsupported")
}
Self::SessionOutOfOrder { previous, current } => {
write!(f, "session packet out of order: {previous} then {current}")
}
}
}
}
impl fmt::Display for LimitKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let name = match self {
Self::EntitiesCreate => "entities",
Self::EntitiesUpdate => "update entities",
Self::EntitiesDestroy => "destroy entities",
Self::TotalEntitiesAfterApply => "total entities",
Self::ComponentsPerEntity => "components per entity",
Self::FieldsPerComponent => "fields per component",
Self::SectionBytes => "section bytes",
};
write!(f, "{name}")
}
}
impl fmt::Display for MaskKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ComponentMask => write!(f, "component mask"),
Self::FieldMask { component } => write!(f, "field mask for {component:?}"),
}
}
}
impl fmt::Display for MaskReason {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotEnoughBits {
expected,
available,
} => {
write!(f, "need {expected} bits, have {available}")
}
Self::FieldCountMismatch { expected, actual } => {
write!(f, "expected {expected} fields, got {actual}")
}
Self::MissingField { field } => {
write!(f, "missing field {field:?} in full snapshot")
}
Self::UnknownComponent { component } => {
write!(f, "unknown component {component:?} in snapshot")
}
Self::InvalidComponentId { raw } => {
write!(f, "invalid component id {raw} in snapshot")
}
Self::InvalidFieldIndex { field_index, max } => {
write!(f, "field index {field_index} exceeds max {max}")
}
Self::ComponentPresenceMismatch { component } => {
write!(f, "component presence mismatch for {component:?}")
}
Self::EmptyFieldMask { component } => {
write!(f, "empty field mask for {component:?} is invalid")
}
}
}
}
impl fmt::Display for ValueReason {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnsignedOutOfRange { bits, value } => {
write!(f, "unsigned value {value} does not fit in {bits} bits")
}
Self::SignedOutOfRange { bits, value } => {
write!(f, "signed value {value} does not fit in {bits} bits")
}
Self::VarUIntOutOfRange { value } => {
write!(f, "varuint value {value} exceeds u32::MAX")
}
Self::VarSIntOutOfRange { value } => {
write!(f, "varsint value {value} exceeds i32 range")
}
Self::FixedPointOutOfRange {
min_q,
max_q,
value,
} => {
write!(f, "fixed-point value {value} outside [{min_q}, {max_q}]")
}
Self::TypeMismatch { expected, found } => {
write!(f, "expected {expected} but got {found}")
}
}
}
}
impl std::error::Error for CodecError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Wire(e) => Some(e),
Self::Bitstream(e) => Some(e),
_ => None,
}
}
}
impl From<wire::DecodeError> for CodecError {
fn from(err: wire::DecodeError) -> Self {
Self::Wire(err)
}
}
impl From<bitstream::BitError> for CodecError {
fn from(err: bitstream::BitError) -> Self {
Self::Bitstream(err)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn error_display_baseline_not_found() {
let err = CodecError::BaselineNotFound { requested_tick: 42 };
let msg = err.to_string();
assert!(msg.contains("42"), "should mention tick");
assert!(msg.contains("baseline"), "should mention baseline");
}
#[test]
fn error_display_entity_not_found() {
let err = CodecError::EntityNotFound { entity_id: 123 };
let msg = err.to_string();
assert!(msg.contains("123"), "should mention entity id");
}
#[test]
fn error_display_component_not_found() {
let err = CodecError::ComponentNotFound {
entity_id: 10,
component_id: 5,
};
let msg = err.to_string();
assert!(msg.contains("10"), "should mention entity id");
assert!(msg.contains('5'), "should mention component id");
}
#[test]
fn error_display_duplicate_entity() {
let err = CodecError::DuplicateEntity { entity_id: 42 };
let msg = err.to_string();
assert!(msg.contains("42"), "should mention entity id");
assert!(msg.contains("duplicate"), "should mention duplicate");
}
#[test]
fn error_display_entity_already_exists() {
let err = CodecError::EntityAlreadyExists { entity_id: 99 };
let msg = err.to_string();
assert!(msg.contains("99"), "should mention entity id");
assert!(msg.contains("exists"), "should mention exists");
}
#[test]
fn error_from_wire_error() {
let wire_err = wire::DecodeError::InvalidMagic { found: 0x1234 };
let codec_err: CodecError = wire_err.into();
assert!(matches!(codec_err, CodecError::Wire(_)));
}
#[test]
fn error_from_bitstream_error() {
let bit_err = bitstream::BitError::UnexpectedEof {
requested: 1,
available: 0,
};
let codec_err: CodecError = bit_err.into();
assert!(matches!(codec_err, CodecError::Bitstream(_)));
}
#[test]
fn error_source_wire() {
let wire_err = wire::DecodeError::InvalidMagic { found: 0x1234 };
let codec_err = CodecError::Wire(wire_err);
let source = std::error::Error::source(&codec_err);
assert!(source.is_some(), "should have a source");
}
#[test]
fn error_source_none_for_others() {
let err = CodecError::EntityNotFound { entity_id: 1 };
let source = std::error::Error::source(&err);
assert!(source.is_none(), "non-wrapped errors should have no source");
}
#[test]
fn error_equality() {
let err1 = CodecError::EntityNotFound { entity_id: 42 };
let err2 = CodecError::EntityNotFound { entity_id: 42 };
let err3 = CodecError::EntityNotFound { entity_id: 43 };
assert_eq!(err1, err2);
assert_ne!(err1, err3);
}
#[test]
fn error_is_std_error() {
fn assert_error<E: std::error::Error>() {}
assert_error::<CodecError>();
}
}