Skip to content

Commit b3f1282

Browse files
committed
Add isErrorThatHandlesItsOwnResponse
1 parent 375131e commit b3f1282

File tree

7 files changed

+32
-8
lines changed

7 files changed

+32
-8
lines changed

x-pack/plugins/actions/server/lib/errors/action_type_disabled.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
*/
66

77
import { KibanaResponseFactory } from '../../../../../../src/core/server';
8+
import { ErrorThatHandlesItsOwnResponse } from './types';
89

910
export type ActionTypeDisabledReason =
1011
| 'config'
1112
| 'license_unavailable'
1213
| 'license_invalid'
1314
| 'license_expired';
1415

15-
export class ActionTypeDisabledError extends Error {
16+
export class ActionTypeDisabledError extends Error implements ErrorThatHandlesItsOwnResponse {
1617
public readonly reason: ActionTypeDisabledReason;
1718

1819
constructor(message: string, reason: ActionTypeDisabledReason) {

x-pack/plugins/actions/server/lib/errors/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,12 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7+
import { ErrorThatHandlesItsOwnResponse } from './types';
8+
9+
export function isErrorThatHandlesItsOwnResponse(
10+
e: ErrorThatHandlesItsOwnResponse
11+
): e is ErrorThatHandlesItsOwnResponse {
12+
return typeof (e as ErrorThatHandlesItsOwnResponse).sendResponse === 'function';
13+
}
14+
715
export { ActionTypeDisabledError, ActionTypeDisabledReason } from './action_type_disabled';
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { KibanaResponseFactory, IKibanaResponse } from '../../../../../../src/core/server';
8+
9+
export interface ErrorThatHandlesItsOwnResponse extends Error {
10+
sendResponse(res: KibanaResponseFactory): IKibanaResponse;
11+
}

x-pack/plugins/actions/server/lib/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ export { TaskRunnerFactory } from './task_runner_factory';
1010
export { ActionExecutor, ActionExecutorContract } from './action_executor';
1111
export { ILicenseState, LicenseState } from './license_state';
1212
export { verifyApiAccess } from './verify_api_access';
13-
export { ActionTypeDisabledError, ActionTypeDisabledReason } from './errors';
13+
export {
14+
ActionTypeDisabledError,
15+
ActionTypeDisabledReason,
16+
isErrorThatHandlesItsOwnResponse,
17+
} from './errors';

x-pack/plugins/actions/server/routes/create.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
KibanaResponseFactory,
1414
} from 'kibana/server';
1515
import { ActionResult } from '../types';
16-
import { ActionTypeDisabledError, ILicenseState, verifyApiAccess } from '../lib';
16+
import { ILicenseState, verifyApiAccess, isErrorThatHandlesItsOwnResponse } from '../lib';
1717

1818
export const bodySchema = schema.object({
1919
name: schema.string(),
@@ -51,7 +51,7 @@ export const createActionRoute = (router: IRouter, licenseState: ILicenseState)
5151
body: actionRes,
5252
});
5353
} catch (e) {
54-
if (e instanceof ActionTypeDisabledError) {
54+
if (isErrorThatHandlesItsOwnResponse(e)) {
5555
return e.sendResponse(res);
5656
}
5757
throw e;

x-pack/plugins/actions/server/routes/execute.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
IKibanaResponse,
1212
KibanaResponseFactory,
1313
} from 'kibana/server';
14-
import { ILicenseState, verifyApiAccess, ActionTypeDisabledError } from '../lib';
14+
import { ILicenseState, verifyApiAccess, isErrorThatHandlesItsOwnResponse } from '../lib';
1515

1616
import { ActionExecutorContract } from '../lib';
1717
import { ActionTypeExecutorResult } from '../types';
@@ -60,7 +60,7 @@ export const executeActionRoute = (
6060
})
6161
: res.noContent();
6262
} catch (e) {
63-
if (e instanceof ActionTypeDisabledError) {
63+
if (isErrorThatHandlesItsOwnResponse(e)) {
6464
return e.sendResponse(res);
6565
}
6666
throw e;

x-pack/plugins/actions/server/routes/update.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
IKibanaResponse,
1313
KibanaResponseFactory,
1414
} from 'kibana/server';
15-
import { ActionTypeDisabledError, ILicenseState, verifyApiAccess } from '../lib';
15+
import { ILicenseState, verifyApiAccess, isErrorThatHandlesItsOwnResponse } from '../lib';
1616

1717
const paramSchema = schema.object({
1818
id: schema.string(),
@@ -57,7 +57,7 @@ export const updateActionRoute = (router: IRouter, licenseState: ILicenseState)
5757
}),
5858
});
5959
} catch (e) {
60-
if (e instanceof ActionTypeDisabledError) {
60+
if (isErrorThatHandlesItsOwnResponse(e)) {
6161
return e.sendResponse(res);
6262
}
6363
throw e;

0 commit comments

Comments
 (0)