diff --git a/CodableFirebase/Encoder.swift b/CodableFirebase/Encoder.swift index 5a2096b..69fe0ef 100644 --- a/CodableFirebase/Encoder.swift +++ b/CodableFirebase/Encoder.swift @@ -393,11 +393,20 @@ extension _FirebaseEncoder { } // The value should request a container from the _FirebaseEncoder. - let depth = storage.count - try value.encode(to: self) + let depth = self.storage.count + do { + try value.encode(to: self) + } catch { + // If the value pushed a container before throwing, pop it back off to restore state. + if self.storage.count > depth { + let _ = self.storage.popContainer() + } + + throw error + } // The top container should be a new container. - guard storage.count > depth else { + guard self.storage.count > depth else { return nil }