Skip to content

Commit 9764d75

Browse files
committed
Proxy ErrAttributeNotFound from clib to allow consumers to test against the error
1 parent d65fd00 commit 9764d75

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

clib/clib.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1684,7 +1684,7 @@ func XMLElementGetAttributeNode(n PtrSource, name string) (uintptr, error) {
16841684
}
16851685

16861686
if prop == nil || XMLNodeType(prop._type) != AttributeNode {
1687-
return 0, errors.New("attribute not found")
1687+
return 0, ErrAttributeNotFound
16881688
}
16891689

16901690
return uintptr(unsafe.Pointer(prop)), nil

clib/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const (
5454
)
5555

5656
var (
57+
ErrAttributeNotFound = errors.New("attribute not found")
5758
ErrAttributeNameTooLong = errors.New("attribute name too long")
5859
ErrElementNameTooLong = errors.New("element name too long")
5960
ErrNamespaceURITooLong = errors.New("namespace uri too long")

dom/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
var (
10-
ErrAttributeNotFound = errors.New("attribute not found")
10+
ErrAttributeNotFound = clib.ErrAttributeNotFound
1111
ErrInvalidNodeType = errors.New("invalid node type")
1212
)
1313

parser_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import (
44
"regexp"
55
"testing"
66

7+
"github.com/lestrrat/go-libxml2/dom"
8+
"github.com/lestrrat/go-libxml2/types"
9+
710
"github.com/lestrrat/go-libxml2/clib"
811
"github.com/lestrrat/go-libxml2/parser"
912
"github.com/stretchr/testify/assert"
@@ -330,3 +333,27 @@ func TestPiWrapNodeIssue(t *testing.T) {
330333
t.Fatalf("XML did not convert back correctly, expected: %v, got: %v", textXML, str)
331334
}
332335
}
336+
337+
func TestGetNonexistentAttributeReturnsRecoverableError(t *testing.T) {
338+
const src = `<?xml version="1.0"?><rootnode/>`
339+
doc, err := ParseString(src)
340+
if !assert.NoError(t, err, "Should parse") {
341+
return
342+
}
343+
defer doc.Free()
344+
345+
rootNode, err := doc.DocumentElement()
346+
if !assert.NoError(t, err, "Should find root element") {
347+
return
348+
}
349+
350+
el, ok := rootNode.(types.Element)
351+
if !ok {
352+
t.Fatalf("Root node was not an element")
353+
}
354+
355+
_, err = el.GetAttribute("non-existant")
356+
if err != dom.ErrAttributeNotFound {
357+
t.Fatalf("GetAttribute() error not comparable to existing library")
358+
}
359+
}

0 commit comments

Comments
 (0)