Skip to content

Commit

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

* [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]>
# Conflicts:
#	x-pack/plugins/ingest_manager/server/routes/agent/acks_handlers.ts
#	x-pack/plugins/ingest_manager/server/routes/agent/handlers.ts
#	x-pack/plugins/ingest_manager/server/routes/agent_policy/handlers.ts
#	x-pack/plugins/ingest_manager/server/routes/package_policy/handlers.ts
#	x-pack/plugins/ingest_manager/server/routes/settings/index.ts
#	x-pack/test/ingest_manager_api_integration/apis/agent_policy/agent_policy.ts

* Remove any 7.x references to *_policy vs *_config

* 404, not 500, on invalid id
  • Loading branch information
John Schulz authored Sep 22, 2020
1 parent d5e331b commit 0455c2d
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 209 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 @@ -12,6 +12,7 @@ import { PostAgentAcksRequestSchema } from '../../types/rest_spec';
import { AcksService } from '../../services/agents';
import { AgentEvent } from '../../../common/types/models';
import { PostAgentAcksResponse } from '../../../common/types/rest_spec';
import { defaultIngestErrorHandler } from '../../errors';

export const postAgentAcksHandlerBuilder = function (
ackService: AcksService
Expand Down Expand Up @@ -50,18 +51,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 @@ -12,6 +12,7 @@ import { PostNewAgentActionRequestSchema } from '../../types/rest_spec';
import { ActionsService } from '../../services/agents';
import { NewAgentAction } from '../../../common/types/models';
import { PostNewAgentActionResponse } from '../../../common/types/rest_spec';
import { defaultIngestErrorHandler } from '../../errors';

export const postNewAgentActionHandlerBuilder = function (
actionsService: ActionsService
Expand Down Expand Up @@ -40,18 +41,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 @@ -47,17 +48,14 @@ export const getAgentHandler: RequestHandler<TypeOf<
};

return response.ok({ body });
} catch (e) {
if (e.isBoom && e.output.statusCode === 404) {
} 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 @@ -112,18 +110,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 @@ -148,17 +143,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 @@ -198,25 +190,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 @@ -256,18 +231,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 @@ -316,11 +281,8 @@ export const putAgentsReassignHandler: RequestHandler<
success: true,
};
return response.ok({ body });
} catch (e) {
return response.customError({
statusCode: 500,
body: { message: e.message },
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};

Expand All @@ -336,10 +298,7 @@ export const getAgentStatusForConfigHandler: RequestHandler<
const body: GetAgentStatusResponse = { results, success: true };

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 @@ -27,10 +28,7 @@ export const postAgentsUnenrollHandler: RequestHandler<
success: true,
};
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 {
DeleteAgentConfigResponse,
GetFullAgentConfigResponse,
} from '../../../common';
import { defaultIngestErrorHandler } from '../../errors';

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

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

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

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

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

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

Expand All @@ -225,11 +211,8 @@ export const deleteAgentConfigsHandler: 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 @@ -259,11 +242,8 @@ export const getFullAgentConfig: RequestHandler<
body: { message: 'Agent config not found' },
});
}
} catch (e) {
return response.customError({
statusCode: 500,
body: { message: e.message },
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};

Expand Down Expand Up @@ -296,10 +276,7 @@ export const downloadFullAgentConfig: RequestHandler<
body: { message: 'Agent config 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 0455c2d

Please sign in to comment.