Add a way to "catch" an ErrorResult, and a way to safely convert an ErrorResult to an nsresult.

This commit is contained in:
Pale Moon
2017-03-19 11:32:22 +01:00
committed by roytam1
parent a36be1efb4
commit 41720aeaac
2 changed files with 32 additions and 0 deletions
+18
View File
@@ -331,6 +331,24 @@ ErrorResult::ReportNotEnoughArgsError(JSContext* cx,
ThrowErrorMessage(cx, dom::MSG_MISSING_ARGUMENTS, errorMessage.get());
}
void
ErrorResult::SuppressException()
{
WouldReportJSException();
if (IsErrorWithMessage()) {
ClearMessage();
} else if (IsJSException()) {
JSContext* cx = nsContentUtils::GetDefaultJSContextForThread();
// Just steal it into a stack value (unrooting it in the process)
// that we then allow to die.
JS::Rooted<JS::Value> temp(cx);
StealJSException(cx, &temp);
}
// We don't use AssignErrorCode, because we want to override existing error
// states, which AssignErrorCode is not allowed to do.
mResult = NS_OK;
}
namespace dom {
bool
+14
View File
@@ -64,6 +64,20 @@ public:
mResult = rv;
}
// Use SuppressException when you want to suppress any exception that might be
// on the ErrorResult. After this call, the ErrorResult will be back a "no
// exception thrown" state.
void SuppressException();
// Use StealNSResult() when you want to safely convert the ErrorResult to an
// nsresult that you will then return to a caller. This will
// SuppressException(), since there will no longer be a way to report it.
nsresult StealNSResult() {
nsresult rv = ErrorCode();
SuppressException();
return rv;
}
void ThrowTypeError(const dom::ErrNum errorNumber, ...);
void ThrowRangeError(const dom::ErrNum errorNumber, ...);
void ReportErrorWithMessage(JSContext* cx);