Skip to content

Commit

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

* res.customError -> defaultIngestErrorHandler

* Missed a variable rename in prior commit

* copying an invalid policy will 404; not 500

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
# 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
  • Loading branch information
John Schulz committed Sep 21, 2020
1 parent 26a71a4 commit 26f4901
Show file tree
Hide file tree
Showing 11 changed files with 632 additions and 152 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 });
}
};
Loading

0 comments on commit 26f4901

Please sign in to comment.