TemplateSrv: Do not throw error for an unknown format but use glob as fallback and warn in the console (#29955)

pull/29954/head^2
Dominik Prokop 4 years ago committed by GitHub
parent 35a755fe50
commit 24bc2b1cf4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      public/app/features/templating/template_srv.test.ts
  2. 6
      public/app/features/templating/template_srv.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}');

@ -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 };

Loading…
Cancel
Save