Bug 1032755 - Return a DOMException with error message when BluetoothReplyRunnable rejects the promise. r=btian

This commit is contained in:
Jamin Liu 2014-08-25 14:51:29 +08:00
parent f895ab8db1
commit 9150681cbf
6 changed files with 83 additions and 12 deletions

View File

@ -21,6 +21,7 @@ BluetoothReplyRunnable::BluetoothReplyRunnable(nsIDOMDOMRequest* aReq,
const nsAString& aName)
: mDOMRequest(aReq)
, mPromise(aPromise)
, mErrorStatus(STATUS_FAIL)
, mName(aName)
{
if (aPromise) {
@ -83,13 +84,9 @@ BluetoothReplyRunnable::FireErrorString()
if (mPromise) {
BT_API2_LOGR("<%s>", NS_ConvertUTF16toUTF8(mName).get());
/**
* Always reject with NS_ERROR_DOM_OPERATION_ERR.
*
* TODO: Return actual error result once bluetooth backend wraps
* nsresult instead of error string.
*/
mPromise->MaybeReject(NS_ERROR_DOM_OPERATION_ERR);
nsresult rv =
NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_DOM_BLUETOOTH, mErrorStatus);
mPromise->MaybeReject(rv);
}
return NS_OK;
@ -106,7 +103,8 @@ BluetoothReplyRunnable::Run()
nsresult rv;
if (mReply->type() != BluetoothReply::TBluetoothReplySuccess) {
SetError(mReply->get_BluetoothReplyError().error());
SetError(mReply->get_BluetoothReplyError().errorString(),
mReply->get_BluetoothReplyError().errorStatus());
rv = FireErrorString();
} else if (!ParseSuccessfulReply(&v)) {
rv = FireErrorString();

View File

@ -35,9 +35,11 @@ public:
void SetReply(BluetoothReply* aReply);
void SetError(const nsAString& aError)
void SetError(const nsAString& aErrorString,
const enum BluetoothStatus aErrorStatus = STATUS_FAIL)
{
mErrorString = aError;
mErrorString = aErrorString;
mErrorStatus = aErrorStatus;
}
virtual void ReleaseMembers();
@ -67,6 +69,7 @@ private:
nsCOMPtr<nsIDOMDOMRequest> mDOMRequest;
nsRefPtr<Promise> mPromise;
BluetoothStatus mErrorStatus;
nsString mErrorString;
nsString mName; // for debugging
};

View File

@ -173,7 +173,27 @@ DispatchBluetoothReply(BluetoothReplyRunnable* aRunnable,
BluetoothReply* reply;
if (!aErrorStr.IsEmpty()) {
nsString err(aErrorStr);
reply = new BluetoothReply(BluetoothReplyError(err));
reply = new BluetoothReply(BluetoothReplyError(STATUS_FAIL, err));
} else {
MOZ_ASSERT(aValue.type() != BluetoothValue::T__None);
reply = new BluetoothReply(BluetoothReplySuccess(aValue));
}
aRunnable->SetReply(reply);
if (NS_FAILED(NS_DispatchToMainThread(aRunnable))) {
BT_WARNING("Failed to dispatch to main thread!");
}
}
void
DispatchBluetoothReply(BluetoothReplyRunnable* aRunnable,
const BluetoothValue& aValue,
const enum BluetoothStatus aStatusCode)
{
// Reply will be deleted by the runnable after running on main thread
BluetoothReply* reply;
if (aStatusCode != STATUS_SUCCESS) {
reply = new BluetoothReply(BluetoothReplyError(aStatusCode, EmptyString()));
} else {
MOZ_ASSERT(aValue.type() != BluetoothValue::T__None);
reply = new BluetoothReply(BluetoothReplySuccess(aValue));

View File

@ -38,16 +38,55 @@ bool
BroadcastSystemMessage(const nsAString& aType,
const BluetoothValue& aData);
/**
* Dispatch Bluetooth reply to main thread. The reply will contain an error
* string if the request fails.
*
* This function is mainly designed for DOMRequest-based methods which return
* 'DOMRequest'. If aErrorStr is not empty, the DOMRequest property 'error.name'
* would be updated to aErrorStr right before the callback function 'onerror'
* is fired.
*
* @param aRunnable the runnable to reply the bluetooth request.
* @param aValue the Bluetooth value which is used to reply a Bluetooth
* request when the request finished successfully.
* @param aErrorStr the error string which is used to reply a Bluetooth
* request when the request failed.
*/
void
DispatchBluetoothReply(BluetoothReplyRunnable* aRunnable,
const BluetoothValue& aValue,
const nsAString& aErrorStr);
/**
* Dispatch Bluetooth reply to main thread. The reply will contain an error
* status if the request fails.
*
* This function mainly designed for Bluetooth APIs which return 'Promise'.
* If aStatusCode is not STATUS_SUCCESS, the Promise would reject with an
* Exception object.
* The ns error used by Exception will be associated with aStatusCode.
* The name and messege of Exception are defined in dom/base/domerr.msg and
* will be filled automatically during promise rejection.
*
* @param aRunnable the runnable to reply the bluetooth request.
* @param aValue the Bluetooth value which is used to reply a Bluetooth
* request if the request finished successfully.
* @param aStatusCode the error status which is used to reply a Bluetooth
* request when the request failed.
*/
void
DispatchBluetoothReply(BluetoothReplyRunnable* aRunnable,
const BluetoothValue& aValue,
const enum BluetoothStatus aStatusCode);
void
DispatchStatusChangedEvent(const nsAString& aType,
const nsAString& aDeviceAddress,
bool aStatus);
END_BLUETOOTH_NAMESPACE
#endif

View File

@ -20,6 +20,14 @@ struct ParamTraits<mozilla::dom::bluetooth::BluetoothObjectType>
mozilla::dom::bluetooth::TYPE_INVALID>
{ };
template <>
struct ParamTraits<mozilla::dom::bluetooth::BluetoothStatus>
: public ContiguousEnumSerializer<
mozilla::dom::bluetooth::BluetoothStatus,
mozilla::dom::bluetooth::STATUS_SUCCESS,
mozilla::dom::bluetooth::STATUS_RMT_DEV_DOWN>
{ };
} // namespace IPC
#endif // mozilla_dom_bluetooth_ipc_bluetoothchild_h__

View File

@ -4,6 +4,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
using mozilla::dom::bluetooth::BluetoothStatus from "mozilla/dom/bluetooth/BluetoothCommon.h";
namespace mozilla {
namespace dom {
namespace bluetooth {
@ -49,7 +51,8 @@ struct BluetoothReplySuccess
struct BluetoothReplyError
{
nsString error;
BluetoothStatus errorStatus;
nsString errorString;
};
union BluetoothReply