From cf78c478c777e7de22b056cc289614ab1da0686e Mon Sep 17 00:00:00 2001
From: Pawel Wasowski
Date: Mon, 15 Jun 2020 10:14:00 +0200
Subject: [PATCH] [common][utils] Add new helper methods
This commit adds 2 helper methods, that may be used from all modules:
- (JS) NativeManager.getErrorObjectAndValidate(), that creates a
WebAPIException from the data sent from C++ layer. If the exception is
not among valid exceptions passed to this function, it is replaced with
a default value (e.g. UnknownError). A list of exceptions/errors,
defined for a function in the documentation can be passed as the list of
valid exceptions to ensure, that the function will only report errors
defined in the documentation.
- (C++) PlatformResult::SetMessage() method to set the error message.
This commit also allows to use JS console.assert() through
xwalk.utils.assert().
[Verification] The code compiles. I use it in the next commit and the
code works fine.
Change-Id: If2291a4123b3235971ff1f1f1c69320bbb1ee167
Signed-off-by: Pawel Wasowski
---
src/common/platform_result.h | 8 ++++++++
src/utils/utils_api.js | 21 +++++++++++++++++++++
2 files changed, 29 insertions(+)
diff --git a/src/common/platform_result.h b/src/common/platform_result.h
index 95b7c30a..d70970dd 100644
--- a/src/common/platform_result.h
+++ b/src/common/platform_result.h
@@ -82,6 +82,14 @@ class PlatformResult {
return message_;
}
+ void SetMessage(const std::string& message) {
+ message_ = message;
+ }
+
+ void SetMessage(std::string&& message) {
+ message_ = std::move(message);
+ }
+
bool IsSuccess() const {
return error_code() == ErrorCode::NO_ERROR;
}
diff --git a/src/utils/utils_api.js b/src/utils/utils_api.js
index 21241f12..bcc2a606 100644
--- a/src/utils/utils_api.js
+++ b/src/utils/utils_api.js
@@ -225,6 +225,7 @@ function Utils() {
Utils.prototype.error = console.error.bind(console);
Utils.prototype.warn = console.warn.bind(console);
Utils.prototype.log = _enableJsLogs ? console.log.bind(console) : function() {};
+Utils.prototype.assert = console.assert.bind(console);
Utils.prototype.global = _global;
@@ -1394,6 +1395,26 @@ NativeManager.prototype.getErrorObject = function(result) {
return new WebAPIException(result.error);
};
+/*
+ * This function checks if the reported error's name is in valid_error_names.
+ * If so, it is returned. Otherwise, default_error is returned.
+ * valid_error_names should contain error names defined in the API reference
+ * for the called function.
+ */
+NativeManager.prototype.getErrorObjectAndValidate = function(result,
+ valid_error_names,
+ default_error) {
+ xwalk.utils.assert(Array.isArray(valid_error_names),
+ "valid_error_names must be an Array. %s was passed instead",
+ typeof valid_error_names);
+ var error = new WebAPIException(result.error);
+ if (valid_error_names.includes(error.name)) {
+ return error;
+ }
+
+ return default_error;
+};
+
NativeManager.prototype.callIfPossible = function(callback) {
if (!_type.isNullOrUndefined(callback)) {
callback.apply(callback, [].slice.call(arguments, 1));
--
2.34.1