[9.0] [kbn/server-route-repository] Use appropriate log level based on HTTP status (#214185) (#214584)

# Backport

This will backport the following commits from `main` to `9.0`:
- [[kbn/server-route-repository] Use appropriate log level based on HTTP
status (#214185)](https://github.com/elastic/kibana/pull/214185)

<!--- Backport version: 9.6.6 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sorenlouv/backport)

<!--BACKPORT [{"author":{"name":"Milton
Hultgren","email":"milton.hultgren@elastic.co"},"sourceCommit":{"committedDate":"2025-03-14T13:54:08Z","message":"[kbn/server-route-repository]
Use appropriate log level based on HTTP status
(#214185)","sha":"f2af002a5e51594b0ae2e195bac7f5a7dfe9c383","branchLabelMapping":{"^v9.1.0$":"main","^v8.19.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","backport:all-open","v9.1.0"],"title":"[kbn/server-route-repository]
Use appropriate log level based on HTTP
status","number":214185,"url":"https://github.com/elastic/kibana/pull/214185","mergeCommit":{"message":"[kbn/server-route-repository]
Use appropriate log level based on HTTP status
(#214185)","sha":"f2af002a5e51594b0ae2e195bac7f5a7dfe9c383"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/214185","number":214185,"mergeCommit":{"message":"[kbn/server-route-repository]
Use appropriate log level based on HTTP status
(#214185)","sha":"f2af002a5e51594b0ae2e195bac7f5a7dfe9c383"}}]}]
BACKPORT-->

Co-authored-by: Milton Hultgren <milton.hultgren@elastic.co>
This commit is contained in:
Kibana Machine 2025-03-15 03:14:03 +11:00 committed by GitHub
parent e874ae42c4
commit 9a627af7b2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -118,6 +118,12 @@ export function registerRoutes<TDependencies extends Record<string, any>>({
}
if (isKibanaResponse(result)) {
if (result.status >= 500) {
logger.error(() => `HTTP ${result.status}: ${JSON.stringify(result.payload)}`);
} else if (result.status >= 400) {
logger.debug(() => `HTTP ${result.status}: ${JSON.stringify(result.payload)}`);
}
return result;
} else if (isObservable(result)) {
const controller = new AbortController();
@ -135,8 +141,6 @@ export function registerRoutes<TDependencies extends Record<string, any>>({
return response.ok({ body });
}
} catch (error) {
logger.error(error);
const opts = {
statusCode: 500,
body: {
@ -156,6 +160,12 @@ export function registerRoutes<TDependencies extends Record<string, any>>({
opts.body.attributes.data = error?.data;
}
if (opts.statusCode >= 500) {
logger.error(() => error);
} else {
logger.debug(() => error);
}
return response.custom(opts);
}
};