Skip to content

Commit

Permalink
Log and fallback to default on context resolution failure
Browse files Browse the repository at this point in the history
  • Loading branch information
davismcphee committed May 30, 2024
1 parent 4321c75 commit faa68c2
Showing 1 changed file with 47 additions and 3 deletions.
50 changes: 47 additions & 3 deletions src/plugins/discover/public/context_awareness/profiles_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { isOfAggregateQueryType } from '@kbn/es-query';
import { isEqual, memoize } from 'lodash';
import { BehaviorSubject, combineLatest, map } from 'rxjs';
import { DataSourceType, isDataSourceType } from '../../common/data_sources';
import { addLog } from '../utils/add_log';
import type {
RootProfileService,
DataSourceProfileService,
Expand Down Expand Up @@ -70,7 +71,13 @@ export class ProfilesManager {
this.rootProfileAbortController?.abort();
this.rootProfileAbortController = abortController;

const context = await this.rootProfileService.resolve(params);
let context = this.rootProfileService.defaultContext;

try {
context = await this.rootProfileService.resolve(params);
} catch (e) {
logResolutionError(ContextType.Root, serializedParams, e);
}

if (abortController.signal.aborted) {
return;
Expand All @@ -91,7 +98,13 @@ export class ProfilesManager {
this.dataSourceProfileAbortController?.abort();
this.dataSourceProfileAbortController = abortController;

const context = await this.dataSourceProfileService.resolve(params);
let context = this.dataSourceProfileService.defaultContext;

try {
context = await this.dataSourceProfileService.resolve(params);
} catch (e) {
logResolutionError(ContextType.DataSource, serializedParams, e);
}

if (abortController.signal.aborted) {
return;
Expand All @@ -103,7 +116,17 @@ export class ProfilesManager {

public resolveDocumentProfile(params: DocumentProfileProviderParams) {
Object.defineProperty(params.record, 'context', {
get: memoize(() => this.documentProfileService.resolve(params)),
get: memoize(() => {
let context = this.documentProfileService.defaultContext;

try {
context = this.documentProfileService.resolve(params);
} catch (e) {
logResolutionError(ContextType.Document, { recordId: params.record.id }, e);
}

return context;
}),
});
}

Expand Down Expand Up @@ -152,3 +175,24 @@ const recordHasContext = (
): record is DataTableRecordWithContext => {
return Boolean(record && 'context' in record);
};

enum ContextType {
Root = 'root',
DataSource = 'data source',
Document = 'document',
}

const logResolutionError = <TParams, TError>(
profileType: ContextType,
params: TParams,
error: TError
) => {
addLog(
`[ProfilesManager] ${profileType} context resolution failed with params: ${JSON.stringify(
params,
null,
2
)}`,
error
);
};

0 comments on commit faa68c2

Please sign in to comment.