浏览代码

utils: show string errors. Fixes #15782

Daniel Lee 6 年之前
父节点
当前提交
8b1e25b50a
共有 2 个文件被更改,包括 56 次插入1 次删除
  1. 55 0
      public/app/core/utils/errors.test.ts
  2. 1 1
      public/app/core/utils/errors.ts

+ 55 - 0
public/app/core/utils/errors.test.ts

@@ -0,0 +1,55 @@
+import { getMessageFromError } from 'app/core/utils/errors';
+
+describe('errors functions', () => {
+  let message;
+
+  describe('when getMessageFromError gets an error string', () => {
+    beforeEach(() => {
+      message = getMessageFromError('error string');
+    });
+
+    it('should return the string', () => {
+      expect(message).toBe('error string');
+    });
+  });
+
+  describe('when getMessageFromError gets an error object with message field', () => {
+    beforeEach(() => {
+      message = getMessageFromError({ message: 'error string' });
+    });
+
+    it('should return the message text', () => {
+      expect(message).toBe('error string');
+    });
+  });
+
+  describe('when getMessageFromError gets an error object with data.message field', () => {
+    beforeEach(() => {
+      message = getMessageFromError({ data: { message: 'error string' } });
+    });
+
+    it('should return the message text', () => {
+      expect(message).toBe('error string');
+    });
+  });
+
+  describe('when getMessageFromError gets an error object with statusText field', () => {
+    beforeEach(() => {
+      message = getMessageFromError({ statusText: 'error string' });
+    });
+
+    it('should return the statusText text', () => {
+      expect(message).toBe('error string');
+    });
+  });
+
+  describe('when getMessageFromError gets an error object', () => {
+    beforeEach(() => {
+      message = getMessageFromError({ customError: 'error string' });
+    });
+
+    it('should return the stringified error', () => {
+      expect(message).toBe('{"customError":"error string"}');
+    });
+  });
+});

+ 1 - 1
public/app/core/utils/errors.ts

@@ -13,5 +13,5 @@ export function getMessageFromError(err: any): string | null {
     }
   }
 
-  return null;
+  return err;
 }