Skip to content

Commit

Permalink
Add isErrorThatHandlesItsOwnResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecote committed Mar 19, 2020
1 parent 375131e commit b3f1282
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
*/

import { KibanaResponseFactory } from '../../../../../../src/core/server';
import { ErrorThatHandlesItsOwnResponse } from './types';

export type ActionTypeDisabledReason =
| 'config'
| 'license_unavailable'
| '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) {
Expand Down
8 changes: 8 additions & 0 deletions x-pack/plugins/actions/server/lib/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
11 changes: 11 additions & 0 deletions x-pack/plugins/actions/server/lib/errors/types.ts
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 5 additions & 1 deletion x-pack/plugins/actions/server/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
4 changes: 2 additions & 2 deletions x-pack/plugins/actions/server/routes/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/actions/server/routes/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -60,7 +60,7 @@ export const executeActionRoute = (
})
: res.noContent();
} catch (e) {
if (e instanceof ActionTypeDisabledError) {
if (isErrorThatHandlesItsOwnResponse(e)) {
return e.sendResponse(res);
}
throw e;
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/actions/server/routes/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit b3f1282

Please sign in to comment.