-
Notifications
You must be signed in to change notification settings - Fork 49
[1.11.2] Add enhanced stack traces and a few more try-catch blocks #427
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6005f27
Add enhanced stack traces and a few more try-catch blocks
slvrtrn 6ff686f
Reverse stack traces for convenience
slvrtrn 99f2960
An option to enable enhanced stack traces, 1.11.2, CHANGELOG
slvrtrn ad4896d
[skip ci] Update CHANGELOG.md
slvrtrn f7572ca
Add a few mildly useful unit tests
slvrtrn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| const errorRe = | ||
| /(Code|Error): (?<code>\d+).*Exception: (?<message>.+)\((?<type>(?=.+[A-Z]{3})[A-Z0-9_]+?)\)/s | ||
|
|
||
| interface ParsedClickHouseError { | ||
| message: string | ||
| code: string | ||
| type?: string | ||
| } | ||
|
|
||
| /** An error that is thrown by the ClickHouse server. */ | ||
| export class ClickHouseError extends Error { | ||
| readonly code: string | ||
| readonly type: string | undefined | ||
| constructor({ message, code, type }: ParsedClickHouseError) { | ||
| super(message) | ||
| this.code = code | ||
| this.type = type | ||
|
|
||
| // Set the prototype explicitly, see: | ||
| // https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work | ||
| Object.setPrototypeOf(this, ClickHouseError.prototype) | ||
| } | ||
| } | ||
|
|
||
| export function parseError(input: string | Error): ClickHouseError | Error { | ||
| const inputIsError = input instanceof Error | ||
| const message = inputIsError ? input.message : input | ||
| const match = message.match(errorRe) | ||
| const groups = match?.groups as ParsedClickHouseError | undefined | ||
| if (groups) { | ||
| return new ClickHouseError(groups) | ||
| } else { | ||
| return inputIsError ? input : new Error(input) | ||
| } | ||
| } | ||
|
|
||
| /** Captures the current stack trace from the sync context before going async. | ||
| * It is necessary since the majority of the stack trace is lost when an async callback is called. */ | ||
| export function getCurrentStackTrace(): string { | ||
| const stack = new Error().stack | ||
| if (!stack) return '' | ||
|
|
||
| // Skip the first three lines of the stack trace, containing useless information | ||
| // - Text `Error` | ||
| // - Info about this function call | ||
| // - Info about the originator of this function call, e.g., `request` | ||
| // Additionally, the original stack trace is, in fact, reversed. | ||
| return stack.split('\n').slice(3).reverse().join('\n') | ||
slvrtrn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** Having the stack trace produced by the {@link getCurrentStackTrace} function, | ||
| * add it to an arbitrary error stack trace. No-op if there is no additional stack trace to add. | ||
| * It could happen if this feature was disabled due to its performance overhead. */ | ||
| export function enhanceStackTrace<E extends Error>( | ||
| err: E, | ||
| stackTrace: string | undefined, | ||
| ): E { | ||
| if (err.stack && stackTrace) { | ||
| const firstNewlineIndex = err.stack.indexOf('\n') | ||
| const firstLine = err.stack.substring(0, firstNewlineIndex) | ||
| const errStack = err.stack.substring(firstNewlineIndex + 1) | ||
| err.stack = `${firstLine}\n${stackTrace}\n${errStack}` | ||
| } | ||
| return err | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| export * from './parse_error' | ||
| export * from './error' |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| export default '1.11.1' | ||
| export default '1.11.2' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.