Loki,Prometheus: Fix of showing error message for empty query (#47379)

* Loki,Prometheus: Dont show error on empty query

* Add tests
This commit is contained in:
Ivana Huckova
2022-04-06 11:17:49 +02:00
committed by GitHub
parent c72881a8b1
commit caa82a124d
5 changed files with 55 additions and 5 deletions

View File

@ -20,8 +20,8 @@ interface Context {
interface ParsingError {
text: string;
from: number;
to: number;
from?: number;
to?: number;
parentType?: string;
}
@ -36,12 +36,25 @@ export function buildVisualQueryFromString(expr: string): Context {
operations: [],
};
const context = {
const context: Context = {
query: visQuery,
errors: [],
};
handleExpression(replacedExpr, node, context);
try {
handleExpression(replacedExpr, node, context);
} catch (err) {
// Not ideal to log it here, but otherwise we would lose the stack trace.
console.error(err);
context.errors.push({
text: err.message,
});
}
// If we have empty query, we want to reset errors
if (isEmptyQuery(context.query)) {
context.errors = [];
}
return context;
}
@ -496,3 +509,10 @@ function createNotSupportedError(expr: string, node: SyntaxNode, error: string)
err.text = `${error}: ${err.text}`;
return err;
}
function isEmptyQuery(query: LokiVisualQuery) {
if (query.labels.length === 0 && query.operations.length === 0) {
return true;
}
return false;
}