Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1934 from jhpetersen/enhance_network_error_check
fix network error detection due to potential falsy instanceof ProgressEvent evaluation if ProgressEvent is monkey patched by another library
- Loading branch information
Showing
4 changed files
with
77 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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
57 changes: 57 additions & 0 deletions
57
projects/angular-auth-oidc-client/src/lib/flows/callback-handling/error-helper.spec.ts
This file contains 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,57 @@ | ||
import { HttpErrorResponse } from '@angular/common/http'; | ||
import { isNetworkError } from './error-helper'; | ||
|
||
describe('error helper', () => { | ||
describe('isNetworkError ', () => { | ||
const HTTP_ERROR = new HttpErrorResponse({}); | ||
|
||
const CONNECTION_ERROR = new HttpErrorResponse({ | ||
error: new ProgressEvent('error'), | ||
status: 0, | ||
statusText: 'Unknown Error', | ||
url: 'https://identity-server.test/openid-connect/token', | ||
}); | ||
|
||
const UNKNOWN_CONNECTION_ERROR = new HttpErrorResponse({ | ||
error: new Error('error'), | ||
status: 0, | ||
statusText: 'Unknown Error', | ||
url: 'https://identity-server.test/openid-connect/token', | ||
}); | ||
|
||
const PARTIAL_CONNECTION_ERROR = new HttpErrorResponse({ | ||
error: new ProgressEvent('error'), | ||
status: 418, // i am a teapot | ||
statusText: 'Unknown Error', | ||
url: 'https://identity-server.test/openid-connect/token', | ||
}); | ||
|
||
it('returns true on http error with status = 0', () => { | ||
expect(isNetworkError(CONNECTION_ERROR)).toBeTrue(); | ||
}); | ||
|
||
it('returns true on http error with status = 0 and unknown error', () => { | ||
expect(isNetworkError(UNKNOWN_CONNECTION_ERROR)).toBeTrue(); | ||
}); | ||
|
||
it('returns true on http error with status <> 0 and error ProgressEvent', () => { | ||
expect(isNetworkError(PARTIAL_CONNECTION_ERROR)).toBeTrue(); | ||
}); | ||
|
||
it('returns false on non http error', () => { | ||
expect(isNetworkError(new Error('not a HttpErrorResponse'))).toBeFalse(); | ||
}); | ||
|
||
it('returns false on string error', () => { | ||
expect(isNetworkError('not a HttpErrorResponse')).toBeFalse(); | ||
}); | ||
|
||
it('returns false on undefined', () => { | ||
expect(isNetworkError(undefined)).toBeFalse(); | ||
}); | ||
|
||
it('returns false on empty http error', () => { | ||
expect(isNetworkError(HTTP_ERROR)).toBeFalse(); | ||
}); | ||
}); | ||
}); |
14 changes: 14 additions & 0 deletions
14
projects/angular-auth-oidc-client/src/lib/flows/callback-handling/error-helper.ts
This file contains 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,14 @@ | ||
import { HttpErrorResponse } from '@angular/common/http'; | ||
|
||
/** | ||
* checks if the error is a network error | ||
* by checking if either internal error is a ProgressEvent with type error | ||
* or another error with status 0 | ||
* @param error | ||
* @returns true if the error is a network error | ||
*/ | ||
export const isNetworkError = (error: unknown): boolean => | ||
!!error && | ||
error instanceof HttpErrorResponse && | ||
((error.error instanceof ProgressEvent && error.error.type === 'error') || | ||
(error.status === 0 && !!error.error)); |
This file contains 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