-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose elasticsearch error wrapper (#40242)
* expose elasticsearch error wrapper * generate docs * Update src/core/server/elasticsearch/errors.ts Co-Authored-By: Rudolf Meijering <skaapgif@gmail.com> * address Oleg comments
- Loading branch information
Showing
12 changed files
with
292 additions
and
9 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
docs/development/core/server/kibana-plugin-server.elasticsearcherror.[code].md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [ElasticsearchError](./kibana-plugin-server.elasticsearcherror.md) > [\[code\]](./kibana-plugin-server.elasticsearcherror.[code].md) | ||
|
||
## ElasticsearchError.\[code\] property | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
[code]?: string; | ||
``` |
18 changes: 18 additions & 0 deletions
18
docs/development/core/server/kibana-plugin-server.elasticsearcherror.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [ElasticsearchError](./kibana-plugin-server.elasticsearcherror.md) | ||
|
||
## ElasticsearchError interface | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
export interface ElasticsearchError extends Boom | ||
``` | ||
## Properties | ||
| Property | Type | Description | | ||
| --- | --- | --- | | ||
| [\[code\]](./kibana-plugin-server.elasticsearcherror.[code].md) | <code>string</code> | | | ||
23 changes: 23 additions & 0 deletions
23
...er/kibana-plugin-server.elasticsearcherrorhelpers.decoratenotauthorizederror.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [ElasticsearchErrorHelpers](./kibana-plugin-server.elasticsearcherrorhelpers.md) > [decorateNotAuthorizedError](./kibana-plugin-server.elasticsearcherrorhelpers.decoratenotauthorizederror.md) | ||
|
||
## ElasticsearchErrorHelpers.decorateNotAuthorizedError() method | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
static decorateNotAuthorizedError(error: Error, reason?: string): ElasticsearchError; | ||
``` | ||
|
||
## Parameters | ||
|
||
| Parameter | Type | Description | | ||
| --- | --- | --- | | ||
| error | <code>Error</code> | | | ||
| reason | <code>string</code> | | | ||
|
||
<b>Returns:</b> | ||
|
||
`ElasticsearchError` | ||
|
22 changes: 22 additions & 0 deletions
22
...e/server/kibana-plugin-server.elasticsearcherrorhelpers.isnotauthorizederror.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [ElasticsearchErrorHelpers](./kibana-plugin-server.elasticsearcherrorhelpers.md) > [isNotAuthorizedError](./kibana-plugin-server.elasticsearcherrorhelpers.isnotauthorizederror.md) | ||
|
||
## ElasticsearchErrorHelpers.isNotAuthorizedError() method | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
static isNotAuthorizedError(error: any): error is ElasticsearchError; | ||
``` | ||
|
||
## Parameters | ||
|
||
| Parameter | Type | Description | | ||
| --- | --- | --- | | ||
| error | <code>any</code> | | | ||
|
||
<b>Returns:</b> | ||
|
||
`error is ElasticsearchError` | ||
|
35 changes: 35 additions & 0 deletions
35
docs/development/core/server/kibana-plugin-server.elasticsearcherrorhelpers.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [ElasticsearchErrorHelpers](./kibana-plugin-server.elasticsearcherrorhelpers.md) | ||
|
||
## ElasticsearchErrorHelpers class | ||
|
||
Helpers provided to simplify future migration away from Boom as internal Error. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
export declare class ElasticsearchErrorHelpers | ||
``` | ||
|
||
## Methods | ||
|
||
| Method | Modifiers | Description | | ||
| --- | --- | --- | | ||
| [decorateNotAuthorizedError(error, reason)](./kibana-plugin-server.elasticsearcherrorhelpers.decoratenotauthorizederror.md) | <code>static</code> | | | ||
| [isNotAuthorizedError(error)](./kibana-plugin-server.elasticsearcherrorhelpers.isnotauthorizederror.md) | <code>static</code> | | | ||
|
||
## Example | ||
|
||
Handle errors | ||
|
||
```js | ||
try { | ||
await client.callWithRequest(request, '...'); | ||
} catch (err) { | ||
if (ElasticsearchErrorHelpers.isNotAuthorizedError(err)) { | ||
const authHeader = err.output.headers['WWW-Authenticate']; | ||
} | ||
|
||
``` | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import Boom from 'boom'; | ||
|
||
import { ElasticsearchErrorHelpers } from './errors'; | ||
|
||
describe('ElasticsearchErrorHelpers', () => { | ||
describe('NotAuthorized error', () => { | ||
describe('decorateNotAuthorizedError', () => { | ||
it('returns original object', () => { | ||
const error = new Error(); | ||
expect(ElasticsearchErrorHelpers.decorateNotAuthorizedError(error)).toBe(error); | ||
}); | ||
|
||
it('makes the error identifiable as a NotAuthorized error', () => { | ||
const error = new Error(); | ||
expect(ElasticsearchErrorHelpers.isNotAuthorizedError(error)).toBe(false); | ||
ElasticsearchErrorHelpers.decorateNotAuthorizedError(error); | ||
expect(ElasticsearchErrorHelpers.isNotAuthorizedError(error)).toBe(true); | ||
}); | ||
|
||
it('adds boom properties', () => { | ||
const error = ElasticsearchErrorHelpers.decorateNotAuthorizedError(new Error()); | ||
expect(typeof error.output).toBe('object'); | ||
expect(error.output.statusCode).toBe(401); | ||
}); | ||
|
||
it('preserves boom properties of input', () => { | ||
const error = Boom.notFound(); | ||
ElasticsearchErrorHelpers.decorateNotAuthorizedError(error); | ||
expect(error.output.statusCode).toBe(404); | ||
}); | ||
|
||
describe('error.output', () => { | ||
it('defaults to message of error', () => { | ||
const error = ElasticsearchErrorHelpers.decorateNotAuthorizedError(new Error('foobar')); | ||
expect(error.output.payload).toHaveProperty('message', 'foobar'); | ||
}); | ||
it('prefixes message with passed reason', () => { | ||
const error = ElasticsearchErrorHelpers.decorateNotAuthorizedError( | ||
new Error('foobar'), | ||
'biz' | ||
); | ||
expect(error.output.payload).toHaveProperty('message', 'biz: foobar'); | ||
}); | ||
it('sets statusCode to 401', () => { | ||
const error = ElasticsearchErrorHelpers.decorateNotAuthorizedError(new Error('foo')); | ||
expect(error.output).toHaveProperty('statusCode', 401); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import Boom from 'boom'; | ||
import { get } from 'lodash'; | ||
|
||
const code = Symbol('ElasticsearchError'); | ||
|
||
enum ErrorCode { | ||
NOT_AUTHORIZED = 'Elasticsearch/notAuthorized', | ||
} | ||
|
||
export interface ElasticsearchError extends Boom { | ||
[code]?: string; | ||
} | ||
|
||
function isElasticsearchError(error: any): error is ElasticsearchError { | ||
return Boolean(error && error[code]); | ||
} | ||
|
||
function decorate( | ||
error: Error, | ||
errorCode: ErrorCode, | ||
statusCode: number, | ||
message?: string | ||
): ElasticsearchError { | ||
if (isElasticsearchError(error)) { | ||
return error; | ||
} | ||
|
||
const boom = Boom.boomify(error, { | ||
statusCode, | ||
message, | ||
// keep status and messages if Boom error object already has them | ||
override: false, | ||
}) as ElasticsearchError; | ||
|
||
boom[code] = errorCode; | ||
|
||
return boom; | ||
} | ||
|
||
/** | ||
* Helpers for working with errors returned from the Elasticsearch service.Since the internal data of | ||
* errors are subject to change, consumers of the Elasticsearch service should always use these helpers | ||
* to classify errors instead of checking error internals such as `body.error.header[WWW-Authenticate]` | ||
* @public | ||
* | ||
* @example | ||
* Handle errors | ||
* ```js | ||
* try { | ||
* await client.asScoped(request).callAsCurrentUser(...); | ||
* } catch (err) { | ||
* if (ElasticsearchErrorHelpers.isNotAuthorizedError(err)) { | ||
* const authHeader = err.output.headers['WWW-Authenticate']; | ||
* } | ||
* ``` | ||
*/ | ||
export class ElasticsearchErrorHelpers { | ||
public static isNotAuthorizedError(error: any): error is ElasticsearchError { | ||
return isElasticsearchError(error) && error[code] === ErrorCode.NOT_AUTHORIZED; | ||
} | ||
|
||
public static decorateNotAuthorizedError(error: Error, reason?: string) { | ||
const decoratedError = decorate(error, ErrorCode.NOT_AUTHORIZED, 401, reason); | ||
const wwwAuthHeader = get<string>(error, 'body.error.header[WWW-Authenticate]'); | ||
|
||
decoratedError.output.headers['WWW-Authenticate'] = | ||
wwwAuthHeader || 'Basic realm="Authorization Required"'; | ||
|
||
return decoratedError; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters