From 3c1346d83adf376fa66934d95b66c4eb35563556 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Fri, 17 Jul 2020 17:01:21 +0200 Subject: [PATCH] BackendSrv: Fix error alert logic (#26411) --- public/app/core/services/backend_srv.ts | 2 +- public/app/core/specs/backend_srv.test.ts | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/public/app/core/services/backend_srv.ts b/public/app/core/services/backend_srv.ts index 6518b37874c..f873b399717 100644 --- a/public/app/core/services/backend_srv.ts +++ b/public/app/core/services/backend_srv.ts @@ -219,7 +219,7 @@ export class BackendSrv implements BackendService { } // is showErrorAlert is undefined we only show alerts non data query and local api requests - if (config.showErrorAlert === undefined || isDataQuery(config.url) || !isLocalUrl(config.url)) { + if (config.showErrorAlert === undefined && (isDataQuery(config.url) || !isLocalUrl(config.url))) { return; } diff --git a/public/app/core/specs/backend_srv.test.ts b/public/app/core/specs/backend_srv.test.ts index 6cd022c9cc3..7aa9035f0d7 100644 --- a/public/app/core/specs/backend_srv.test.ts +++ b/public/app/core/specs/backend_srv.test.ts @@ -7,6 +7,7 @@ import { BackendSrv } from '../services/backend_srv'; import { Emitter } from '../utils/emitter'; import { ContextSrv, User } from '../services/context_srv'; import { describe, expect } from '../../../test/lib/common'; +import { BackendSrvRequest, FetchError } from '@grafana/runtime'; const getTestContext = (overides?: object) => { const defaults = { @@ -217,6 +218,26 @@ describe('backendSrv', () => { }); }); + describe('when showing error alert', () => { + describe('when showErrorAlert is undefined and url is a normal api call', () => { + it('It should emit alert event for normal api errors', async () => { + const { backendSrv, appEventsMock } = getTestContext({}); + backendSrv.showErrorAlert( + { + url: 'api/do/something', + } as BackendSrvRequest, + { + data: { + message: 'Something failed', + error: 'Error', + }, + } as FetchError + ); + expect(appEventsMock.emit).toHaveBeenCalledWith(AppEvents.alertError, ['Something failed', '']); + }); + }); + }); + describe('when making an unsuccessful 422 call', () => { it('then it should emit Validation failed message', async () => { jest.useFakeTimers();