[gxml] Change error handling from throwing errors to setting a document-level error variable (2)



commit 078ab13462d6d477f1621c8c7a2c1516e41465f8
Author: Richard Schwarting <aquarichy gmail com>
Date:   Sat Jul 27 00:00:18 2013 -0400

    Change error handling from throwing errors to setting a document-level error variable (2)

 gxml/Document.vala              |   20 ++++++++++++++------
 gxml/NodeList.vala              |    9 +++++----
 gxml/ProcessingInstruction.vala |    2 +-
 gxml/Text.vala                  |    2 +-
 4 files changed, 21 insertions(+), 12 deletions(-)
---
diff --git a/gxml/Document.vala b/gxml/Document.vala
index 0c40f80..089c49b 100644
--- a/gxml/Document.vala
+++ b/gxml/Document.vala
@@ -242,11 +242,11 @@ namespace GXml {
                        Xml.Node *root;
 
                        if (doc == null)
-                               throw new DomError.INVALID_DOC ("Failed to parse document.");
+                               this.last_error = new DomError.INVALID_DOC ("Failed to parse document.");
                        if (require_root) {
                                root = doc->get_root_element ();
                                if (root == null)
-                                       throw new DomError.INVALID_ROOT ("Could not obtain root for 
document.");
+                                       this.last_error = new DomError.INVALID_ROOT ("Could not obtain root 
for document.");
                        }
 
                        // TODO: consider passing root as a base node?
@@ -359,7 +359,7 @@ namespace GXml {
                                this.from_stream (instream, can);
                                instream.close ();
                        } catch (GLib.Error e) {
-                               throw new DomError.INVALID_DOC (e.message);
+                               this.last_error = new DomError.INVALID_DOC (e.message);
                        }
                }
                /**
@@ -568,12 +568,12 @@ namespace GXml {
                private void check_html (string feature) throws DomError {
                        if (this.doctype != null && this.doctype.name == "html") {
                                // TODO: ^ check name == html by icase
-                               throw new DomError.NOT_SUPPORTED ("HTML documents do not support '%s'".printf 
(feature)); // i18n
+                               this.last_error = new DomError.NOT_SUPPORTED ("HTML documents do not support 
'%s'".printf (feature)); // i18n
                        }
                }
                private void check_character_validity (string str) throws DomError {
                        if (true == false) { // TODO: define validity
-                               throw new DomError.INVALID_CHARACTER ("'%s' contains invalid 
characters.".printf (str));
+                               this.last_error = new DomError.INVALID_CHARACTER ("'%s' contains invalid 
characters.".printf (str));
                        }
                }
                public override string to_string (bool format = false, int level = 0) {
@@ -600,7 +600,7 @@ namespace GXml {
                                if (xmldoc->get_root_element () == null) {
                                        xmldoc->set_root_element (((Element)new_child).node);
                                } else {
-                                       throw new DomError.HIERARCHY_REQUEST ("Document already has a root 
element.  Could not add child element with name '%s'".printf (new_child.node_name));
+                                       this.last_error = new DomError.HIERARCHY_REQUEST ("Document already 
has a root element.  Could not add child element with name '%s'".printf (new_child.node_name));
                                }
                        } else if (new_child.node_type == NodeType.DOCUMENT_TYPE) {
                                GLib.warning ("Appending document_types not yet supported");
@@ -616,5 +616,13 @@ namespace GXml {
                        // TODO: do we need to append this to this.new_nodes?  Do we need to append the 
result to this.nodes_to_free?  Test memory implications
                        return this.lookup_node (our_copy_xml); // inducing a GXmlNode
                }
+
+               /**
+                * Sets the last error encountered.
+                */
+               public DomError last_error {
+                       get;
+                       internal set;
+               }
        }
 }
diff --git a/gxml/NodeList.vala b/gxml/NodeList.vala
index e6c43c6..bed02f1 100644
--- a/gxml/NodeList.vala
+++ b/gxml/NodeList.vala
@@ -544,7 +544,7 @@ namespace GXml {
                }
 
                /** Node's child methods, implemented here **/
-               internal new DomNode? insert_before (DomNode new_child, DomNode? ref_child) throws DomError {
+               internal new DomNode? insert_before (DomNode new_child, DomNode? ref_child) {
                        Xml.Node *child = head;
 
                        if (ref_child == null) {
@@ -555,7 +555,8 @@ namespace GXml {
                                child = child->next;
                        }
                        if (child == null) {
-                               throw new DomError.NOT_FOUND ("ref_child not found.");
+                               this.owner.last_error = new DomError.NOT_FOUND ("ref_child not found.");
+                               return null;
                                // TODO: provide a more useful description of ref_child, but there are so 
many different types
                        } else {
                                if (new_child.node_type == NodeType.DOCUMENT_FRAGMENT) {
@@ -569,7 +570,7 @@ namespace GXml {
                        return new_child;
                }
 
-               internal new DomNode? replace_child (DomNode new_child, DomNode old_child) throws DomError {
+               internal new DomNode? replace_child (DomNode new_child, DomNode old_child) {
                        // TODO: verify that libxml2 already removes
                        // new_child first if it is found elsewhere in
                        // the tree.
@@ -593,7 +594,7 @@ namespace GXml {
                                        // it is a valid child
                                        child->replace (((BackedNode)new_child).node);
                                } else {
-                                       throw new DomError.NOT_FOUND ("old_child not found");
+                                       this.owner.last_error = new DomError.NOT_FOUND ("old_child not 
found");
                                        // TODO: provide more useful descr. of old_child
                                }
                        }
diff --git a/gxml/ProcessingInstruction.vala b/gxml/ProcessingInstruction.vala
index 9aac94a..b8d8065 100644
--- a/gxml/ProcessingInstruction.vala
+++ b/gxml/ProcessingInstruction.vala
@@ -60,7 +60,7 @@ namespace GXml {
                // TODO: confirm that data here is freeform attributes
                public string data /* throws DomError (not supported yet) */ {
                        get;
-                       set;    //throw new DomError.DOM ("Error");
+                       set;    //this.owner.last_error = new DomError.DOM ("Error");
                }
                /**
                 * The target name.
diff --git a/gxml/Text.vala b/gxml/Text.vala
index b7fa676..6720ae9 100644
--- a/gxml/Text.vala
+++ b/gxml/Text.vala
@@ -78,7 +78,7 @@ namespace GXml {
                        /* libxml2 doesn't handle this directly, in part because it doesn't
                           allow Text siblings.  Boo! */
                        if (offset < 0 || offset > this.length) {
-                               throw new DomError.INDEX_SIZE ("Offset '%u' is invalid for string of length 
'%u'", offset, this.length); // i18n
+                               this.owner_document.last_error = new DomError.INDEX_SIZE ("Offset '%u' is 
invalid for string of length '%u'", offset, this.length); // i18n
                        }
 
                        Text other = this.owner_document.create_text_node (this.data.substring 
((long)offset));


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]