Skip to content

Commit

Permalink
[ML] Fix response_stream plugin error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
walterra committed Jun 30, 2022
1 parent e511d8b commit 8843e6e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
16 changes: 15 additions & 1 deletion examples/response_stream/common/api/reducer_stream/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const API_ACTION_NAME = {
UPDATE_PROGRESS: 'update_progress',
ADD_TO_ENTITY: 'add_to_entity',
DELETE_ENTITY: 'delete_entity',
ERROR: 'error',
} as const;
export type ApiActionName = typeof API_ACTION_NAME[keyof typeof API_ACTION_NAME];

Expand Down Expand Up @@ -59,7 +60,20 @@ export function deleteEntityAction(payload: string): ApiActionDeleteEntity {
};
}

interface ApiActionError {
type: typeof API_ACTION_NAME.ERROR;
payload: string;
}

export function errorAction(payload: string): ApiActionError {
return {
type: API_ACTION_NAME.ERROR,
payload,
};
}

export type ReducerStreamApiAction =
| ApiActionUpdateProgress
| ApiActionAddToEntity
| ApiActionDeleteEntity;
| ApiActionDeleteEntity
| ApiActionError;
7 changes: 7 additions & 0 deletions examples/response_stream/common/api/reducer_stream/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ export const UI_ACTION_NAME = {
export type UiActionName = typeof UI_ACTION_NAME[keyof typeof UI_ACTION_NAME];

export interface StreamState {
errors: string[];
progress: number;
entities: Record<string, number>;
}
export const initialState: StreamState = {
errors: [],
progress: 0,
entities: {},
};
Expand Down Expand Up @@ -64,6 +66,11 @@ export function reducerStreamReducer(
...state,
entities: addToEntities,
};
case API_ACTION_NAME.ERROR:
return {
...state,
errors: [...state.errors, action.payload],
};
case UI_ACTION_NAME.RESET:
return initialState;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,20 @@ export const PageReducerStream: FC = () => {
}
};

// This is for low level errors on the stream/HTTP level.
useEffect(() => {
if (error) {
notifications.toasts.addDanger(error);
}
}, [error, notifications.toasts]);

// This is for errors on the application level
useEffect(() => {
if (data.errors.length > 0) {
notifications.toasts.addDanger(data.errors[data.errors.length - 1]);
}
}, [data.errors, notifications.toasts]);

const buttonLabel = isRunning ? 'Stop development' : 'Start development';

return (
Expand Down
13 changes: 7 additions & 6 deletions examples/response_stream/server/routes/reducer_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { IRouter, Logger } from '@kbn/core/server';
import { streamFactory } from '@kbn/aiops-utils';

import {
errorAction,
reducerStreamRequestBodySchema,
updateProgressAction,
addToEntityAction,
Expand Down Expand Up @@ -38,8 +39,9 @@ export const defineReducerStreamRoute = (router: IRouter, logger: Logger) => {
shouldStop = true;
});

const { end, error, push, responseWithHeaders } = streamFactory<ReducerStreamApiAction>(
request.headers
const { end, push, responseWithHeaders } = streamFactory<ReducerStreamApiAction>(
request.headers,
logger
);

const entities = [
Expand Down Expand Up @@ -84,18 +86,17 @@ export const defineReducerStreamRoute = (router: IRouter, logger: Logger) => {
push(deleteEntityAction(randomEntity));
} else if (randomAction === 'throw-error') {
// Throw an error. It should not crash Kibana!
// It should be caught, logged and passed on as a stream error.
// It should be caught and logged to the Kibana server console.
throw new Error('There was a (simulated) server side error!');
} else if (randomAction === 'emit-error') {
// Directly emit an error to the stream, this will not be logged.
error('Error pushed to the stream');
// Emit an error as a stream action.
push(errorAction('(Simulated) error pushed to the stream'));
return;
}

pushStreamUpdate();
} catch (e) {
logger.error(e);
error(e);
}
}, Math.floor(Math.random() * maxTimeoutMs));
}
Expand Down

0 comments on commit 8843e6e

Please sign in to comment.