When working with an HTTP service that returns an observable, I encountered an error during the subscribe process for a specific use case that I would like to address within the successful path.
My scenario looks like this:
In my service class:
class MyService {
getEntities(): Observable<any> {
return this.http.get('<the url'>)
.pipe(
catchError(err => {
// Handling errors and returning a corresponding message string for each type of error.
// For instance, in this example, I handle the status code 403
if (err.status === 403) {
return throwError('Custom error message for 403');
}
// Here lies the objective.
if (err.status === 409) {
// Find a way to trigger the goodResponse in the consumer class.
}
})
);
}
}
In my consumer class:
class MyComponent {
private myService: MyService;
constructor() {
this.myService = new MyService();
}
callTheAPI() {
this.myService.getEntities()
.subscribe(goodResponse => {
// Handle successful response
}, error => {
// Handle error
});
}
}
In the current context, the aim is to ensure a successful subscription when the status code is 409.