Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allows Alerts to recover gracefully from Executor errors #53688

Merged
merged 16 commits into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions x-pack/legacy/plugins/alerting/server/lib/result_type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

export interface Ok<T> {
tag: 'ok';
value: T;
}

export interface Err<E> {
tag: 'err';
error: E;
}
export type Result<T, E> = Ok<T> | Err<E>;

export function asOk<T>(value: T): Ok<T> {
return {
tag: 'ok',
value,
};
}

export function asErr<T>(error: T): Err<T> {
return {
tag: 'err',
error,
};
}

export function isOk<T, E>(result: Result<T, E>): result is Ok<T> {
return result.tag === 'ok';
}

export function isErr<T, E>(result: Result<T, E>): result is Err<E> {
return !isOk(result);
}

export async function promiseResult<T, E>(future: Promise<T>): Promise<Result<T, E>> {
try {
return asOk(await future);
} catch (e) {
return asErr(e);
}
}

export function map<T, E, Resolution>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map doesn't seem like a great name for this function; processResult(), handleResult(), something like that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually a standard name for this operation in FP... it's mapping the Result type, be it an Ok or an Err into a specific type.
And TBH I don't feel process and handle tell me anything about what is happening here.

result: Result<T, E>,
onOk: (value: T) => Resolution,
onErr: (error: E) => Resolution
): Resolution {
return isOk(result) ? onOk(result.value) : onErr(result.error);
}
Loading