@@ -10,12 +10,6 @@ import { i18n } from '@kbn/i18n';
1010import { has , get } from 'lodash' ;
1111import { UsageCollectionSetup } from 'src/plugins/usage_collection/server' ;
1212import { 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' ;
1913import {
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' ;
3133import { MonitoringConfig } from './config' ;
3234// @ts -ignore
3335import { requireUIRoutes } from './routes' ;
@@ -91,6 +93,16 @@ interface IBulkUploader {
9193// This is used to test the version of kibana
9294const snapshotRegex = / - s n a p s h o t / 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+
94106export 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 ) ;
0 commit comments