-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add compatibility wrapper for Boom errors thrown from route handler (#…
…51157) * add wrapErrors method to router * add RouteRegistrar type * update generated doc * add migration example * rename wrapErrors to handleLegacyErrors
- Loading branch information
1 parent
e753c35
commit 7fef618
Showing
18 changed files
with
279 additions
and
28 deletions.
There are no files selected for viewing
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
13 changes: 13 additions & 0 deletions
13
docs/development/core/server/kibana-plugin-server.irouter.handlelegacyerrors.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,13 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [IRouter](./kibana-plugin-server.irouter.md) > [handleLegacyErrors](./kibana-plugin-server.irouter.handlelegacyerrors.md) | ||
|
||
## IRouter.handleLegacyErrors property | ||
|
||
Wrap a router handler to catch and converts legacy boom errors to proper custom errors. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
handleLegacyErrors: <P extends ObjectType, Q extends ObjectType, B extends ObjectType>(handler: RequestHandler<P, Q, B>) => RequestHandler<P, Q, B>; | ||
``` |
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
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
13 changes: 13 additions & 0 deletions
13
docs/development/core/server/kibana-plugin-server.routeregistrar.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,13 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [RouteRegistrar](./kibana-plugin-server.routeregistrar.md) | ||
|
||
## RouteRegistrar type | ||
|
||
Handler to declare a route. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
export declare type RouteRegistrar = <P extends ObjectType, Q extends ObjectType, B extends ObjectType>(route: RouteConfig<P, Q, B>, handler: RequestHandler<P, Q, B>) => void; | ||
``` |
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
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,80 @@ | ||
/* | ||
* 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 { KibanaResponse, KibanaResponseFactory, kibanaResponseFactory } from './response'; | ||
import { wrapErrors } from './error_wrapper'; | ||
import { KibanaRequest, RequestHandler, RequestHandlerContext } from 'kibana/server'; | ||
|
||
const createHandler = (handler: () => any): RequestHandler<any, any, any> => () => { | ||
return handler(); | ||
}; | ||
|
||
describe('wrapErrors', () => { | ||
let context: RequestHandlerContext; | ||
let request: KibanaRequest<any, any, any>; | ||
let response: KibanaResponseFactory; | ||
|
||
beforeEach(() => { | ||
context = {} as any; | ||
request = {} as any; | ||
response = kibanaResponseFactory; | ||
}); | ||
|
||
it('should pass-though call parameters to the handler', async () => { | ||
const handler = jest.fn(); | ||
const wrapped = wrapErrors(handler); | ||
await wrapped(context, request, response); | ||
expect(handler).toHaveBeenCalledWith(context, request, response); | ||
}); | ||
|
||
it('should pass-though result from the handler', async () => { | ||
const handler = createHandler(() => { | ||
return 'handler-response'; | ||
}); | ||
const wrapped = wrapErrors(handler); | ||
const result = await wrapped(context, request, response); | ||
expect(result).toBe('handler-response'); | ||
}); | ||
|
||
it('should intercept and convert thrown Boom errors', async () => { | ||
const handler = createHandler(() => { | ||
throw Boom.notFound('not there'); | ||
}); | ||
const wrapped = wrapErrors(handler); | ||
const result = await wrapped(context, request, response); | ||
expect(result).toBeInstanceOf(KibanaResponse); | ||
expect(result.status).toBe(404); | ||
expect(result.payload).toEqual({ | ||
error: 'Not Found', | ||
message: 'not there', | ||
statusCode: 404, | ||
}); | ||
}); | ||
|
||
it('should re-throw non-Boom errors', async () => { | ||
const handler = createHandler(() => { | ||
throw new Error('something went bad'); | ||
}); | ||
const wrapped = wrapErrors(handler); | ||
await expect(wrapped(context, request, response)).rejects.toMatchInlineSnapshot( | ||
`[Error: something went bad]` | ||
); | ||
}); | ||
}); |
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,48 @@ | ||
/* | ||
* 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 { ObjectType, TypeOf } from '@kbn/config-schema'; | ||
import { KibanaRequest } from './request'; | ||
import { KibanaResponseFactory } from './response'; | ||
import { RequestHandler } from './router'; | ||
import { RequestHandlerContext } from '../../../server'; | ||
|
||
export const wrapErrors = <P extends ObjectType, Q extends ObjectType, B extends ObjectType>( | ||
handler: RequestHandler<P, Q, B> | ||
): RequestHandler<P, Q, B> => { | ||
return async ( | ||
context: RequestHandlerContext, | ||
request: KibanaRequest<TypeOf<P>, TypeOf<Q>, TypeOf<B>>, | ||
response: KibanaResponseFactory | ||
) => { | ||
try { | ||
return await handler(context, request, response); | ||
} catch (e) { | ||
if (Boom.isBoom(e)) { | ||
return response.customError({ | ||
body: e.output.payload, | ||
statusCode: e.output.statusCode, | ||
headers: e.output.headers, | ||
}); | ||
} | ||
throw e; | ||
} | ||
}; | ||
}; |
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
Oops, something went wrong.