From b3f1282f5e1eda6ae2023e262384d6f244c1fbd5 Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Thu, 19 Mar 2020 16:05:17 -0400 Subject: [PATCH] Add isErrorThatHandlesItsOwnResponse --- .../actions/server/lib/errors/action_type_disabled.ts | 3 ++- x-pack/plugins/actions/server/lib/errors/index.ts | 8 ++++++++ x-pack/plugins/actions/server/lib/errors/types.ts | 11 +++++++++++ x-pack/plugins/actions/server/lib/index.ts | 6 +++++- x-pack/plugins/actions/server/routes/create.ts | 4 ++-- x-pack/plugins/actions/server/routes/execute.ts | 4 ++-- x-pack/plugins/actions/server/routes/update.ts | 4 ++-- 7 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 x-pack/plugins/actions/server/lib/errors/types.ts diff --git a/x-pack/plugins/actions/server/lib/errors/action_type_disabled.ts b/x-pack/plugins/actions/server/lib/errors/action_type_disabled.ts index 19259854c3ac3e..fb15125fa6957c 100644 --- a/x-pack/plugins/actions/server/lib/errors/action_type_disabled.ts +++ b/x-pack/plugins/actions/server/lib/errors/action_type_disabled.ts @@ -5,6 +5,7 @@ */ import { KibanaResponseFactory } from '../../../../../../src/core/server'; +import { ErrorThatHandlesItsOwnResponse } from './types'; export type ActionTypeDisabledReason = | 'config' @@ -12,7 +13,7 @@ export type ActionTypeDisabledReason = | 'license_invalid' | 'license_expired'; -export class ActionTypeDisabledError extends Error { +export class ActionTypeDisabledError extends Error implements ErrorThatHandlesItsOwnResponse { public readonly reason: ActionTypeDisabledReason; constructor(message: string, reason: ActionTypeDisabledReason) { diff --git a/x-pack/plugins/actions/server/lib/errors/index.ts b/x-pack/plugins/actions/server/lib/errors/index.ts index 9549569cb418a9..79c6d53c403ffc 100644 --- a/x-pack/plugins/actions/server/lib/errors/index.ts +++ b/x-pack/plugins/actions/server/lib/errors/index.ts @@ -4,4 +4,12 @@ * you may not use this file except in compliance with the Elastic License. */ +import { ErrorThatHandlesItsOwnResponse } from './types'; + +export function isErrorThatHandlesItsOwnResponse( + e: ErrorThatHandlesItsOwnResponse +): e is ErrorThatHandlesItsOwnResponse { + return typeof (e as ErrorThatHandlesItsOwnResponse).sendResponse === 'function'; +} + export { ActionTypeDisabledError, ActionTypeDisabledReason } from './action_type_disabled'; diff --git a/x-pack/plugins/actions/server/lib/errors/types.ts b/x-pack/plugins/actions/server/lib/errors/types.ts new file mode 100644 index 00000000000000..949dc348265ae8 --- /dev/null +++ b/x-pack/plugins/actions/server/lib/errors/types.ts @@ -0,0 +1,11 @@ +/* + * 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. + */ + +import { KibanaResponseFactory, IKibanaResponse } from '../../../../../../src/core/server'; + +export interface ErrorThatHandlesItsOwnResponse extends Error { + sendResponse(res: KibanaResponseFactory): IKibanaResponse; +} diff --git a/x-pack/plugins/actions/server/lib/index.ts b/x-pack/plugins/actions/server/lib/index.ts index d6cf3688f93d49..f03b6de1fc5fbc 100644 --- a/x-pack/plugins/actions/server/lib/index.ts +++ b/x-pack/plugins/actions/server/lib/index.ts @@ -10,4 +10,8 @@ export { TaskRunnerFactory } from './task_runner_factory'; export { ActionExecutor, ActionExecutorContract } from './action_executor'; export { ILicenseState, LicenseState } from './license_state'; export { verifyApiAccess } from './verify_api_access'; -export { ActionTypeDisabledError, ActionTypeDisabledReason } from './errors'; +export { + ActionTypeDisabledError, + ActionTypeDisabledReason, + isErrorThatHandlesItsOwnResponse, +} from './errors'; diff --git a/x-pack/plugins/actions/server/routes/create.ts b/x-pack/plugins/actions/server/routes/create.ts index 263218fbafbecb..0456fa8667de3c 100644 --- a/x-pack/plugins/actions/server/routes/create.ts +++ b/x-pack/plugins/actions/server/routes/create.ts @@ -13,7 +13,7 @@ import { KibanaResponseFactory, } from 'kibana/server'; import { ActionResult } from '../types'; -import { ActionTypeDisabledError, ILicenseState, verifyApiAccess } from '../lib'; +import { ILicenseState, verifyApiAccess, isErrorThatHandlesItsOwnResponse } from '../lib'; export const bodySchema = schema.object({ name: schema.string(), @@ -51,7 +51,7 @@ export const createActionRoute = (router: IRouter, licenseState: ILicenseState) body: actionRes, }); } catch (e) { - if (e instanceof ActionTypeDisabledError) { + if (isErrorThatHandlesItsOwnResponse(e)) { return e.sendResponse(res); } throw e; diff --git a/x-pack/plugins/actions/server/routes/execute.ts b/x-pack/plugins/actions/server/routes/execute.ts index 2743050e34937d..78693b5bfcf237 100644 --- a/x-pack/plugins/actions/server/routes/execute.ts +++ b/x-pack/plugins/actions/server/routes/execute.ts @@ -11,7 +11,7 @@ import { IKibanaResponse, KibanaResponseFactory, } from 'kibana/server'; -import { ILicenseState, verifyApiAccess, ActionTypeDisabledError } from '../lib'; +import { ILicenseState, verifyApiAccess, isErrorThatHandlesItsOwnResponse } from '../lib'; import { ActionExecutorContract } from '../lib'; import { ActionTypeExecutorResult } from '../types'; @@ -60,7 +60,7 @@ export const executeActionRoute = ( }) : res.noContent(); } catch (e) { - if (e instanceof ActionTypeDisabledError) { + if (isErrorThatHandlesItsOwnResponse(e)) { return e.sendResponse(res); } throw e; diff --git a/x-pack/plugins/actions/server/routes/update.ts b/x-pack/plugins/actions/server/routes/update.ts index 03d5e428ca37aa..692693f0106658 100644 --- a/x-pack/plugins/actions/server/routes/update.ts +++ b/x-pack/plugins/actions/server/routes/update.ts @@ -12,7 +12,7 @@ import { IKibanaResponse, KibanaResponseFactory, } from 'kibana/server'; -import { ActionTypeDisabledError, ILicenseState, verifyApiAccess } from '../lib'; +import { ILicenseState, verifyApiAccess, isErrorThatHandlesItsOwnResponse } from '../lib'; const paramSchema = schema.object({ id: schema.string(), @@ -57,7 +57,7 @@ export const updateActionRoute = (router: IRouter, licenseState: ILicenseState) }), }); } catch (e) { - if (e instanceof ActionTypeDisabledError) { + if (isErrorThatHandlesItsOwnResponse(e)) { return e.sendResponse(res); } throw e;