From d5956ce4b79a8e987944bb353bea12e250db5562 Mon Sep 17 00:00:00 2001 From: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> Date: Mon, 6 Sep 2021 09:28:54 -0400 Subject: [PATCH] Loki: Fix error handling, remove legacy condition (#38633) * Fix error handling, remove legacy condition * Fix strict errors in Loki and lower error count * Update --- .../app/plugins/datasource/loki/datasource.ts | 33 +++++++------------ scripts/ci-check-strict.sh | 2 +- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/public/app/plugins/datasource/loki/datasource.ts b/public/app/plugins/datasource/loki/datasource.ts index 3f59efb49ea..eb431ca3d0b 100644 --- a/public/app/plugins/datasource/loki/datasource.ts +++ b/public/app/plugins/datasource/loki/datasource.ts @@ -42,9 +42,9 @@ import { LokiOptions, LokiQuery, LokiRangeQueryRequest, - LokiResponse, LokiResultType, LokiStreamResponse, + LokiStreamResult, } from './types'; import { LiveStreams, LokiLiveTarget } from './live_streams'; import LanguageProvider from './language_provider'; @@ -156,7 +156,7 @@ export class LokiDatasource extends DataSourceApi { }; return this._request(INSTANT_QUERY_ENDPOINT, query).pipe( - map((response: { data: LokiResponse }) => { + map((response) => { if (response.data.data.resultType === LokiResultType.Stream) { return { data: response.data @@ -176,7 +176,7 @@ export class LokiDatasource extends DataSourceApi { key: `${target.refId}_instant`, }; }), - catchError((err: any) => this.throwUnless(err, err.status === 404, target)) + catchError((err) => throwError(() => this.processError(err, target))) ); }; @@ -236,8 +236,8 @@ export class LokiDatasource extends DataSourceApi { const query = this.createRangeQuery(target, options, maxDataPoints); return this._request(RANGE_QUERY_ENDPOINT, query).pipe( - catchError((err: any) => this.throwUnless(err, err.status === 404, target)), - switchMap((response: { data: LokiResponse; status: number }) => + catchError((err) => throwError(() => this.processError(err, target))), + switchMap((response) => processRangeQueryResponse( response.data, target, @@ -281,7 +281,7 @@ export class LokiDatasource extends DataSourceApi { state: LoadingState.Streaming, })), catchError((err: any) => { - return throwError(`Live tailing was stopped due to following error: ${err.reason}`); + return throwError(() => `Live tailing was stopped due to following error: ${err.reason}`); }) ); }; @@ -451,11 +451,7 @@ export class LokiDatasource extends DataSourceApi { const reverse = options && options.direction === 'FORWARD'; return lastValueFrom( this._request(RANGE_QUERY_ENDPOINT, target).pipe( - catchError((err: any) => { - if (err.status === 404) { - return of(err); - } - + catchError((err) => { const error: DataQueryError = { message: 'Error during context query. Please check JS console logs.', status: err.status, @@ -463,9 +459,11 @@ export class LokiDatasource extends DataSourceApi { }; throw error; }), - switchMap((res: { data: LokiStreamResponse; status: number }) => + switchMap((res) => of({ - data: res.data ? res.data.data.result.map((stream) => lokiStreamResultToDataFrame(stream, reverse)) : [], + data: res.data + ? res.data.data.result.map((stream: LokiStreamResult) => lokiStreamResultToDataFrame(stream, reverse)) + : [], }) ) ) @@ -625,15 +623,6 @@ export class LokiDatasource extends DataSourceApi { return (row && row.searchWords && row.searchWords.length > 0) === true; } - throwUnless(err: FetchError, condition: boolean, target: LokiQuery) { - if (condition) { - return of(err); - } - - const error = this.processError(err, target); - throw error; - } - processError(err: FetchError, target: LokiQuery) { let error = cloneDeep(err); if (err.data.message.includes('escape') && target.expr.includes('\\')) { diff --git a/scripts/ci-check-strict.sh b/scripts/ci-check-strict.sh index a17872feb7b..fd56372326f 100755 --- a/scripts/ci-check-strict.sh +++ b/scripts/ci-check-strict.sh @@ -3,7 +3,7 @@ set -e echo -e "Collecting code stats (typescript errors & more)" -ERROR_COUNT_LIMIT=49 +ERROR_COUNT_LIMIT=47 ERROR_COUNT="$(./node_modules/.bin/tsc --project tsconfig.json --noEmit --strict true | grep -oP 'Found \K(\d+)')" if [ "$ERROR_COUNT" -gt $ERROR_COUNT_LIMIT ]; then