Skip to content

Commit

Permalink
[7.x] [Ingest Manager] Ensure at least default Error handling on all …
Browse files Browse the repository at this point in the history
…routes (#77975) (#78086)

* [Ingest Manager] Ensure at least default Error handling on all routes (#77975)

* res.customError -> defaultIngestErrorHandler

* Missed a variable rename in prior commit

* copying an invalid policy will 404; not 500

Co-authored-by: Elastic Machine <[email protected]>

* Add import missed by backport script

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
John Schulz and elasticmachine authored Sep 22, 2020
1 parent b03c8bc commit e679b85
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 221 deletions.
15 changes: 3 additions & 12 deletions x-pack/plugins/ingest_manager/server/routes/agent/acks_handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { RequestHandler } from 'kibana/server';
import { AcksService } from '../../services/agents';
import { AgentEvent } from '../../../common/types/models';
import { PostAgentAcksRequest, PostAgentAcksResponse } from '../../../common/types/rest_spec';
import { defaultIngestErrorHandler } from '../../errors';

export const postAgentAcksHandlerBuilder = function (
ackService: AcksService
Expand Down Expand Up @@ -43,18 +44,8 @@ export const postAgentAcksHandlerBuilder = function (
};

return response.ok({ body });
} catch (e) {
if (e.isBoom) {
return response.customError({
statusCode: e.output.statusCode,
body: { message: e.message },
});
}

return response.customError({
statusCode: 500,
body: { message: e.message },
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { TypeOf } from '@kbn/config-schema';
import { PostNewAgentActionRequestSchema } from '../../types/rest_spec';
import { ActionsService } from '../../services/agents';
import { PostNewAgentActionResponse } from '../../../common/types/rest_spec';
import { defaultIngestErrorHandler } from '../../errors';

export const postNewAgentActionHandlerBuilder = function (
actionsService: ActionsService
Expand Down Expand Up @@ -38,18 +39,8 @@ export const postNewAgentActionHandlerBuilder = function (
};

return response.ok({ body });
} catch (e) {
if (e.isBoom) {
return response.customError({
statusCode: e.output.statusCode,
body: { message: e.message },
});
}

return response.customError({
statusCode: 500,
body: { message: e.message },
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};
};
79 changes: 19 additions & 60 deletions x-pack/plugins/ingest_manager/server/routes/agent/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
import * as AgentService from '../../services/agents';
import * as APIKeyService from '../../services/api_keys';
import { appContextService } from '../../services/app_context';
import { defaultIngestErrorHandler } from '../../errors';

export const getAgentHandler: RequestHandler<TypeOf<
typeof GetOneAgentRequestSchema.params
Expand All @@ -46,17 +47,14 @@ export const getAgentHandler: RequestHandler<TypeOf<
};

return response.ok({ body });
} catch (e) {
if (soClient.errors.isNotFoundError(e)) {
} catch (error) {
if (soClient.errors.isNotFoundError(error)) {
return response.notFound({
body: { message: `Agent ${request.params.agentId} not found` },
});
}

return response.customError({
statusCode: 500,
body: { message: e.message },
});
return defaultIngestErrorHandler({ error, response });
}
};

Expand Down Expand Up @@ -109,18 +107,15 @@ export const deleteAgentHandler: RequestHandler<TypeOf<
};

return response.ok({ body });
} catch (e) {
if (e.isBoom) {
} catch (error) {
if (error.isBoom) {
return response.customError({
statusCode: e.output.statusCode,
statusCode: error.output.statusCode,
body: { message: `Agent ${request.params.agentId} not found` },
});
}

return response.customError({
statusCode: 500,
body: { message: e.message },
});
return defaultIngestErrorHandler({ error, response });
}
};

Expand All @@ -144,17 +139,14 @@ export const updateAgentHandler: RequestHandler<
};

return response.ok({ body });
} catch (e) {
if (e.isBoom && e.output.statusCode === 404) {
} catch (error) {
if (error.isBoom && error.output.statusCode === 404) {
return response.notFound({
body: { message: `Agent ${request.params.agentId} not found` },
});
}

return response.customError({
statusCode: 500,
body: { message: e.message },
});
return defaultIngestErrorHandler({ error, response });
}
};

Expand Down Expand Up @@ -193,25 +185,8 @@ export const postAgentCheckinHandler: RequestHandler<
};

return response.ok({ body });
} catch (err) {
const logger = appContextService.getLogger();
if (err.isBoom) {
if (err.output.statusCode >= 500) {
logger.error(err);
}

return response.customError({
statusCode: err.output.statusCode,
body: { message: err.output.payload.message },
});
}

logger.error(err);

return response.customError({
statusCode: 500,
body: { message: err.message },
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};

Expand Down Expand Up @@ -250,18 +225,8 @@ export const postAgentEnrollHandler: RequestHandler<
};

return response.ok({ body });
} catch (e) {
if (e.isBoom) {
return response.customError({
statusCode: e.output.statusCode,
body: { message: e.message },
});
}

return response.customError({
statusCode: 500,
body: { message: e.message },
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};

Expand Down Expand Up @@ -307,11 +272,8 @@ export const putAgentsReassignHandler: RequestHandler<

const body: PutAgentReassignResponse = {};
return response.ok({ body });
} catch (e) {
return response.customError({
statusCode: 500,
body: { message: e.message },
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};

Expand All @@ -330,10 +292,7 @@ export const getAgentStatusForAgentPolicyHandler: RequestHandler<
const body: GetAgentStatusResponse = { results };

return response.ok({ body });
} catch (e) {
return response.customError({
statusCode: 500,
body: { message: e.message },
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { TypeOf } from '@kbn/config-schema';
import { PostAgentUnenrollResponse } from '../../../common/types';
import { PostAgentUnenrollRequestSchema } from '../../types';
import * as AgentService from '../../services/agents';
import { defaultIngestErrorHandler } from '../../errors';

export const postAgentsUnenrollHandler: RequestHandler<
TypeOf<typeof PostAgentUnenrollRequestSchema.params>,
Expand All @@ -25,10 +26,7 @@ export const postAgentsUnenrollHandler: RequestHandler<

const body: PostAgentUnenrollResponse = {};
return response.ok({ body });
} catch (e) {
return response.customError({
statusCode: 500,
body: { message: e.message },
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
DeleteAgentPolicyResponse,
GetFullAgentPolicyResponse,
} from '../../../common';
import { defaultIngestErrorHandler } from '../../errors';

export const getAgentPoliciesHandler: RequestHandler<
undefined,
Expand Down Expand Up @@ -64,11 +65,8 @@ export const getAgentPoliciesHandler: RequestHandler<
);

return response.ok({ body });
} catch (e) {
return response.customError({
statusCode: 500,
body: { message: e.message },
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};

Expand All @@ -91,11 +89,8 @@ export const getOneAgentPolicyHandler: RequestHandler<TypeOf<
body: { message: 'Agent policy not found' },
});
}
} catch (e) {
return response.customError({
statusCode: 500,
body: { message: e.message },
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};

Expand Down Expand Up @@ -144,11 +139,8 @@ export const createAgentPolicyHandler: RequestHandler<
return response.ok({
body,
});
} catch (e) {
return response.customError({
statusCode: 500,
body: { message: e.message },
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};

Expand All @@ -172,11 +164,8 @@ export const updateAgentPolicyHandler: RequestHandler<
return response.ok({
body,
});
} catch (e) {
return response.customError({
statusCode: 500,
body: { message: e.message },
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};

Expand All @@ -200,11 +189,8 @@ export const copyAgentPolicyHandler: RequestHandler<
return response.ok({
body,
});
} catch (e) {
return response.customError({
statusCode: 500,
body: { message: e.message },
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};

Expand All @@ -222,11 +208,8 @@ export const deleteAgentPoliciesHandler: RequestHandler<
return response.ok({
body,
});
} catch (e) {
return response.customError({
statusCode: 500,
body: { message: e.message },
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};

Expand Down Expand Up @@ -255,11 +238,8 @@ export const getFullAgentPolicy: RequestHandler<
body: { message: 'Agent policy not found' },
});
}
} catch (e) {
return response.customError({
statusCode: 500,
body: { message: e.message },
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};

Expand Down Expand Up @@ -292,10 +272,7 @@ export const downloadFullAgentPolicy: RequestHandler<
body: { message: 'Agent policy not found' },
});
}
} catch (e) {
return response.customError({
statusCode: 500,
body: { message: e.message },
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { RequestHandler, SavedObjectsClientContract } from 'src/core/server';
import { DataStream } from '../../types';
import { GetDataStreamsResponse, KibanaAssetType } from '../../../common';
import { getPackageSavedObjects, getKibanaSavedObject } from '../../services/epm/packages/get';
import { defaultIngestErrorHandler } from '../../errors';

const DATA_STREAM_INDEX_PATTERN = 'logs-*-*,metrics-*-*';

Expand Down Expand Up @@ -157,11 +158,8 @@ export const getListHandler: RequestHandler = async (context, request, response)
return response.ok({
body,
});
} catch (e) {
return response.customError({
statusCode: 500,
body: { message: e.message },
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};

Expand Down
Loading

0 comments on commit e679b85

Please sign in to comment.