Commit d3dcb19a by Ivana Huckova Committed by GitHub

Loki: Improve error message for unescaped \ and add LogQL link to docs (#26136)

* Add custom escaping error message

* Include regex escape error message

* Update docs, add logql link

* Refactor
parent 45cfb345
......@@ -59,7 +59,7 @@ The new field with the link shown in log details:
## Querying Logs
Querying and displaying log data from Loki is available via [Explore]({{< relref "../explore" >}}), and with the [logs panel]({{< relref "../../panels/visualizations/logs-panel.md" >}}) in dashboards. Select the Loki data source, and then enter a log query to display your logs.
Querying and displaying log data from Loki is available via [Explore]({{< relref "../explore" >}}), and with the [logs panel]({{< relref "../../panels/visualizations/logs-panel.md" >}}) in dashboards. Select the Loki data source, and then enter a [LogQL](https://github.com/grafana/loki/blob/master/docs/logql.md) query to display your logs.
### Log Queries
......
......@@ -189,6 +189,33 @@ describe('LokiDatasource', () => {
expect(dataFrame.meta?.limit).toBe(20);
expect(dataFrame.meta?.searchWords).toEqual(['foo']);
});
test('should return custom error message when Loki returns escaping error', async () => {
const customData = { ...(instanceSettings.jsonData || {}), maxLines: 20 };
const customSettings = { ...instanceSettings, jsonData: customData };
const ds = new LokiDatasource(customSettings, templateSrvMock);
datasourceRequestMock.mockImplementation(
jest.fn().mockReturnValueOnce(
Promise.reject({
data: 'parse error at line 1, col 6: invalid char escape',
status: 400,
statusText: 'Bad Request',
})
)
);
const options = getQueryOptions<LokiQuery>({
targets: [{ expr: '{job="gra\\fana"}', refId: 'B' }],
});
try {
await ds.query(options).toPromise();
} catch (err) {
expect(err.message).toBe(
'Error: parse error at line 1, col 6: invalid char escape. Make sure that all special characters are escaped with \\. For more information on escaping of special characters visit LogQL documentation at https://github.com/grafana/loki/blob/master/docs/logql.md.'
);
}
});
});
describe('When interpolating variables', () => {
......
......@@ -537,7 +537,11 @@ export class LokiDatasource extends DataSourceApi<LokiQuery, LokiOptions> {
if (err.data) {
if (typeof err.data === 'string') {
error.message = err.data;
if (err.data.includes('escape') && target.expr.includes('\\')) {
error.message = `Error: ${err.data}. Make sure that all special characters are escaped with \\. For more information on escaping of special characters visit LogQL documentation at https://github.com/grafana/loki/blob/master/docs/logql.md.`;
} else {
error.message = err.data;
}
} else if (err.data.error) {
error.message = safeStringifyValue(err.data.error);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment