[CORD-2827] Fixed unauthorized error handling

Change-Id: I6ddef7f869c17db4d8479f23f6e8734f6002d8fc
diff --git a/src/app/core/loader/loader.ts b/src/app/core/loader/loader.ts
index b93aebf..3606ca8 100644
--- a/src/app/core/loader/loader.ts
+++ b/src/app/core/loader/loader.ts
@@ -19,6 +19,8 @@
 import {IXosModelDiscovererService} from '../../datasources/helpers/model-discoverer.service';
 import {IXosOnboarder} from '../../extender/services/onboard.service';
 import {IXosAuthService} from '../../datasources/rest/auth.rest';
+import './loader.scss';
+
 class LoaderCtrl {
   static $inject = [
     '$log',
@@ -32,6 +34,9 @@
     `XosOnboarder`
   ];
 
+  public loader: boolean = true;
+  public error: string;
+
   constructor (
     private $log: ng.ILogService,
     private $rootScope: ng.IScope,
@@ -49,26 +54,45 @@
 
   public run() {
     if (this.XosModelDiscoverer.areModelsLoaded()) {
+      this.$log.debug(`[XosLoader] Models are already loaded, moving to: ${this.XosConfig.lastVisitedUrl}`);
       this.moveOnTo(this.XosConfig.lastVisitedUrl);
     }
     else if (!this.XosAuthService.isAuthenticated()) {
+      this.$log.debug(`[XosLoader] Not authenticated, send to login`);
       this.$state.go('xos.login');
     }
     else {
       // NOTE loading XOS Models
       this.XosModelDiscoverer.discover()
         .then((res) => {
-          if (res) {
+          this.$log.info('[XosLoader] res: ' + res, res, typeof res);
+          if (res === 'chameleon') {
+            this.loader = false;
+            this.error = 'chameleon';
+            return 'chameleon';
+          }
+          else if (res) {
             this.$log.info('[XosLoader] All models loaded');
+            // NOTE loading GUI Extensions
+            this.XosOnboarder.onboard();
+            return true;
           }
           else {
             this.$log.info('[XosLoader] Failed to load some models, moving on.');
+            return true;
           }
-          // NOTE loading GUI Extensions
-          return this.XosOnboarder.onboard();
         })
-        .then(() => {
-          this.moveOnTo(this.XosConfig.lastVisitedUrl);
+        .then((res) => {
+          if (res === true) {
+            this.moveOnTo(this.XosConfig.lastVisitedUrl);
+          }
+          // NOTE otherwise stay here since we're printing some error messages
+        })
+        .catch(() => {
+          // XosModelDiscoverer.discover reject only in case of authentication error
+          this.XosAuthService.clearUser();
+          this.moveOnTo('/login');
+
         })
         .finally(() => {
           // NOTE it is in a timeout as the searchService is loaded after that
@@ -99,7 +123,22 @@
 
 export const xosLoader: angular.IComponentOptions = {
   template: `
-    <div class="loader"></div>
+    <div ng-show="vm.loader" class="loader"></div>
+    <div class="row" ng-show="vm.error == 'chameleon'">
+      <div class="col-sm-6 col-sm-offset-3">
+        <div class="alert alert-danger">
+          <div class="row">
+            <div class="col-xs-2">
+              <i class="fa fa-exclamation-triangle"></i>
+            </div>
+            <div class="col-xs-10">
+              <strong>Cannot load models definition.</strong><br>
+              Please check that the Chameleon container is running
+            </div>
+          </div>
+        </div>
+      </div>
+    </div>
   `,
   controllerAs: 'vm',
   controller: LoaderCtrl