Skip to content

Commit

Permalink
improves ErrorMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
MGJamJam committed Apr 4, 2024
1 parent e8a55bc commit 5cf7556
Showing 1 changed file with 35 additions and 18 deletions.
53 changes: 35 additions & 18 deletions bitmovin-analytics-datasource/src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
FieldType,
} from '@grafana/data';
import { getBackendSrv } from '@grafana/runtime';
import { lastValueFrom } from 'rxjs';
import { catchError, lastValueFrom, map, of, Observable } from 'rxjs';

import { MyQuery, MyDataSourceOptions } from './types';

Expand Down Expand Up @@ -51,26 +51,43 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
headers: { 'X-Api-Key': this.apiKey },
method: method,
};
const response = getBackendSrv().fetch(options);

return lastValueFrom(response);
return getBackendSrv().fetch(options);
}

async testDatasource() {
const response = await this.request('/analytics/licenses', 'GET');
return lastValueFrom(
this.request('/analytics/licenses', 'GET').pipe(
map(() => {
return {
status: 'success',
message: 'Data source successfully setup and connected.',
};
}),
catchError((err) => {
let message = 'Bitmovin: ';
if (err.status) message += err.status + ' ';
if (err.statusText) {
message += err.statusText;
} else {
message += 'Can not connect to Bitmovin API';
}

let errorMessage = err.data?.message || err.data?.data?.message;

//additional errorDetails like requestId and timestamp if requestId is set
let errorDetails;
if (err.data?.requestId) {
errorDetails = 'Timestamp: ' + new Date().toISOString();
errorDetails += err.data?.requestId ? '\nRequestId: ' + err.data?.requestId : '';
}

if (response.status === 200) {
return {
status: 'success',
message: 'Datasource setup successfully.',
};
} else {
return {
status: 'error',
message: response.statusText
? response.statusText
: 'An unexpected error occurred. Datasource not setup correctly.',
};
}
return of({
status: 'error',
message: message,
details: { message: errorMessage, verboseMessage: errorDetails },
});
})
)
);
}
}

0 comments on commit 5cf7556

Please sign in to comment.