-
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
[Lens] Leverage original http request error #79831
Changes from 7 commits
926fbae
a0b1958
20710c8
9073897
b7d33a7
521e932
a8cf08f
0b2aaa4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-plugins-expressions-public](./kibana-plugin-plugins-expressions-public.md) > [ExpressionRenderError](./kibana-plugin-plugins-expressions-public.expressionrendererror.md) > [original](./kibana-plugin-plugins-expressions-public.expressionrendererror.original.md) | ||
|
||
## ExpressionRenderError.original property | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
original?: Error; | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { i18n } from '@kbn/i18n'; | ||
|
||
import { ExpressionRenderError } from 'src/plugins/expressions/public'; | ||
|
||
interface ElasticsearchErrorClause { | ||
type: string; | ||
reason: string; | ||
caused_by?: ElasticsearchErrorClause; | ||
} | ||
|
||
interface RequestError extends Error { | ||
body?: { attributes?: { error: ElasticsearchErrorClause } }; | ||
} | ||
|
||
const isRequestError = (e: Error | RequestError): e is RequestError => { | ||
if ('body' in e) { | ||
return e.body?.attributes?.error?.caused_by !== undefined; | ||
} | ||
return false; | ||
}; | ||
|
||
function getNestedErrorClause({ | ||
type, | ||
reason, | ||
caused_by: causedBy, | ||
}: ElasticsearchErrorClause): { type: string; reason: string } { | ||
if (causedBy) { | ||
return getNestedErrorClause(causedBy); | ||
} | ||
return { type, reason }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we be skipping all the intermediate types and reasons? This recursion makes sense, but are there cases where it's losing important information? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see the point - for the known cases the root exception is the relevant one and I made this experience very often in Java in general (lot's of re-thrown exceptions, but the actual cause is in the very last one). I guess when we solve this cleanly there should be an option to expand the error to show all the details. I will open an issue for this. |
||
} | ||
|
||
export function getOriginalRequestErrorMessage(error?: ExpressionRenderError | null) { | ||
if (error && 'original' in error && error.original && isRequestError(error.original)) { | ||
const rootError = getNestedErrorClause(error.original.body!.attributes!.error); | ||
if (rootError.reason && rootError.type) { | ||
return i18n.translate('xpack.lens.editorFrame.expressionFailureMessage', { | ||
defaultMessage: 'Request error: {type}, {reason}', | ||
values: { | ||
reason: rootError.reason, | ||
type: rootError.type, | ||
}, | ||
}); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you could leverage some knowledge from TS here and just do
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is necessary because
err
could be a string based on the type, so we have to do some generic checks first, otherwise typescript will complain about accessingoriginal
on a union type that might not have the property