From 8030dc7dd5bc8fc55e87ed62083d3fce7da6b555 Mon Sep 17 00:00:00 2001 From: Brian Fletcher Date: Fri, 28 Jan 2022 11:28:14 +0000 Subject: [PATCH] only skip proxies if configured to do so Signed-off-by: Brian Fletcher --- .../proxy-backend/src/service/router.test.ts | 32 ++++++++++++++++++- plugins/proxy-backend/src/service/router.ts | 7 +++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/plugins/proxy-backend/src/service/router.test.ts b/plugins/proxy-backend/src/service/router.test.ts index f27a8cf3ae..6b4d6b21a7 100644 --- a/plugins/proxy-backend/src/service/router.test.ts +++ b/plugins/proxy-backend/src/service/router.test.ts @@ -60,7 +60,36 @@ describe('createRouter', () => { }); describe('where buildMiddleware would fail', () => { - it('works', async () => { + it('throws an error if skip failures is not set', async () => { + const logger = getVoidLogger(); + logger.warn = jest.fn(); + const config = new ConfigReader({ + backend: { + baseUrl: 'https://example.com:7007', + listen: { + port: 7007, + }, + }, + // no target would cause the buildMiddleware to fail + proxy: { + '/test': { + headers: { + Authorization: 'Bearer supersecret', + }, + }, + }, + }); + const discovery = SingleHostDiscovery.fromConfig(config); + await expect( + createRouter({ + config, + logger, + discovery, + }), + ).rejects.toThrow(new Error('Proxy target must be a string')); + }); + + it('works if skip failures is set', async () => { const logger = getVoidLogger(); logger.warn = jest.fn(); const config = new ConfigReader({ @@ -84,6 +113,7 @@ describe('createRouter', () => { config, logger, discovery, + skipBrokenProxies: true, }); expect((logger.warn as jest.Mock).mock.calls[0][0]).toEqual( 'skipped configuring /test due to Proxy target must be a string', diff --git a/plugins/proxy-backend/src/service/router.ts b/plugins/proxy-backend/src/service/router.ts index f9ce9943f5..7ea59d74eb 100644 --- a/plugins/proxy-backend/src/service/router.ts +++ b/plugins/proxy-backend/src/service/router.ts @@ -51,6 +51,7 @@ export interface RouterOptions { logger: Logger; config: Config; discovery: PluginEndpointDiscovery; + skipBrokenProxies?: boolean; } export interface ProxyConfig extends Options { @@ -193,7 +194,11 @@ export async function createRouter( buildMiddleware(pathPrefix, options.logger, route, proxyRouteConfig), ); } catch (e) { - options.logger.warn(`skipped configuring ${route} due to ${e.message}`); + if (options.skipBrokenProxies) { + options.logger.warn(`skipped configuring ${route} due to ${e.message}`); + } else { + throw e; + } } });