~samwhited/xmpp

12e35d55ff3bb71a6bf93b73c1fbcd789c0064c6 — Sam Whited 9 months ago affc897
stanza: add Error method on IQ

This provides an easy way to reply to IQs with an error, similar to
Result.

Signed-off-by: Sam Whited <sam@samwhited.com>
2 files changed, 10 insertions(+), 0 deletions(-)

M CHANGELOG.md
M stanza/iq.go
M CHANGELOG.md => CHANGELOG.md +1 -0
@@ 24,6 24,7 @@ All notable changes to this project will be documented in this file.
- receipts: add `Request` and `Requested` to add receipt requests to messages
  without waiting on a response
- roster: add `Set` and `Delete` functions for roster management
- stanza: add `Error` method on the `IQ` type
- stream: new `InnerXML` and `ApplicationError` methods on `Error` provide a way
  to easily construct customized stream errors
- styling: add `Disable` and `Unstyled` to support disabling styling on some

M stanza/iq.go => stanza/iq.go +9 -0
@@ 108,6 108,15 @@ func (iq IQ) Result(payload xml.TokenReader) xml.TokenReader {
	return iq.Wrap(payload)
}

// Error returns a token reader that wraps the first element from payload in an
// IQ stanza with the to and from attributes switched and the type set to
// ErrorIQ.
func (iq IQ) Error(err Error) xml.TokenReader {
	iq.Type = ErrorIQ
	iq.From, iq.To = iq.To, iq.From
	return iq.Wrap(err.TokenReader())
}

// IQType is the type of an IQ stanza.
// It should normally be one of the constants defined in this package.
type IQType string