We used to insert a new incoming message for errors returned by servers, but it was too noisy and confusing for users. Instead, we should attach the errors to the original outgoing message. We already have a secondary index for the outgoing elementId, so the lookup should be simple.
Additionally we should probably look closer at our XEP-0198 stream management code and mark errors on unacked stanzas.