-
Notifications
You must be signed in to change notification settings - Fork 8.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Ingest Manager] Error handling in server APIs #66688
Comments
Pinging @elastic/ingest-management (Team:Ingest Management) |
A couple things here:
Example CodeBusiness logic layer class MyService {
public async findPackage(packageName: string) {
try {
const package = await this.doApiCall(`/my-api`, { packageName });
if (!package) {
throw new PackageMissingError({ packageName });
}
return package;
} catch (e) {
// This is all made up for this example, but you get the idea
if (e.timedOut) {
throw new ElasticsearchUnavailableError(e);
} else if (e.unauthenticated)
throw new UnauthorizedError(e);
}
throw new UnknownError(e);
}
}
} HTTP handler router.get(
{ path: '/api/ingest/package', validation: { body: ... } },
async (context, req, res) => {
const { packageName } = req.body;
try {
const package = myService.findPackage(packageName);
return res.ok({ package });
} catch (e) {
// This could be extracted into a generic `translateIngestError` util
if (e instanceof PackageMissingError) {
return res.notFound(); // don't log 'expected' or unexceptional errors
} else if (e instanceof ElasticsearchUnavailableError) {
log.warning(e);
return res.unavailable();
} else if (e instanceof UnauthorizedError) {
return res.unauthorized();
} else {
log.error(e);
return res.internalError();
}
}
}
); |
@joshdover Thanks for your comments! I'll adapt the original proposal. What is the time frame for getting rid of |
No timeline right now, but we may consider removing Hapi altogether in the future to simplify our HTTP stack. Don't see this happening until some 8.x version. |
I've updated the description, and opened #67278 with a minimal implementation example following @joshdover 's suggestions. (Thanks!) I mostly agree. One notable exception is that I implemented the different error types as a typescript Also, if we want to add more logic to the handlers (e.g like supporting different log levels) this should be easy to add later. |
Currently, various parts of the APIs provided by Ingest Manager have implemented error handling and logging in different levels of completeness.
Overall, we should do the following when an error happens:
in the place where the error happens, use the Kibana Logger to log a descriptive error message. The logger is available since [Ingest] Use Kibana logger for proper server-side logging #66017in the place where the error happens, throw aBoom
error with a suitable status code that is not500
, and the same error message that was loggedIngestManagerError
with a suitableIngestManagerErrorType
and a helpful and descriptive error message.in the request handler, if the caught error is aBoom
error, use its status code inres.customError()
.instanceof IngestManagerError
, useres.customError()
to return the error to the caller. Pass the message, and get a suitable HTTP response code from thegetHTTPResponseCode()
helper.IngestManagerError
, use status code500
. In that case, log an error to the console with the full error message, and also log the stack trace of the error.For an example how this looks in implementation see #66541Implementation example: #67278
Reasoning
500
. This stack trace comes from within platform code and is not helpful in debugging the error. I find it also confusing, because it implies that the error hasn't been caught and handled correctly, which is not entirely true in our code. (There is Add error logs for HTTP 500 error details #65291 open for that.)400 bad request
instead.Tracking
This task is to go through each of these APIs and ensure it handles and reports errors properly:
setup
agent_config
enrollment_api_key
agent
epm
datasource
data_streams
install_script
output
settings
app
The text was updated successfully, but these errors were encountered: