-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Ensure all errors thrown by TdpClient are instances of Error class
#54877
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
10 commits
Select commit
Hold shift + click to select a range
4f3fb45
Add a utility function to convert any input to an Error instance
gzdunek 3872154
Add types for TdpClient events
gzdunek 230352d
Always throw Error from `adaptWebSocketToTdpTransport`
gzdunek 195c8e9
Improve getting the error message in `getErrMessage`
gzdunek ac71a06
Move `isAbortError` to error.ts
gzdunek fabcd1a
Remove unused `tshd/errors.ts`
gzdunek 3770a6b
Handle `JSON.stringify` error, add `cause` to thrown errors
gzdunek 6907d80
Remove unnecessary `async`
gzdunek 3eb12d8
Remove `error.toString` from the callsites
gzdunek 01b1818
Handle `err` being undefined
gzdunek 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
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 |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| /** | ||
| * Teleport | ||
| * Copyright (C) 2025 Gravitational, Inc. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| import { ensureError, isAbortError } from './error'; | ||
|
|
||
| class CustomErrorClass extends Error { | ||
| constructor(message: string) { | ||
| super(message); | ||
| this.name = 'CustomErrorClass'; | ||
| } | ||
| } | ||
|
|
||
| describe('ensureError', () => { | ||
| const cases = [ | ||
| { | ||
| input: new Error('already error'), | ||
| expectedMessage: 'already error', | ||
| expectedName: 'Error', | ||
| expectedInstance: Error, | ||
| }, | ||
| { | ||
| input: { message: 'custom message' }, | ||
| expectedMessage: 'custom message', | ||
| expectedName: 'Error', | ||
| expectedInstance: Error, | ||
| }, | ||
| { | ||
| input: { message: '', otherField: '123' }, | ||
| expectedMessage: '', | ||
| expectedName: 'Error', | ||
| expectedInstance: Error, | ||
| }, | ||
| { | ||
| input: { name: 'MyError', message: 'fail' }, | ||
| expectedMessage: 'fail', | ||
| expectedName: 'MyError', | ||
| expectedInstance: Error, | ||
| }, | ||
| { | ||
| input: new CustomErrorClass('fail'), | ||
| expectedMessage: 'fail', | ||
| expectedName: 'CustomErrorClass', | ||
| expectedInstance: CustomErrorClass, | ||
| }, | ||
| { | ||
| input: { foo: 'bar' }, | ||
| expectedMessage: '{"foo":"bar"}', | ||
| expectedName: 'Error', | ||
| expectedInstance: Error, | ||
| }, | ||
| { | ||
| input: 'just a string', | ||
| expectedMessage: 'just a string', | ||
| expectedName: 'Error', | ||
| expectedInstance: Error, | ||
| }, | ||
| { | ||
| input: 42, | ||
| expectedMessage: '42', | ||
| expectedName: 'Error', | ||
| expectedInstance: Error, | ||
| }, | ||
| { | ||
| input: null, | ||
| expectedMessage: '', | ||
| expectedName: 'Error', | ||
| expectedInstance: Error, | ||
| }, | ||
| { | ||
| input: undefined, | ||
| expectedMessage: '', | ||
| expectedName: 'Error', | ||
| expectedInstance: Error, | ||
| }, | ||
| ]; | ||
|
|
||
| test.each(cases)( | ||
| 'converts input "$input" to Error with message "$expectedMessage" and name "$expectedName"', | ||
| ({ input, expectedMessage, expectedName, expectedInstance }) => { | ||
| const error = ensureError(input); | ||
|
|
||
| expect(error).toBeInstanceOf(expectedInstance); | ||
| expect(error.message).toBe(expectedMessage); | ||
| expect(error.name).toBe(expectedName); | ||
| // Non-Error instances should have the original input attached as .cause. | ||
| expect(error.cause).toBe(input instanceof Error ? undefined : input); | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| describe('isAbortError', () => { | ||
| describe.each([ | ||
| ['DOMException', newDOMAbortError], | ||
| ['ApiError', newApiAbortError], | ||
| ['gRPC Error', newGrpcAbortError], | ||
| ])('for error type %s', (_, ErrorType) => { | ||
| it('is abort error', () => { | ||
| expect(isAbortError(ErrorType())).toBe(true); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| function newDOMAbortError() { | ||
| return new DOMException('Aborted', 'AbortError'); | ||
| } | ||
|
|
||
| // mimics ApiError | ||
| function newApiAbortError() { | ||
| return new Error('The user aborted a request', { | ||
| cause: newDOMAbortError(), | ||
| }); | ||
| } | ||
|
|
||
| // mimics TshdRpcError | ||
| function newGrpcAbortError() { | ||
| return { code: 'CANCELLED' }; | ||
| } |
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,74 @@ | ||
| /** | ||
| * Teleport | ||
| * Copyright (C) 2025 Gravitational, Inc. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| /** | ||
| * Converts any unknown input into an Error instance. | ||
| * Preserves message and name if available. | ||
| */ | ||
| export function ensureError(input: unknown): Error { | ||
| if (input instanceof Error) { | ||
| return input; | ||
| } | ||
|
|
||
| if (input === null || input === undefined) { | ||
| return new Error('', { cause: input }); | ||
| } | ||
|
|
||
| if (typeof input !== 'object') { | ||
| return new Error(String(input), { cause: input }); | ||
| } | ||
|
|
||
| let message = ''; | ||
| if ('message' in input) { | ||
| message = String(input.message); | ||
| } else { | ||
| try { | ||
| message = JSON.stringify(input); | ||
| } catch { | ||
| message = '[Unable to stringify the thrown value]'; | ||
| } | ||
| } | ||
|
|
||
| const error = new Error(message, { cause: input }); | ||
| if ('name' in input && typeof input.name === 'string') { | ||
| error.name = input.name; | ||
| } | ||
| return error; | ||
| } | ||
|
|
||
| /** Extracts an error message or returns a default one. */ | ||
| export function getErrorMessage(err: unknown): string { | ||
| const errorInstance = ensureError(err); | ||
| if (errorInstance.message === '') { | ||
| return 'something went wrong'; | ||
| } | ||
| return errorInstance.message; | ||
| } | ||
|
|
||
| export function isAbortError(err: any): boolean { | ||
| // handles Web UI abort error | ||
| if ( | ||
| (err instanceof DOMException && err.name === 'AbortError') || | ||
| (err?.cause && isAbortError(err.cause)) | ||
| ) { | ||
| return true; | ||
| } | ||
|
|
||
| // handles Connect abort error (specifically gRPC cancel error), see TshdRpcError | ||
| return err?.code === 'CANCELLED'; | ||
| } | ||
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.
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.
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.
just curious, i've been using
input == nullto test for both null and undefined, should i be doing it more explicitly like this?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 use the explicit version because I can never remember the coercion rules for
==😅It’s probably just personal preference, but I find
===a lot easier to reason about.