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

This commit is contained in:
Dominik Prokop
2020-12-22 13:26:58 +01:00
committed by GitHub
parent 35a755fe50
commit 24bc2b1cf4
2 changed files with 11 additions and 2 deletions

View File

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

View File

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