~samwhited/xmpp

4eda47928d57a568a0b9a7c7fc5ae345d599c9b7 — Sam Whited 5 years ago ff77410
Move application error type into base xmpp package
5 files changed, 24 insertions(+), 23 deletions(-)

R errors/errors.go => errors.go
R errors/doc.go => errors_test.go
R errors/example_test.go => example_test.go
M stream/example_test.go
M stream/streamerror.go
R errors/errors.go => errors.go +11 -12
@@ 2,7 2,7 @@
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

package errors
package xmpp

import (
	"encoding/xml"


@@ 10,22 10,21 @@ import (

// New returns an error that formats as the given text and marshals with the
// given XML name and with the text as chardata.
func New(name xml.Name, text string) error {
	return &ErrorXML{
		XMLName:  name,
		CharData: text,
	}
}
// func Error(name xml.Name, text string) error {
// 	return &ErrorXML{
// 		XMLName:  name,
// 		CharData: text,
// 	}
// }

// ErrorXML is a trivial implementation of error intended to be marshalable and
// Error is a trivial implementation of error intended to be marshalable and
// unmarshalable as XML.
type ErrorXML struct {
type Error struct {
	XMLName  xml.Name
	InnerXML string `xml:",innerxml"`
	CharData string `xml:",chardata"`
}

// Satisfies the error interface and returns the error string.
func (e *ErrorXML) Error() string {
	return e.CharData
func (e Error) Error() string {
	return e.InnerXML
}

R errors/doc.go => errors_test.go +4 -2
@@ 2,5 2,7 @@
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

// Package errors provides application level XMPP errors.
package errors // import "bitbucket.org/mellium/xmpp/errors"
package xmpp

var _ error = (*Error)(nil)
var _ error = Error{}

R errors/example_test.go => example_test.go +4 -4
@@ 2,18 2,18 @@
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

package errors
package xmpp

import (
	"encoding/xml"
	"fmt"
)

func ExampleNew() {
	e := New(
func ExampleError() {
	e := Error{
		xml.Name{"http://example.net", "comedy"},
		"There was a comedy of errors.",
	)
	}
	b, _ := xml.Marshal(e)
	fmt.Println(string(b))
	// Output:

M stream/example_test.go => stream/example_test.go +2 -2
@@ 9,7 9,7 @@ import (
	"encoding/xml"
	"fmt"

	"bitbucket.org/mellium/xmpp/errors"
	"bitbucket.org/mellium/xmpp"
)

func ExampleStreamError_UnmarshalXML() {


@@ 36,7 36,7 @@ func ExampleStreamError_MarshalXML() {
}

func ExampleUndefinedConditionError() {
	apperr := errors.New(xml.Name{"http://example.org/ns", "app-error"}, "")
	apperr := xmpp.Error{xml.Name{"http://example.org/ns", "app-error"}, ""}
	e := UndefinedConditionError(apperr)
	b, _ := xml.MarshalIndent(e, "", "  ")
	fmt.Println(string(b))

M stream/streamerror.go => stream/streamerror.go +3 -3
@@ 8,7 8,7 @@ import (
	"encoding/xml"
	"net"

	"bitbucket.org/mellium/xmpp/errors"
	"bitbucket.org/mellium/xmpp"
)

// A list of stream errors defined in RFC 6120 §4.9.3


@@ 161,10 161,10 @@ func UndefinedConditionError(e error) StreamError {

	var b []byte
	switch e := e.(type) {
	case *xmpp.Error, xmpp.Error:
		b, _ = xml.Marshal(e)
	default:
		b = []byte(e.Error())
	case *errors.ErrorXML:
		b, _ = xml.Marshal(e)
	}

	return StreamError{"undefined-condition", b}