summaryrefslogtreecommitdiff
path: root/dom/base
diff options
context:
space:
mode:
authorBrian Smith <brian@dbsoft.org>2023-09-28 21:18:27 -0500
committerBrian Smith <brian@dbsoft.org>2023-09-28 21:18:27 -0500
commitb1d111ab6b7e5493226f6d9fe1ba38dc5c467379 (patch)
tree73a3aa461f3977bc889898d0b9bf89e4ae6c38cc /dom/base
parent99c25d8e984905ef96990df66fea6bc12c310bbb (diff)
downloaduxp-b1d111ab6b7e5493226f6d9fe1ba38dc5c467379.tar.gz
Issue #1442 - Part 16 - Report stream errors during consumption.
https://bugzilla.mozilla.org/show_bug.cgi?id=1128959
Diffstat (limited to 'dom/base')
-rw-r--r--dom/base/nsContentUtils.cpp71
-rw-r--r--dom/base/nsContentUtils.h4
2 files changed, 75 insertions, 0 deletions
diff --git a/dom/base/nsContentUtils.cpp b/dom/base/nsContentUtils.cpp
index 8dac3e70d4..4c00f358f2 100644
--- a/dom/base/nsContentUtils.cpp
+++ b/dom/base/nsContentUtils.cpp
@@ -39,6 +39,8 @@
#include "mozilla/dom/ContentChild.h"
#include "mozilla/dom/CustomElementRegistry.h"
#include "mozilla/dom/DocumentFragment.h"
+#include "mozilla/dom/DOMException.h"
+#include "mozilla/dom/DOMExceptionBinding.h"
#include "mozilla/dom/DOMTypes.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/FileSystemSecurity.h"
@@ -10079,3 +10081,72 @@ void nsContentUtils::StructuredClone(JSContext* aCx,
nsTArray<RefPtr<MessagePort>> ports = holder.TakeTransferredPorts();
Unused << ports;
}
+
+/* static */ void
+nsContentUtils::ExtractErrorValues(JSContext* aCx,
+ JS::Handle<JS::Value> aValue,
+ nsACString& aSourceSpecOut,
+ uint32_t* aLineOut,
+ uint32_t* aColumnOut,
+ nsString& aMessageOut)
+{
+ MOZ_ASSERT(aLineOut);
+ MOZ_ASSERT(aColumnOut);
+
+ if (aValue.isObject()) {
+ JS::Rooted<JSObject*> obj(aCx, &aValue.toObject());
+ RefPtr<dom::DOMException> domException;
+
+ // Try to process as an Error object. Use the file/line/column values
+ // from the Error as they will be more specific to the root cause of
+ // the problem.
+ JSErrorReport* err = obj ? JS_ErrorFromException(aCx, obj) : nullptr;
+ if (err) {
+ // Use xpc to extract the error message only. We don't actually send
+ // this report anywhere.
+ RefPtr<xpc::ErrorReport> report = new xpc::ErrorReport();
+ report->Init(err,
+ "<unknown>", // toString result
+ false, // chrome
+ 0); // window ID
+
+ if (!report->mFileName.IsEmpty()) {
+ CopyUTF16toUTF8(report->mFileName, aSourceSpecOut);
+ *aLineOut = report->mLineNumber;
+ *aColumnOut = report->mColumn;
+ }
+ aMessageOut.Assign(report->mErrorMsg);
+ }
+
+ // Next, try to unwrap the rejection value as a DOMException.
+ else if(NS_SUCCEEDED(UNWRAP_OBJECT(DOMException, obj, domException))) {
+
+ nsAutoString filename;
+ domException->GetFilename(aCx, filename);
+ if (!filename.IsEmpty()) {
+ CopyUTF16toUTF8(filename, aSourceSpecOut);
+ *aLineOut = domException->LineNumber(aCx);
+ *aColumnOut = domException->ColumnNumber();
+ }
+
+ domException->GetName(aMessageOut);
+ aMessageOut.AppendLiteral(": ");
+
+ nsAutoString message;
+ domException->GetMessageMoz(message);
+ aMessageOut.Append(message);
+ }
+ }
+
+ // If we could not unwrap a specific error type, then perform default safe
+ // string conversions on primitives. Objects will result in "[Object]"
+ // unfortunately.
+ if (aMessageOut.IsEmpty()) {
+ nsAutoJSString jsString;
+ if (jsString.init(aCx, aValue)) {
+ aMessageOut = jsString;
+ } else {
+ JS_ClearPendingException(aCx);
+ }
+ }
+}
diff --git a/dom/base/nsContentUtils.h b/dom/base/nsContentUtils.h
index 9bfb91add2..6e9f23054a 100644
--- a/dom/base/nsContentUtils.h
+++ b/dom/base/nsContentUtils.h
@@ -1010,6 +1010,10 @@ public:
static bool PrefetchEnabled(nsIDocShell* aDocShell);
+ static void ExtractErrorValues(JSContext* aCx, JS::Handle<JS::Value> aValue,
+ nsACString& aSourceSpecOut, uint32_t *aLineOut,
+ uint32_t *aColumnOut, nsString& aMessageOut);
+
static nsresult CalculateBufferSizeForImage(const uint32_t& aStride,
const mozilla::gfx::IntSize& aImageSize,
const mozilla::gfx::SurfaceFormat& aFormat,