From 9764d75fb610c024b7c9a9caa49333cccd89fbfe Mon Sep 17 00:00:00 2001 From: Erich Healy Date: Mon, 14 Aug 2017 14:25:33 -0700 Subject: [PATCH] Proxy ErrAttributeNotFound from clib to allow consumers to test against the error --- clib/clib.go | 2 +- clib/interface.go | 1 + dom/interface.go | 2 +- parser_test.go | 27 +++++++++++++++++++++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/clib/clib.go b/clib/clib.go index 53359a5..29c0159 100644 --- a/clib/clib.go +++ b/clib/clib.go @@ -1684,7 +1684,7 @@ func XMLElementGetAttributeNode(n PtrSource, name string) (uintptr, error) { } if prop == nil || XMLNodeType(prop._type) != AttributeNode { - return 0, errors.New("attribute not found") + return 0, ErrAttributeNotFound } return uintptr(unsafe.Pointer(prop)), nil diff --git a/clib/interface.go b/clib/interface.go index eb2ed11..f832fc4 100644 --- a/clib/interface.go +++ b/clib/interface.go @@ -54,6 +54,7 @@ const ( ) var ( + ErrAttributeNotFound = errors.New("attribute not found") ErrAttributeNameTooLong = errors.New("attribute name too long") ErrElementNameTooLong = errors.New("element name too long") ErrNamespaceURITooLong = errors.New("namespace uri too long") diff --git a/dom/interface.go b/dom/interface.go index ccce4b3..2b88d1a 100644 --- a/dom/interface.go +++ b/dom/interface.go @@ -7,7 +7,7 @@ import ( ) var ( - ErrAttributeNotFound = errors.New("attribute not found") + ErrAttributeNotFound = clib.ErrAttributeNotFound ErrInvalidNodeType = errors.New("invalid node type") ) diff --git a/parser_test.go b/parser_test.go index 88fe44f..c4bf3f4 100644 --- a/parser_test.go +++ b/parser_test.go @@ -4,6 +4,9 @@ import ( "regexp" "testing" + "github.com/lestrrat/go-libxml2/dom" + "github.com/lestrrat/go-libxml2/types" + "github.com/lestrrat/go-libxml2/clib" "github.com/lestrrat/go-libxml2/parser" "github.com/stretchr/testify/assert" @@ -330,3 +333,27 @@ func TestPiWrapNodeIssue(t *testing.T) { t.Fatalf("XML did not convert back correctly, expected: %v, got: %v", textXML, str) } } + +func TestGetNonexistentAttributeReturnsRecoverableError(t *testing.T) { + const src = `` + doc, err := ParseString(src) + if !assert.NoError(t, err, "Should parse") { + return + } + defer doc.Free() + + rootNode, err := doc.DocumentElement() + if !assert.NoError(t, err, "Should find root element") { + return + } + + el, ok := rootNode.(types.Element) + if !ok { + t.Fatalf("Root node was not an element") + } + + _, err = el.GetAttribute("non-existant") + if err != dom.ErrAttributeNotFound { + t.Fatalf("GetAttribute() error not comparable to existing library") + } +}