File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -47,6 +47,30 @@ describe('getErrorMessage', () => {
4747 } ;
4848 expect ( getErrorMessage ( error ) ) . toBe ( 'Bad Request Message' ) ;
4949 } ) ;
50+
51+ it ( 'should remove a trailing period from URLs in error messages' , ( ) => {
52+ const error = new Error ( 'Migrate to https://antigravity.google.' ) ;
53+
54+ expect ( getErrorMessage ( error ) ) . toBe (
55+ 'Migrate to https://antigravity.google' ,
56+ ) ;
57+ } ) ;
58+
59+ it ( 'should preserve periods that do not trail a URL' , ( ) => {
60+ expect ( getErrorMessage ( new Error ( 'Authentication failed.' ) ) ) . toBe (
61+ 'Authentication failed.' ,
62+ ) ;
63+ } ) ;
64+
65+ it . each ( [ null , undefined ] ) (
66+ 'should handle an Error with a nullish message' ,
67+ ( message ) => {
68+ const error = new Error ( 'original' ) ;
69+ Object . defineProperty ( error , 'message' , { value : message } ) ;
70+
71+ expect ( getErrorMessage ( error ) ) . toBe ( '' ) ;
72+ } ,
73+ ) ;
5074} ) ;
5175
5276describe ( 'isAbortError' , ( ) => {
Original file line number Diff line number Diff line change @@ -33,10 +33,16 @@ export function isAbortError(error: unknown): boolean {
3333 return error instanceof Error && error . name === 'AbortError' ;
3434}
3535
36+ function stripTrailingPeriodsFromUrls (
37+ message : string | null | undefined ,
38+ ) : string {
39+ return message ?. replace ( / ( h t t p s ? : \/ \/ \S + ) \. (? = \s | $ ) / g, '$1' ) ?? '' ;
40+ }
41+
3642export function getErrorMessage ( error : unknown ) : string {
3743 const friendlyError = toFriendlyError ( error ) ;
3844 if ( friendlyError instanceof Error ) {
39- return friendlyError . message ;
45+ return stripTrailingPeriodsFromUrls ( friendlyError . message ) ;
4046 }
4147 if (
4248 typeof friendlyError === 'object' &&
@@ -45,10 +51,11 @@ export function getErrorMessage(error: unknown): string {
4551 typeof ( friendlyError as { message : unknown } ) . message === 'string'
4652 ) {
4753 // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
48- return ( friendlyError as { message : string } ) . message ;
54+ const message = ( friendlyError as { message : string } ) . message ;
55+ return stripTrailingPeriodsFromUrls ( message ) ;
4956 }
5057 try {
51- return String ( friendlyError ) ;
58+ return stripTrailingPeriodsFromUrls ( String ( friendlyError ) ) ;
5259 } catch {
5360 return 'Failed to get error details' ;
5461 }
You can’t perform that action at this time.
0 commit comments