From 2fafb291c7c0ee4cc2a81b4bfde417dc281b6d54 Mon Sep 17 00:00:00 2001 From: "allan%beaufour.dk" Date: Fri, 8 Apr 2005 15:40:54 +0000 Subject: [PATCH] Print messages in JS console for existing exceptions in code. Bug 289297, r=doron+smaug, a=mkaply --- extensions/xforms/nsXFormsControlStub.cpp | 6 +++- extensions/xforms/nsXFormsControlStub.h | 6 +++- extensions/xforms/nsXFormsInputElement.cpp | 2 +- extensions/xforms/nsXFormsModelElement.cpp | 17 +++++++--- extensions/xforms/nsXFormsOutputElement.cpp | 4 +-- extensions/xforms/nsXFormsSendElement.cpp | 5 ++- extensions/xforms/nsXFormsSetIndexElement.cpp | 13 ++++++-- extensions/xforms/nsXFormsTriggerElement.cpp | 3 ++ extensions/xforms/nsXFormsUtils.cpp | 32 ++++++++++++------- extensions/xforms/nsXFormsUtils.h | 22 ++++++++++--- .../resources/locale/en-US/xforms.properties | 13 ++++++-- 11 files changed, 92 insertions(+), 31 deletions(-) diff --git a/extensions/xforms/nsXFormsControlStub.cpp b/extensions/xforms/nsXFormsControlStub.cpp index 3ce89d8982f..3327d88f659 100644 --- a/extensions/xforms/nsXFormsControlStub.cpp +++ b/extensions/xforms/nsXFormsControlStub.cpp @@ -111,6 +111,9 @@ nsXFormsControlStub::ResetBoundNode() { mBoundNode = nsnull; + if (!mHasParent) + return NS_OK; + nsCOMPtr modelNode; nsCOMPtr result; nsresult rv = @@ -337,9 +340,10 @@ nsXFormsControlStub::DocumentChanged(nsIDOMDocument *aNewDocument) NS_IMETHODIMP nsXFormsControlStub::ParentChanged(nsIDOMElement *aNewParent) { + mHasParent = aNewParent != nsnull; // We need to re-evaluate our instance data binding when our parent changes, // since xmlns declarations or our context could have changed. - if (aNewParent) { + if (mHasParent) { Bind(); Refresh(); } diff --git a/extensions/xforms/nsXFormsControlStub.h b/extensions/xforms/nsXFormsControlStub.h index 3d82333d6f5..ac54ca3bd44 100644 --- a/extensions/xforms/nsXFormsControlStub.h +++ b/extensions/xforms/nsXFormsControlStub.h @@ -114,7 +114,8 @@ public: nsIXTFElement::NOTIFY_DOCUMENT_CHANGED | nsIXTFElement::NOTIFY_PARENT_CHANGED | nsIXTFElement::NOTIFY_HANDLE_DEFAULT), - kElementFlags(nsXFormsUtils::ELEMENT_WITH_MODEL_ATTR) + kElementFlags(nsXFormsUtils::ELEMENT_WITH_MODEL_ATTR), + mHasParent(PR_FALSE) {}; protected: @@ -133,6 +134,9 @@ protected: /** This event listener is used to create xforms-hint and xforms-help events. */ nsCOMPtr mEventListener; + /** State that tells whether control has a parent or not */ + PRBool mHasParent; + /** Returns the read only state of the control (ie. mBoundNode) */ PRBool GetReadOnlyState(); diff --git a/extensions/xforms/nsXFormsInputElement.cpp b/extensions/xforms/nsXFormsInputElement.cpp index b0c878b4b87..00c575b35a3 100644 --- a/extensions/xforms/nsXFormsInputElement.cpp +++ b/extensions/xforms/nsXFormsInputElement.cpp @@ -325,7 +325,7 @@ nsXFormsInputElement::UpdateInstanceData() NS_IMETHODIMP nsXFormsInputElement::Refresh() { - if (!mControl || !mModel) + if (!mControl || !mModel || !mHasParent) return NS_OK; nsAutoString text; diff --git a/extensions/xforms/nsXFormsModelElement.cpp b/extensions/xforms/nsXFormsModelElement.cpp index 3c64f16fd3e..12482cd9291 100644 --- a/extensions/xforms/nsXFormsModelElement.cpp +++ b/extensions/xforms/nsXFormsModelElement.cpp @@ -329,7 +329,8 @@ nsXFormsModelElement::DoneAddingChildren() } } if (NS_FAILED(rv)) { - // this is a fatal error + // this is a fatal error (XXX) + nsXFormsUtils::ReportError(NS_LITERAL_STRING("schemaLoadError"), mElement); nsXFormsUtils::DispatchEvent(mElement, eEvent_LinkException); return NS_OK; } @@ -694,6 +695,7 @@ NS_IMETHODIMP nsXFormsModelElement::OnError(nsresult aStatus, const nsAString &aStatusMessage) { + nsXFormsUtils::ReportError(NS_LITERAL_STRING("schemaLoadError"), mElement); nsXFormsUtils::DispatchEvent(mElement, eEvent_LinkException); return NS_OK; } @@ -728,7 +730,8 @@ nsXFormsModelElement::HandleEvent(nsIDOMEvent* aEvent) mSchemaCount++; } if (NS_FAILED(rv)) { - // this is a fatal error + // this is a fatal error (XXX) + nsXFormsUtils::ReportError(NS_LITERAL_STRING("schemaLoadError"), mElement); nsXFormsUtils::DispatchEvent(mElement, eEvent_LinkException); return NS_OK; } @@ -834,6 +837,7 @@ nsXFormsModelElement::InstanceLoadFinished(PRBool aSuccess) { --mPendingInstanceCount; if (!aSuccess) { + nsXFormsUtils::ReportError(NS_LITERAL_STRING("instanceLoadError"), mElement); nsXFormsUtils::DispatchEvent(mElement, eEvent_LinkException); } else if (IsComplete()) { nsresult rv = FinishConstruction(); @@ -1115,8 +1119,7 @@ nsXFormsModelElement::FinishConstruction() nsresult rv = mSchemas->ProcessSchemaElement(element, nsnull, getter_AddRefs(schema)); if (!NS_SUCCEEDED(rv)) { - nsXFormsUtils::ReportError(NS_LITERAL_STRING("schemaProcessError"), - nsnull, 0, node, node); + nsXFormsUtils::ReportError(NS_LITERAL_STRING("schemaProcessError"), node); } } } @@ -1217,6 +1220,8 @@ nsXFormsModelElement::ProcessBind(nsIXFormsXPathEvaluator *aEvaluator, rv = aEvaluator->CreateExpression(propStrings[i], aBindElement, getter_AddRefs(props[i])); if (NS_FAILED(rv)) { + nsXFormsUtils::ReportError(NS_LITERAL_STRING("mipParseError"), + aBindElement); nsXFormsUtils::DispatchEvent(mElement, eEvent_ComputeException); return rv; } @@ -1240,6 +1245,8 @@ nsXFormsModelElement::ProcessBind(nsIXFormsXPathEvaluator *aEvaluator, #ifdef DEBUG printf("xforms-binding-exception: XPath Evaluation failed\n"); #endif + nsXFormsUtils::ReportError(NS_LITERAL_STRING("nodesetEvaluateError"), + aBindElement); nsXFormsUtils::DispatchEvent(mElement, eEvent_BindingException); return rv; } @@ -1352,6 +1359,8 @@ nsXFormsModelElement::ProcessBind(nsIXFormsXPathEvaluator *aEvaluator, #ifdef DEBUG printf("xforms-binding-exception: Multiple MIPs on same node!"); #endif + nsXFormsUtils::ReportError(NS_LITERAL_STRING("multiMIPError"), + aBindElement); nsXFormsUtils::DispatchEvent(aBindElement, eEvent_BindingException); return NS_ERROR_FAILURE; diff --git a/extensions/xforms/nsXFormsOutputElement.cpp b/extensions/xforms/nsXFormsOutputElement.cpp index 4fb802cb892..7e18cd80f6d 100755 --- a/extensions/xforms/nsXFormsOutputElement.cpp +++ b/extensions/xforms/nsXFormsOutputElement.cpp @@ -204,7 +204,7 @@ nsXFormsOutputElement::AttributeSet(nsIAtom *aName, const nsAString &aValue) nsresult nsXFormsOutputElement::Bind() { - if (!mValue) + if (!mValue || !mHasParent) return NS_OK; mBoundNode = nsnull; @@ -240,7 +240,7 @@ nsXFormsOutputElement::Bind() NS_IMETHODIMP nsXFormsOutputElement::Refresh() { - if (!mValue) + if (!mValue || !mHasParent) return NS_OK; nsAutoString text; diff --git a/extensions/xforms/nsXFormsSendElement.cpp b/extensions/xforms/nsXFormsSendElement.cpp index cabdbc8c2db..c39c425a7b0 100644 --- a/extensions/xforms/nsXFormsSendElement.cpp +++ b/extensions/xforms/nsXFormsSendElement.cpp @@ -60,7 +60,7 @@ nsXFormsSendElement::HandleAction(nsIDOMEvent* aEvent, NS_NAMED_LITERAL_STRING(submission, "submission"); nsAutoString submissionID; - mElement->GetAttribute(NS_LITERAL_STRING("submission"), submissionID); + mElement->GetAttribute(submission, submissionID); if (submissionID.IsEmpty()) return NS_OK; @@ -73,6 +73,9 @@ nsXFormsSendElement::HandleAction(nsIDOMEvent* aEvent, doc->GetElementById(submissionID, getter_AddRefs(el)); if (!el || !nsXFormsUtils::IsXFormsElement(el, submission)) { + const PRUnichar *strings[] = { submissionID.get(), submission.get() }; + nsXFormsUtils::ReportError(NS_LITERAL_STRING("idRefError"), + strings, 2, mElement, mElement); return nsXFormsUtils::DispatchEvent(mElement, eEvent_BindingException); } diff --git a/extensions/xforms/nsXFormsSetIndexElement.cpp b/extensions/xforms/nsXFormsSetIndexElement.cpp index e5ab8d92626..6a13ee9f1e1 100644 --- a/extensions/xforms/nsXFormsSetIndexElement.cpp +++ b/extensions/xforms/nsXFormsSetIndexElement.cpp @@ -73,8 +73,10 @@ nsXFormsSetIndexElement::HandleAction(nsIDOMEvent *aEvent, // Get @repeat and @index nsAutoString id, index; - mElement->GetAttribute(NS_LITERAL_STRING("repeat"), id); - mElement->GetAttribute(NS_LITERAL_STRING("index"), index); + NS_NAMED_LITERAL_STRING(repeatStr, "repeat"); + mElement->GetAttribute(repeatStr, id); + NS_NAMED_LITERAL_STRING(indexStr, "index"); + mElement->GetAttribute(indexStr, index); if (id.IsEmpty() || index.IsEmpty()) { /// @todo Should we dispatch an exception, or just define that this does /// not happen as it is a non-validating form as both are required @@ -91,13 +93,15 @@ nsXFormsSetIndexElement::HandleAction(nsIDOMEvent *aEvent, // checked that @index is present above rv = nsXFormsUtils::EvaluateNodeBinding(mElement, 0, - NS_LITERAL_STRING("index"), + indexStr, EmptyString(), nsIDOMXPathResult::NUMBER_TYPE, getter_AddRefs(model), getter_AddRefs(result)); NS_ENSURE_SUCCESS(rv, rv); if (!result) { + nsXFormsUtils::ReportError(NS_LITERAL_STRING("indexEvalError"), + mElement); // beaufour: This makes sense, but is not according to the spec. I guess... nsXFormsUtils::DispatchEvent(mElement, eEvent_BindingException); return NS_ERROR_ABORT; @@ -121,6 +125,9 @@ nsXFormsSetIndexElement::HandleAction(nsIDOMEvent *aEvent, rv = domDoc->GetElementById(id, getter_AddRefs(repeatElem)); nsCOMPtr repeat = do_QueryInterface(repeatElem); if (!repeat) { + const PRUnichar *strings[] = { id.get(), repeatStr.get() }; + nsXFormsUtils::ReportError(NS_LITERAL_STRING("idRefError"), + strings, 2, mElement, mElement); // beaufour: This makes sense, but is not according to the spec. I guess... nsXFormsUtils::DispatchEvent(mElement, eEvent_BindingException); return NS_ERROR_ABORT; diff --git a/extensions/xforms/nsXFormsTriggerElement.cpp b/extensions/xforms/nsXFormsTriggerElement.cpp index 6355e859f86..023aef90bb3 100644 --- a/extensions/xforms/nsXFormsTriggerElement.cpp +++ b/extensions/xforms/nsXFormsTriggerElement.cpp @@ -343,6 +343,9 @@ nsXFormsSubmitElement::HandleDefault(nsIDOMEvent *aEvent, PRBool *aHandled) nsCOMPtr xfSubmission(do_QueryInterface(submissionElement)); if (!xfSubmission) { + const PRUnichar *strings[] = { submissionID.get(), submission.get() }; + nsXFormsUtils::ReportError(NS_LITERAL_STRING("idRefError"), + strings, 2, mElement, mElement); return nsXFormsUtils::DispatchEvent(mElement, eEvent_BindingException); } diff --git a/extensions/xforms/nsXFormsUtils.cpp b/extensions/xforms/nsXFormsUtils.cpp index 4761b377928..3dd42043ba2 100644 --- a/extensions/xforms/nsXFormsUtils.cpp +++ b/extensions/xforms/nsXFormsUtils.cpp @@ -261,11 +261,15 @@ nsXFormsUtils::GetNodeContext(nsIDOMElement *aElement, NS_ENSURE_TRUE(domDoc, NS_ERROR_FAILURE); nsAutoString bindId; - aElement->GetAttribute(NS_LITERAL_STRING("bind"), bindId); + NS_NAMED_LITERAL_STRING(bindStr, "bind"); + aElement->GetAttribute(bindStr, bindId); if (!bindId.IsEmpty()) { // CASE 1: Use @bind domDoc->GetElementById(bindId, aBindElement); - if (!IsXFormsElement(*aBindElement, NS_LITERAL_STRING("bind"))) { + if (!IsXFormsElement(*aBindElement, bindStr)) { + const PRUnichar *strings[] = { bindId.get(), bindStr.get() }; + nsXFormsUtils::ReportError(NS_LITERAL_STRING("idRefError"), + strings, 2, aElement, aElement); DispatchEvent(aElement, eEvent_BindingException); return NS_ERROR_ABORT; } @@ -286,7 +290,8 @@ nsXFormsUtils::GetNodeContext(nsIDOMElement *aElement, // CASE 2: Use @model // If bind did not set model, and the element has a model attribute we use this nsAutoString modelId; - aElement->GetAttribute(NS_LITERAL_STRING("model"), modelId); + NS_NAMED_LITERAL_STRING(modelStr, "model"); + aElement->GetAttribute(modelStr, modelId); if (!modelId.IsEmpty()) { nsCOMPtr modelElement; @@ -295,6 +300,9 @@ nsXFormsUtils::GetNodeContext(nsIDOMElement *aElement, // No element found, or element not a \ element if (!model) { + const PRUnichar *strings[] = { modelId.get(), modelStr.get() }; + nsXFormsUtils::ReportError(NS_LITERAL_STRING("idRefError"), + strings, 2, aElement, aElement); nsXFormsUtils::DispatchEvent(aElement, eEvent_BindingException); return NS_ERROR_FAILURE; } @@ -393,11 +401,13 @@ nsXFormsUtils::EvaluateXPath(const nsAString &aExpression, } CallQueryInterface(supResult, &result); // addrefs } - else if(rv == NS_ERROR_XFORMS_CALCUATION_EXCEPTION){ - nsCOMPtr resolverElement = do_QueryInterface(aResolverNode); - nsCOMPtr modelPriv = nsXFormsUtils::GetModel(resolverElement); - nsCOMPtr model = do_QueryInterface(modelPriv); - DispatchEvent(model, eEvent_ComputeException); + else if (rv == NS_ERROR_XFORMS_CALCUATION_EXCEPTION) { + nsXFormsUtils::ReportError(NS_LITERAL_STRING("exprEvaluateError"), + nsnull, 0, aContextNode, nsnull); + nsCOMPtr resolverElement = do_QueryInterface(aResolverNode); + nsCOMPtr modelPriv = nsXFormsUtils::GetModel(resolverElement); + nsCOMPtr model = do_QueryInterface(modelPriv); + DispatchEvent(model, eEvent_ComputeException); } return result; @@ -800,11 +810,8 @@ nsXFormsUtils::FindParentContext(nsIDOMElement *aElement, NS_ENSURE_ARG_POINTER(aModel); NS_ENSURE_ARG_POINTER(aContextNode); - nsCOMPtr elementNode = do_QueryInterface(aElement); - NS_ENSURE_TRUE(elementNode, NS_ERROR_FAILURE); - nsCOMPtr curNode; - nsresult rv = elementNode->GetParentNode(getter_AddRefs(curNode)); + nsresult rv = aElement->GetParentNode(getter_AddRefs(curNode)); NS_ENSURE_SUCCESS(rv, NS_OK); // If a model is set, get its ID @@ -881,6 +888,7 @@ nsXFormsUtils::FindParentContext(nsIDOMElement *aElement, model = do_QueryInterface(modelElement); } if (!model) { + nsXFormsUtils::ReportError(NS_LITERAL_STRING("noModelError"), aElement); DispatchEvent(aElement, eEvent_BindingException); return NS_ERROR_ABORT; } diff --git a/extensions/xforms/nsXFormsUtils.h b/extensions/xforms/nsXFormsUtils.h index e3e000b4525..9b723a9aa43 100644 --- a/extensions/xforms/nsXFormsUtils.h +++ b/extensions/xforms/nsXFormsUtils.h @@ -362,11 +362,25 @@ public: * @param aContext If set, the node is used to output what element caused the error */ - static NS_HIDDEN_(void) ReportError(const nsString& aMessageName, + static NS_HIDDEN_(void) ReportError(const nsString &aMessageName, const PRUnichar **aParams, - PRUint32 aParamLength, - nsIDOMNode *aElement, - nsIDOMNode *aContext); + PRUint32 aParamLength, + nsIDOMNode *aElement, + nsIDOMNode *aContext); + + /** + * Simple version of ReportError(), used when reporting without message + * arguments and file location and node is taken from same element (or + * nsnull). + * + * @param aMessageName Name of string + * @param aElement Element to use for location and context + */ + static NS_HIDDEN_(void) ReportError(const nsString &aMessageName, + nsIDOMNode *aElement = nsnull) + { + nsXFormsUtils::ReportError(aMessageName, nsnull, 0, aElement, aElement); + } }; diff --git a/extensions/xforms/resources/locale/en-US/xforms.properties b/extensions/xforms/resources/locale/en-US/xforms.properties index ff690584bb0..8bbcc52a817 100644 --- a/extensions/xforms/resources/locale/en-US/xforms.properties +++ b/extensions/xforms/resources/locale/en-US/xforms.properties @@ -1,5 +1,14 @@ errorContext = \n\nContext: %S errorSource = \n\nSource: %S -missingTypeName = XForms Error (1): type (%S) missing type name. -schemaProcessError = XForms Error (2): Failed to process inline schema. +missingTypeName = XForms Error (1): type (%S) missing type name +schemaProcessError = XForms Error (2): Failed to process inline schema +schemaLoadError = XForms Error (3): Failed to load schema +instanceLoadError = XForms Error (4): Failed to load instance +mipParseError = XForms Error (5): Error while parsing model item property: %S +nodesetEvaluateError = XForms Error (6): Error evaluating nodeset expression: %S +multiMIPError = XForms Error (7): Multiply defined model item property on element +idRefError = XForms Error (8): id (%S) does not refer to a %S element +indexEvalError = XForms Error (9): Error evaluating index attribute: %S +exprEvaluateError = XForms Error (10): Error evaluating XPath expression: %S +noModelError = XForms Error (11): Could not find model for element