[common][utils] Add new helper methods 59/236159/3
authorPawel Wasowski <p.wasowski2@samsung.com>
Mon, 15 Jun 2020 08:14:00 +0000 (10:14 +0200)
committerPawel Wasowski <p.wasowski2@samsung.com>
Mon, 6 Jul 2020 18:33:50 +0000 (20:33 +0200)
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 <p.wasowski2@samsung.com>
src/common/platform_result.h
src/utils/utils_api.js

index 95b7c30a0420e610a87d68965ab46a900acded5a..d70970dd210fa73b57dafdbf5f52bb378847027c 100644 (file)
@@ -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;
   }
index 21241f12001a51f73245da3b4dddeafc7d2fbfed..bcc2a60635baa0c4078faab607586cbab24d230b 100644 (file)
@@ -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));