Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Monitoring] Fixed server response errors #63181

Merged
merged 6 commits into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion x-pack/plugins/monitoring/server/lib/errors/known_errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
40 changes: 28 additions & 12 deletions x-pack/plugins/monitoring/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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';
Expand Down Expand Up @@ -92,6 +94,16 @@ interface IBulkUploader {
// This is used to test the version of kibana
const snapshotRegex = /-snapshot/i;

const wrapError = (error: any): CustomHttpResponseOptions<ResponseError> => {
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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
});
}