Last active
April 28, 2020 11:11
-
-
Save jhades/d699243fc450a85c2990d54b088c365a to your computer and use it in GitHub Desktop.
RxJs Error Handling
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
@Component({ | |
selector: 'home', | |
templateUrl: './home.component.html' | |
}) | |
export class HomeComponent implements OnInit { | |
constructor(private http: HttpClient) {} | |
ngOnInit() { | |
const http$ = this.http.get<Course[]>('/api/courses'); | |
http$.subscribe( | |
res => console.log('HTTP response', res), | |
err => console.log('HTTP Error', err), | |
() => console.log('HTTP request completed.') | |
); | |
} | |
} | |
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
try { | |
// synchronous operation | |
const httpResponse = getHttpResponseSync('/api/courses'); | |
} | |
catch(error) { | |
// handle 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
const http$ = this.http.get<Course[]>('/api/courses'); | |
http$ | |
.pipe( | |
catchError(err => of([])) | |
) | |
.subscribe( | |
res => console.log('HTTP response', res), | |
err => console.log('HTTP Error', err), | |
() => console.log('HTTP request completed.') | |
); |
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
const http$ = this.http.get<Course[]>('/api/courses'); | |
http$ | |
.pipe( | |
catchError(err => { | |
console.log('Handling error locally and rethrowing it...', err); | |
return throwError(err); | |
}) | |
) | |
.subscribe( | |
res => console.log('HTTP response', res), | |
err => console.log('HTTP Error', err), | |
() => console.log('HTTP request completed.') | |
); | |
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
const http$ = this.http.get<Course[]>('/api/courses'); | |
http$ | |
.pipe( | |
map(res => res['payload']), | |
catchError(err => { | |
console.log('caught mapping error and rethrowing', err); | |
return throwError(err); | |
}), | |
catchError(err => { | |
console.log('caught rethrown error, providing fallback value'); | |
return of([]); | |
}) | |
) | |
.subscribe( | |
res => console.log('HTTP response', res), | |
err => console.log('HTTP Error', err), | |
() => console.log('HTTP request completed.') | |
); |
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
try { | |
// synchronous operation | |
const httpResponse = getHttpResponseSync('/api/courses'); | |
} | |
catch(error) { | |
// handle error, only executed in case of error | |
} | |
finally { | |
// this will always get executed | |
} |
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
const http$ = this.http.get<Course[]>('/api/courses'); | |
http$ | |
.pipe( | |
map(res => res['payload']), | |
catchError(err => { | |
console.log('caught mapping error and rethrowing', err); | |
return throwError(err); | |
}), | |
finalize(() => console.log("first finalize() block executed")), | |
catchError(err => { | |
console.log('caught rethrown error, providing fallback value'); | |
return of([]); | |
}), | |
finalize(() => console.log("second finalize() block executed")) | |
) | |
.subscribe( | |
res => console.log('HTTP response', res), | |
err => console.log('HTTP Error', err), | |
() => console.log('HTTP request completed.') | |
); |
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
const http$ = this.http.get<Course[]>('/api/courses'); | |
http$.pipe( | |
tap(() => console.log("HTTP request executed")), | |
map(res => Object.values(res["payload"]) ), | |
shareReplay(), | |
retryWhen(errors => { | |
return errors | |
.pipe( | |
tap(() => console.log('retrying...')) | |
); | |
} ) | |
) | |
.subscribe( | |
res => console.log('HTTP response', res), | |
err => console.log('HTTP Error', err), | |
() => console.log('HTTP request completed.') | |
); | |
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
const http$ = this.http.get<Course[]>('/api/courses'); | |
http$.pipe( | |
tap(() => console.log("HTTP request executed")), | |
map(res => Object.values(res["payload"]) ), | |
shareReplay(), | |
retryWhen(errors => { | |
return errors | |
.pipe( | |
delayWhen(() => timer(2000)), | |
tap(() => console.log('retrying...')) | |
); | |
} ) | |
) | |
.subscribe( | |
res => console.log('HTTP response', res), | |
err => console.log('HTTP Error', err), | |
() => console.log('HTTP request completed.') | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment