-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: show error notification when HTTP error response received
- Loading branch information
Showing
6 changed files
with
115 additions
and
2 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
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,19 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { ErrorPrintInterceptor } from './error-print.interceptor'; | ||
|
||
describe('ErrorPrintInterceptor', () => { | ||
beforeEach(() => | ||
TestBed.configureTestingModule({ | ||
providers: [ErrorPrintInterceptor], | ||
}) | ||
); | ||
|
||
it('should be created', () => { | ||
const interceptor: ErrorPrintInterceptor = TestBed.inject( | ||
ErrorPrintInterceptor | ||
); | ||
|
||
expect(interceptor).toBeTruthy(); | ||
}); | ||
}); |
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,33 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { | ||
HttpEvent, | ||
HttpHandler, | ||
HttpInterceptor, | ||
HttpRequest, | ||
} from '@angular/common/http'; | ||
import { Observable } from 'rxjs'; | ||
import { NotificationService } from '../notification.service'; | ||
import { tap } from 'rxjs/operators'; | ||
|
||
@Injectable() | ||
export class ErrorPrintInterceptor implements HttpInterceptor { | ||
constructor(private readonly notificationService: NotificationService) {} | ||
|
||
intercept( | ||
request: HttpRequest<unknown>, | ||
next: HttpHandler | ||
): Observable<HttpEvent<unknown>> { | ||
return next.handle(request).pipe( | ||
tap({ | ||
error: () => { | ||
const url = new URL(request.url); | ||
|
||
this.notificationService.showError( | ||
`Request to "${url.pathname}" failed. Check the console for the details`, | ||
0 | ||
); | ||
}, | ||
}) | ||
); | ||
} | ||
} |
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,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { NotificationService } from './notification.service'; | ||
|
||
describe('NotificationService', () => { | ||
let service: NotificationService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(NotificationService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
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,22 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { MatSnackBar } from '@angular/material/snack-bar'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class NotificationService { | ||
constructor(private readonly snackBar: MatSnackBar) {} | ||
|
||
/** | ||
* Show message with an error | ||
* | ||
* @param text Error text message | ||
* @param duration Duration to close after. 0 to close manually only | ||
*/ | ||
showError(text: string, duration = 3000) { | ||
this.snackBar.open(text, 'Dismiss', { | ||
duration, | ||
panelClass: 'shop-snackbar-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