From dacc95fa85788bf0a3cd77074159b7512b6f61fb Mon Sep 17 00:00:00 2001 From: igoristic Date: Thu, 16 Apr 2020 20:07:24 -0400 Subject: [PATCH] [Monitoring] Fixed server response errors (#63181) * Fixed server response errors * Fixed async error Co-authored-by: Elastic Machine --- .../server/lib/errors/known_errors.js | 4 +- x-pack/plugins/monitoring/server/plugin.ts | 40 +++++++++++++------ .../server/routes/api/v1/cluster/cluster.js | 10 +++-- 3 files changed, 38 insertions(+), 16 deletions(-) diff --git a/x-pack/plugins/monitoring/server/lib/errors/known_errors.js b/x-pack/plugins/monitoring/server/lib/errors/known_errors.js index 6f02d0b0b26c0..17bcdd0414adf 100644 --- a/x-pack/plugins/monitoring/server/lib/errors/known_errors.js +++ b/x-pack/plugins/monitoring/server/lib/errors/known_errors.js @@ -47,5 +47,7 @@ export function isKnownError(err) { export function handleKnownError(err) { err.message = err.message + ': ' + (err.description || mapTypeMessage[err.constructor.name]); - return boomify(err, { statusCode: KNOWN_ERROR_STATUS_CODE }); + let statusCode = err.statusCode || err.status; + statusCode = statusCode !== 500 ? statusCode : KNOWN_ERROR_STATUS_CODE; + return boomify(err, { statusCode }); } diff --git a/x-pack/plugins/monitoring/server/plugin.ts b/x-pack/plugins/monitoring/server/plugin.ts index 0fa1a5bf144ac..a45e80ac71d65 100644 --- a/x-pack/plugins/monitoring/server/plugin.ts +++ b/x-pack/plugins/monitoring/server/plugin.ts @@ -10,12 +10,6 @@ import { i18n } from '@kbn/i18n'; import { has, get } from 'lodash'; import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; import { TelemetryCollectionManagerPluginSetup } from 'src/plugins/telemetry_collection_manager/server'; -import { - LOGGING_TAG, - KIBANA_MONITORING_LOGGING_TAG, - KIBANA_ALERTING_ENABLED, - KIBANA_STATS_TYPE_MONITORING, -} from '../common/constants'; import { Logger, PluginInitializerContext, @@ -27,7 +21,15 @@ import { CoreStart, IRouter, IClusterClient, -} from '../../../../src/core/server'; + CustomHttpResponseOptions, + ResponseError, +} from 'kibana/server'; +import { + LOGGING_TAG, + KIBANA_MONITORING_LOGGING_TAG, + KIBANA_ALERTING_ENABLED, + KIBANA_STATS_TYPE_MONITORING, +} from '../common/constants'; import { MonitoringConfig } from './config'; // @ts-ignore import { requireUIRoutes } from './routes'; @@ -92,6 +94,16 @@ interface IBulkUploader { // This is used to test the version of kibana const snapshotRegex = /-snapshot/i; +const wrapError = (error: any): CustomHttpResponseOptions => { + const options = { statusCode: error.statusCode ?? 500 }; + const boom = Boom.isBoom(error) ? error : Boom.boomify(error, options); + return { + body: boom, + headers: boom.output.headers, + statusCode: boom.output.statusCode, + }; +}; + export class Plugin { private readonly initializerContext: PluginInitializerContext; private readonly log: Logger; @@ -369,12 +381,16 @@ export class Plugin { }, }, }; - - const result = await options.handler(legacyRequest); - if (Boom.isBoom(result)) { - return res.customError({ statusCode: result.output.statusCode, body: result }); + try { + const result = await options.handler(legacyRequest); + return res.ok({ body: result }); + } catch (err) { + const statusCode: number = err.output?.statusCode || err.statusCode || err.status; + if (Boom.isBoom(err) || statusCode !== 500) { + return res.customError({ statusCode, body: err }); + } + return res.internalError(wrapError(err)); } - return res.ok({ body: result }); }; const validate: any = get(options, 'config.validate', false); diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/cluster/cluster.js b/x-pack/plugins/monitoring/server/routes/api/v1/cluster/cluster.js index 8d6fe04cdb7bd..240cb84539dbf 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/cluster/cluster.js +++ b/x-pack/plugins/monitoring/server/routes/api/v1/cluster/cluster.js @@ -46,9 +46,13 @@ export function clusterRoute(server) { codePaths: req.payload.codePaths, }; - return getClustersFromRequest(req, indexPatterns, options).catch(err => - handleError(err, req) - ); + let clusters = []; + try { + clusters = await getClustersFromRequest(req, indexPatterns, options); + } catch (err) { + throw handleError(err, req); + } + return clusters; }, }); }