From 40898e1cd61fe1382334ed31be10e543caa96ee0 Mon Sep 17 00:00:00 2001 From: Daniel Lee Date: Thu, 6 Dec 2018 00:19:55 +0100 Subject: [PATCH] loki: adds proper error handling for config page This handles three different error cases for the config page so that the user gets an error and not just a blank red error box. --- .../datasource/loki/datasource.test.ts | 98 +++++++++++++++++++ .../app/plugins/datasource/loki/datasource.ts | 18 +++- 2 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 public/app/plugins/datasource/loki/datasource.test.ts diff --git a/public/app/plugins/datasource/loki/datasource.test.ts b/public/app/plugins/datasource/loki/datasource.test.ts new file mode 100644 index 00000000000..ddb4d6ed549 --- /dev/null +++ b/public/app/plugins/datasource/loki/datasource.test.ts @@ -0,0 +1,98 @@ +import LokiDatasource from './datasource'; + +describe('LokiDatasource', () => { + const instanceSettings = { + url: 'myloggingurl', + }; + + describe('when performing testDataSource', () => { + let ds; + let result; + + describe('and call succeeds', () => { + beforeEach(async () => { + const backendSrv = { + async datasourceRequest() { + return Promise.resolve({ + status: 200, + data: { + values: ['avalue'], + }, + }); + }, + }; + ds = new LokiDatasource(instanceSettings, backendSrv, {}); + result = await ds.testDatasource(); + }); + + it('should return successfully', () => { + expect(result.status).toBe('success'); + }); + }); + + describe('and call fails with 401 error', () => { + beforeEach(async () => { + const backendSrv = { + async datasourceRequest() { + return Promise.reject({ + statusText: 'Unauthorized', + status: 401, + data: { + message: 'Unauthorized', + }, + }); + }, + }; + ds = new LokiDatasource(instanceSettings, backendSrv, {}); + result = await ds.testDatasource(); + }); + + it('should return error status and a detailed error message', () => { + expect(result.status).toEqual('error'); + expect(result.message).toBe('Loki: Unauthorized. 401. Unauthorized'); + }); + }); + + describe('and call fails with 404 error', () => { + beforeEach(async () => { + const backendSrv = { + async datasourceRequest() { + return Promise.reject({ + statusText: 'Not found', + status: 404, + data: '404 page not found', + }); + }, + }; + ds = new LokiDatasource(instanceSettings, backendSrv, {}); + result = await ds.testDatasource(); + }); + + it('should return error status and a detailed error message', () => { + expect(result.status).toEqual('error'); + expect(result.message).toBe('Loki: Not found. 404. 404 page not found'); + }); + }); + + describe('and call fails with 502 error', () => { + beforeEach(async () => { + const backendSrv = { + async datasourceRequest() { + return Promise.reject({ + statusText: 'Bad Gateway', + status: 502, + data: '', + }); + }, + }; + ds = new LokiDatasource(instanceSettings, backendSrv, {}); + result = await ds.testDatasource(); + }); + + it('should return error status and a detailed error message', () => { + expect(result.status).toEqual('error'); + expect(result.message).toBe('Loki: Bad Gateway. 502'); + }); + }); + }); +}); diff --git a/public/app/plugins/datasource/loki/datasource.ts b/public/app/plugins/datasource/loki/datasource.ts index 9c5ab450542..ebe1e226a75 100644 --- a/public/app/plugins/datasource/loki/datasource.ts +++ b/public/app/plugins/datasource/loki/datasource.ts @@ -140,7 +140,23 @@ export default class LokiDatasource { }; }) .catch(err => { - return { status: 'error', message: err.message }; + let message = 'Loki: '; + if (err.statusText) { + message += err.statusText; + } else { + message += 'Cannot connect to Loki'; + } + + if (err.status) { + message += `. ${err.status}`; + } + + if (err.data && err.data.message) { + message += `. ${err.data.message}`; + } else if (err.data) { + message += `. ${err.data}`; + } + return { status: 'error', message: message }; }); } }