From 24bc2b1cf4347904796b4f831dd7595d3f42cfcb Mon Sep 17 00:00:00 2001 From: Dominik Prokop Date: Tue, 22 Dec 2020 13:26:58 +0100 Subject: [PATCH] TemplateSrv: Do not throw error for an unknown format but use glob as fallback and warn in the console (#29955) --- public/app/features/templating/template_srv.test.ts | 7 +++++++ public/app/features/templating/template_srv.ts | 6 ++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/public/app/features/templating/template_srv.test.ts b/public/app/features/templating/template_srv.test.ts index 74105e7012f..ab973a3301f 100644 --- a/public/app/features/templating/template_srv.test.ts +++ b/public/app/features/templating/template_srv.test.ts @@ -295,6 +295,13 @@ describe('templateSrv', () => { expect(result).toBe('test'); }); + it('should use glob format when unknown format provided', () => { + let result = _templateSrv.formatValue('test', 'nonexistentformat'); + expect(result).toBe('test'); + result = _templateSrv.formatValue(['test', 'test1'], 'nonexistentformat'); + expect(result).toBe('{test,test1}'); + }); + it('multi value and glob format should render glob string', () => { const result = _templateSrv.formatValue(['test', 'test2'], 'glob'); expect(result).toBe('{test,test2}'); diff --git a/public/app/features/templating/template_srv.ts b/public/app/features/templating/template_srv.ts index 812dbde0c2e..8fb002c63e0 100644 --- a/public/app/features/templating/template_srv.ts +++ b/public/app/features/templating/template_srv.ts @@ -137,9 +137,11 @@ export class TemplateSrv implements BaseTemplateSrv { args = []; } - const formatItem = formatRegistry.getIfExists(format); + let formatItem = formatRegistry.getIfExists(format); + if (!formatItem) { - throw new Error(`Variable format ${format} not found`); + console.error(`Variable format ${format} not found. Using glob format as fallback.`); + formatItem = formatRegistry.get('glob'); } const options: FormatOptions = { value, args, text: text ?? value };