Skip to content

Commit

Permalink
use properly typed errors for obs
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Jun 2, 2020
1 parent 9ba28ac commit fa3b8c0
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
22 changes: 22 additions & 0 deletions x-pack/plugins/global_search/common/errors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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 { GlobalSearchFindError } from './errors';

describe('GlobalSearchFindError', () => {
describe('#invalidLicense', () => {
it('create an error with the correct `type`', () => {
const error = GlobalSearchFindError.invalidLicense('foobar');
expect(error.message).toBe('foobar');
expect(error.type).toBe('invalid-license');
});

it('can be identified via instanceof', () => {
const error = GlobalSearchFindError.invalidLicense('foo');
expect(error instanceof GlobalSearchFindError).toBe(true);
});
});
});
27 changes: 27 additions & 0 deletions x-pack/plugins/global_search/common/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.
*/

// only one type for now, but already present for future-proof reasons
export type GlobalSearchFindErrorType = 'invalid-license';

/**
* Error thrown from the {@link GlobalSearchPluginStart.find | GlobalSearch find API}'s result observable
*
* @public
*/
export class GlobalSearchFindError extends Error {
public static invalidLicense(message: string) {
return new GlobalSearchFindError('invalid-license', message);
}

private constructor(public readonly type: GlobalSearchFindErrorType, message: string) {
super(message);

// Set the prototype explicitly, see:
// https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work
Object.setPrototypeOf(this, GlobalSearchFindError.prototype);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { map, takeUntil } from 'rxjs/operators';
import { duration } from 'moment';
import { HttpStart, ApplicationStart } from 'src/core/public';
import { GlobalSearchProviderResult } from '../../common/types';
import { GlobalSearchFindError } from '../../common/errors';
import { takeInArray } from '../../common/operators';
import { processProviderResult } from '../../common/process_result';
import { ILicenseChecker } from '../../common/license_checker';
Expand Down Expand Up @@ -82,7 +83,9 @@ export class SearchService {
const licenseState = this.licenseChecker!.getState();
if (!licenseState.valid) {
return throwError(
`GlobalSearch API is disabled because of invalid license state: ${licenseState.message}`
GlobalSearchFindError.invalidLicense(
`GlobalSearch API is disabled because of invalid license state: ${licenseState.message}`
)
);
}

Expand Down
6 changes: 5 additions & 1 deletion x-pack/plugins/global_search/server/routes/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { reduce, map } from 'rxjs/operators';
import { schema } from '@kbn/config-schema';
import { IRouter } from 'src/core/server';
import { GlobalSearchFindError } from '../../common/errors';

export const registerInternalFindRoute = (router: IRouter) => {
router.post(
Expand Down Expand Up @@ -39,7 +40,10 @@ export const registerInternalFindRoute = (router: IRouter) => {
},
});
} catch (e) {
return res.forbidden({ body: e });
if (e instanceof GlobalSearchFindError && e.type === 'invalid-license') {
return res.forbidden({ body: e.message });
}
throw e;
}
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Observable, timer, merge, throwError } from 'rxjs';
import { map, takeUntil } from 'rxjs/operators';
import { KibanaRequest, CoreStart, IBasePath } from 'src/core/server';
import { GlobalSearchProviderResult } from '../../common/types';
import { GlobalSearchFindError } from '../../common/errors';
import { takeInArray } from '../../common/operators';
import { ILicenseChecker } from '../../common/license_checker';

Expand Down Expand Up @@ -84,7 +85,9 @@ export class SearchService {
const licenseState = this.licenseChecker!.getState();
if (!licenseState.valid) {
return throwError(
`GlobalSearch API is disabled because of invalid license state: ${licenseState.message}`
GlobalSearchFindError.invalidLicense(
`GlobalSearch API is disabled because of invalid license state: ${licenseState.message}`
)
);
}

Expand Down

0 comments on commit fa3b8c0

Please sign in to comment.