~samwhited/xmpp

0e39877f50900c6cae85b6fae5999f205bbc7778 — Sam Whited 3 months ago 6a6b382
stanza: return more detailed error text

Signed-off-by: Sam Whited <sam@samwhited.com>
4 files changed, 22 insertions(+), 6 deletions(-)

M CHANGELOG.md
M stanza/error.go
M stanza/error_test.go
M stanza/example_error_test.go
M CHANGELOG.md => CHANGELOG.md +2 -0
@@ 36,6 36,8 @@ All notable changes to this project will be documented in this file.
- stanza: implement [XEP-0203: Delayed Delivery]
- stanza: more general `UnmarshalError` function that doesn't focus on IQs
- stanza: add `Error` method to `Presence` and `Message`
- stanza: errors now return text if present instead of condition when converting
  to a string
- websocket: add `Negotiator` to replace the `WebSocket` option on the stream
  config
- disco: new `disco.Handler` option to configure a `mux.ServeMux` to respond to

M stanza/error.go => stanza/error.go +14 -1
@@ 346,7 346,20 @@ func (se Error) Is(target error) bool {

// Error satisfies the error interface by returning the condition.
func (se Error) Error() string {
	return string(se.Condition)
	// If there is no error message, just return the condition.
	if len(se.Text) == 0 {
		return string(se.Condition)
	}

	// Return the default language, or a different one at random if multiple were
	// provided.
	var lang, txt string
	for lang, txt = range se.Text {
		if lang == "" {
			break
		}
	}
	return txt
}

// TokenReader satisfies the xmlstream.Marshaler interface for Error.

M stanza/error_test.go => stanza/error_test.go +5 -4
@@ 108,13 108,14 @@ func TestCmp(t *testing.T) {
func TestErrorReturnsCondition(t *testing.T) {
	s := stanza.Error{Condition: "leprosy"}
	if string(s.Condition) != s.Error() {
		t.Errorf("Expected stanza error to return condition `leprosy` but got %s", s.Error())
		t.Errorf("expected stanza error to return condition `leprosy` but got %s", s.Error())
	}
	const expected = "Text"
	s = stanza.Error{Condition: "nope", Text: map[string]string{
		"": "Text",
		"": expected,
	}}
	if string(s.Condition) != s.Error() {
		t.Errorf("Expected stanza error to return text `Text` but got %s", s.Error())
	if expected != s.Error() {
		t.Errorf("expected stanza error to return text %q but got %q", expected, s.Error())
	}
}


M stanza/example_error_test.go => stanza/example_error_test.go +1 -1
@@ 49,7 49,7 @@ func ExampleError_UnmarshalXML() {
		panic(err)
	}

	fmt.Printf("%s: %s", se, se.Text[""])
	fmt.Printf("%s: %s", se.Condition, se.Text[""])
	// Output:
	// bad-request: Malformed XML
}