Skip to content

Commit 1d51019

Browse files
[Monitoring] Fixed server response errors (elastic#63181)
* Fixed server response errors * Fixed async error Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent b9822b1 commit 1d51019

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

x-pack/plugins/monitoring/server/lib/errors/known_errors.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,7 @@ export function isKnownError(err) {
4747

4848
export function handleKnownError(err) {
4949
err.message = err.message + ': ' + (err.description || mapTypeMessage[err.constructor.name]);
50-
return boomify(err, { statusCode: KNOWN_ERROR_STATUS_CODE });
50+
let statusCode = err.statusCode || err.status;
51+
statusCode = statusCode !== 500 ? statusCode : KNOWN_ERROR_STATUS_CODE;
52+
return boomify(err, { statusCode });
5153
}

x-pack/plugins/monitoring/server/plugin.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ import { i18n } from '@kbn/i18n';
1010
import { has, get } from 'lodash';
1111
import { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
1212
import { TelemetryCollectionManagerPluginSetup } from 'src/plugins/telemetry_collection_manager/server';
13-
import {
14-
LOGGING_TAG,
15-
KIBANA_MONITORING_LOGGING_TAG,
16-
KIBANA_ALERTING_ENABLED,
17-
KIBANA_STATS_TYPE_MONITORING,
18-
} from '../common/constants';
1913
import {
2014
Logger,
2115
PluginInitializerContext,
@@ -27,7 +21,15 @@ import {
2721
CoreStart,
2822
IRouter,
2923
IClusterClient,
30-
} from '../../../../src/core/server';
24+
CustomHttpResponseOptions,
25+
ResponseError,
26+
} from 'kibana/server';
27+
import {
28+
LOGGING_TAG,
29+
KIBANA_MONITORING_LOGGING_TAG,
30+
KIBANA_ALERTING_ENABLED,
31+
KIBANA_STATS_TYPE_MONITORING,
32+
} from '../common/constants';
3133
import { MonitoringConfig } from './config';
3234
// @ts-ignore
3335
import { requireUIRoutes } from './routes';
@@ -91,6 +93,16 @@ interface IBulkUploader {
9193
// This is used to test the version of kibana
9294
const snapshotRegex = /-snapshot/i;
9395

96+
const wrapError = (error: any): CustomHttpResponseOptions<ResponseError> => {
97+
const options = { statusCode: error.statusCode ?? 500 };
98+
const boom = Boom.isBoom(error) ? error : Boom.boomify(error, options);
99+
return {
100+
body: boom,
101+
headers: boom.output.headers,
102+
statusCode: boom.output.statusCode,
103+
};
104+
};
105+
94106
export class Plugin {
95107
private readonly initializerContext: PluginInitializerContext;
96108
private readonly log: Logger;
@@ -352,12 +364,16 @@ export class Plugin {
352364
},
353365
},
354366
};
355-
356-
const result = await options.handler(legacyRequest);
357-
if (Boom.isBoom(result)) {
358-
return res.customError({ statusCode: result.output.statusCode, body: result });
367+
try {
368+
const result = await options.handler(legacyRequest);
369+
return res.ok({ body: result });
370+
} catch (err) {
371+
const statusCode: number = err.output?.statusCode || err.statusCode || err.status;
372+
if (Boom.isBoom(err) || statusCode !== 500) {
373+
return res.customError({ statusCode, body: err });
374+
}
375+
return res.internalError(wrapError(err));
359376
}
360-
return res.ok({ body: result });
361377
};
362378

363379
const validate: any = get(options, 'config.validate', false);

x-pack/plugins/monitoring/server/routes/api/v1/cluster/cluster.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,13 @@ export function clusterRoute(server) {
4646
codePaths: req.payload.codePaths,
4747
};
4848

49-
return getClustersFromRequest(req, indexPatterns, options).catch(err =>
50-
handleError(err, req)
51-
);
49+
let clusters = [];
50+
try {
51+
clusters = await getClustersFromRequest(req, indexPatterns, options);
52+
} catch (err) {
53+
throw handleError(err, req);
54+
}
55+
return clusters;
5256
},
5357
});
5458
}

0 commit comments

Comments
 (0)