From 2275454efad2441f19f928ebb992ca37428ff6ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Schr=C3=B6der?= Date: Thu, 28 May 2015 17:05:57 +0200 Subject: [PATCH] Better error reporting and new validator support attached to whole uci section --- luciexpress/htdocs/js/app.js | 2 +- luciexpress/htdocs/js/uci.js | 12 ++++++++-- .../plugins/core/widgets/luci.config.js | 23 ++++++++++++++----- .../plugins/core/widgets/luci.errors.html | 2 +- .../plugins/core/widgets/luci.errors.js | 5 ++-- .../wifi/widgets/uci.wireless.interface.html | 17 +++++++------- .../wifi/widgets/uci.wireless.interface.js | 10 +++++++- luciexpress/htdocs/plugins/wifi/wifi.js | 19 +++++++++++++++ 8 files changed, 69 insertions(+), 21 deletions(-) diff --git a/luciexpress/htdocs/js/app.js b/luciexpress/htdocs/js/app.js index 67599bf0d..6f60036d8 100644 --- a/luciexpress/htdocs/js/app.js +++ b/luciexpress/htdocs/js/app.js @@ -122,7 +122,7 @@ JUCI.app.config(function ($stateProvider, $locationProvider, $compileProvider, $ $rootScope.errors = []; $rootScope.$on("error", function(ev, data){ $rootScope.errors.push({message: data}); - console.log("ERROR: "+ev.name+": "+JSON.stringify(Object.keys(ev.currentScope))); + //console.log("ERROR: "+ev.name+": "+JSON.stringify(Object.keys(ev.currentScope))); }); // set current language gettextCatalog.currentLanguage = "en"; diff --git a/luciexpress/htdocs/js/uci.js b/luciexpress/htdocs/js/uci.js index a3382b5dc..e597a56fb 100644 --- a/luciexpress/htdocs/js/uci.js +++ b/luciexpress/htdocs/js/uci.js @@ -283,10 +283,14 @@ return deferred.promise(); } + /* UCISection.prototype.$save = function(){ var deferred = $.Deferred(); var self = this; + // try to validate the section using section wide validator + if(self[".validator"] instanceof Function) self[".validator"](self); + $rpc.uci.set({ config: self[".config"][".name"], section: self[".name"], @@ -297,7 +301,7 @@ deferred.reject(); }); return deferred.promise(); - } + }*/ UCISection.prototype.$delete = function(){ var self = this; @@ -326,6 +330,9 @@ if(!type) return {}; var self = this; var changed = {}; + + if(type[".validator"] instanceof Function) type[".validator"](self); + Object.keys(type).map(function(k){ if(self[k] && self[k].dirty){ //console.log("Adding dirty field: "+k); @@ -443,12 +450,13 @@ }); } - UCIConfig.prototype.$registerSectionType = function(name, descriptor){ + UCIConfig.prototype.$registerSectionType = function(name, descriptor, validator){ var config = this[".name"]; var conf_type = section_types[config]; if(typeof conf_type === "undefined") conf_type = section_types[config] = {}; conf_type[name] = descriptor; this["@"+name] = []; + if(validator !== undefined && validator instanceof Function) conf_type[name][".validator"] = validator; console.log("Registered new section type "+config+"."+name); } diff --git a/luciexpress/htdocs/plugins/core/widgets/luci.config.js b/luciexpress/htdocs/plugins/core/widgets/luci.config.js index 26cbb4d7f..1baadff3d 100644 --- a/luciexpress/htdocs/plugins/core/widgets/luci.config.js +++ b/luciexpress/htdocs/plugins/core/widgets/luci.config.js @@ -75,19 +75,30 @@ $juci.module("core") ''+ '
', replace: true, + scope: { + onPreApply: "&" + }, controller: "luciConfigApplyController" }; }).controller("luciConfigApplyController", function($scope, $uci){ $scope.onApply = function(){ + if($scope.onPreApply) $scope.onPreApply(); $scope.busy = 1; - $uci.save().done(function(){ - console.log("Saved uci configuration!"); - }).fail(function(){ - console.error("Could not save uci configuration!"); - }).always(function(){ + try { + $uci.save().done(function(){ + console.log("Saved uci configuration!"); + }).fail(function(){ + console.error("Could not save uci configuration!"); + }).always(function(){ + $scope.busy = 0; + setTimeout(function(){$scope.$apply();}, 0); + }); + } catch(e){ $scope.busy = 0; setTimeout(function(){$scope.$apply();}, 0); - }); + $scope.$emit("error", e.message); + console.error("Error while applying config: "+e.message); + } } $scope.onCancel = function(){ // simple way to reset diff --git a/luciexpress/htdocs/plugins/core/widgets/luci.errors.html b/luciexpress/htdocs/plugins/core/widgets/luci.errors.html index eb5f054ba..6969b8c20 100644 --- a/luciexpress/htdocs/plugins/core/widgets/luci.errors.html +++ b/luciexpress/htdocs/plugins/core/widgets/luci.errors.html @@ -1,5 +1,5 @@
    -
  • {{err.message}}
  • +
  • {{(err.message||err)}}
diff --git a/luciexpress/htdocs/plugins/core/widgets/luci.errors.js b/luciexpress/htdocs/plugins/core/widgets/luci.errors.js index 212b64872..99fa1c39d 100644 --- a/luciexpress/htdocs/plugins/core/widgets/luci.errors.js +++ b/luciexpress/htdocs/plugins/core/widgets/luci.errors.js @@ -4,6 +4,7 @@ JUCI.app return { // accepted parameters for this tag scope: { + ngModel: "=" }, templateUrl: plugin_root+"/widgets/luci.errors.html", replace: true, @@ -11,6 +12,6 @@ JUCI.app }; }) .controller("luciErrors", function($scope, $rootScope, $localStorage){ - - $scope.errors = $rootScope.errors; + if($scope.ngModel) $scope.errors = $scope.ngModel; + else $scope.errors = $rootScope.errors; }); diff --git a/luciexpress/htdocs/plugins/wifi/widgets/uci.wireless.interface.html b/luciexpress/htdocs/plugins/wifi/widgets/uci.wireless.interface.html index 52c4f82ca..f918da1f8 100644 --- a/luciexpress/htdocs/plugins/wifi/widgets/uci.wireless.interface.html +++ b/luciexpress/htdocs/plugins/wifi/widgets/uci.wireless.interface.html @@ -19,7 +19,7 @@ - + @@ -30,17 +30,18 @@ - - -
- - -
+ + +
+ + +
+ diff --git a/luciexpress/htdocs/plugins/wifi/widgets/uci.wireless.interface.js b/luciexpress/htdocs/plugins/wifi/widgets/uci.wireless.interface.js index e293657c9..158088164 100644 --- a/luciexpress/htdocs/plugins/wifi/widgets/uci.wireless.interface.js +++ b/luciexpress/htdocs/plugins/wifi/widgets/uci.wireless.interface.js @@ -11,6 +11,11 @@ $juci.module("wifi") require: "^ngModel" }; }).controller("WifiInterfaceController", function($scope, $uci, gettext){ + $scope.errors = []; + $scope.$on("error", function(ev, err){ + ev.stopPropagation(); + $scope.errors.push(err); + }); $scope.$watch("interface", function(value){ try { $scope.cryptoChoices = $scope.interface.encryption.schema.allow; @@ -26,5 +31,8 @@ $juci.module("wifi") x[".frequency"] = dev[".label"]; }); $scope.title = "wifi-iface.name="+$scope.interface[".name"]; - }); + }); + $scope.onPreApply = function(){ + $scope.errors.length = 0; + } }); diff --git a/luciexpress/htdocs/plugins/wifi/wifi.js b/luciexpress/htdocs/plugins/wifi/wifi.js index 3ecf948a4..b2984028c 100644 --- a/luciexpress/htdocs/plugins/wifi/wifi.js +++ b/luciexpress/htdocs/plugins/wifi/wifi.js @@ -67,6 +67,25 @@ JUCI.app "macmode": { dvalue: 1, type: Number, allow: [ 0, 1, 2 ] }, "macfilter": { dvalue: false, type: Boolean }, "maclist": { dvalue: [], type: Array, match_each: /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/ } + }, function validator(section){ + // validate ssid + if(section.ssid.value.length >= 32) + throw new Error("SSID string can be at most 32 characters long!"); + // validate keys + switch(section.encryption.value){ + case "wep": { + if(!section.key.value || !section.key.value.match(/[a-f0-9A-F]{10,26}/)) + throw new Error("WEP encryption key must be 10-26 hexadecimal characters!"); + } break; + case "psk": + case "psk2": + case "mixed-psk": { + if(!section.key.value || !(section.key.value.length > 8 && section.key.value.length < 64)) + throw new Error("WPA key must be 8-63 characters long!"); + } break; + default: + break; + } });