Skip to content

Commit

Permalink
[Monitoring] Fixed server response errors (#63181)
Browse files Browse the repository at this point in the history
* Fixed server response errors

* Fixed async error

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
igoristic and elasticmachine committed Apr 17, 2020
1 parent 4e8ff57 commit dacc95f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
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;
},
});
}

0 comments on commit dacc95f

Please sign in to comment.