Browse Source

Improved error handling

Torkel Ödegaard 7 years ago
parent
commit
6d874dd1f1

+ 20 - 6
public/app/core/copy/appNotification.ts

@@ -1,3 +1,4 @@
+import _ from 'lodash';
 import { AppNotification, AppNotificationSeverity, AppNotificationTimeout } from 'app/types';
 
 const defaultSuccessNotification: AppNotification = {
@@ -31,12 +32,25 @@ export const createSuccessNotification = (title: string, text?: string): AppNoti
   id: Date.now(),
 });
 
-export const createErrorNotification = (title: string, text?: string): AppNotification => ({
-  ...defaultErrorNotification,
-  title: title,
-  text: text,
-  id: Date.now(),
-});
+export const createErrorNotification = (title: string, text?: any): AppNotification => {
+  // Handling if text is an error object
+  if (text && !_.isString(text)) {
+    if (text.message) {
+      text = text.message;
+    } else if (text.data && text.data.message) {
+      text = text.data.message;
+    } else {
+      text = text.toString();
+    }
+  }
+
+  return {
+    ...defaultErrorNotification,
+    title: title,
+    text: text,
+    id: Date.now(),
+  };
+};
 
 export const createWarningNotification = (title: string, text?: string): AppNotification => ({
   ...defaultWarningNotification,

+ 4 - 3
public/app/features/dashboard/state/initDashboard.ts

@@ -81,7 +81,6 @@ export function initDashboard({
 
     try {
       switch (routeInfo) {
-        // handle old urls with no uid
         case DashboardRouteInfo.Home: {
           // load home dash
           dashDTO = await getBackendSrv().get('/api/dashboards/home');
@@ -130,6 +129,7 @@ export function initDashboard({
       }
     } catch (err) {
       dispatch(setDashboardLoadingState(DashboardLoadingState.Error));
+      dispatch(notifyApp(createErrorNotification('Dashboard fetch failed', err)));
       console.log(err);
       return;
     }
@@ -143,6 +143,7 @@ export function initDashboard({
       dashboard = new DashboardModel(dashDTO.dashboard, dashDTO.meta);
     } catch (err) {
       dispatch(setDashboardLoadingState(DashboardLoadingState.Error));
+      dispatch(notifyApp(createErrorNotification('Dashboard model initializing failure', err)));
       console.log(err);
       return;
     }
@@ -168,7 +169,7 @@ export function initDashboard({
     try {
       await variableSrv.init(dashboard);
     } catch (err) {
-      dispatch(notifyApp(createErrorNotification('Templating init failed')));
+      dispatch(notifyApp(createErrorNotification('Templating init failed', err)));
       console.log(err);
     }
 
@@ -194,7 +195,7 @@ export function initDashboard({
 
       keybindingSrv.setupDashboardBindings($scope, dashboard, onRemovePanel);
     } catch (err) {
-      dispatch(notifyApp(createErrorNotification('Dashboard init failed', err.toString())));
+      dispatch(notifyApp(createErrorNotification('Dashboard init failed', err)));
       console.log(err);
     }