Is there a way for me to retrieve the catchError message from the service method within the component?

fetchClientFinanceDetails(id: string, updateType: OnvioClientUpdateType): Observable <ClientFinance> {
  return this.getClientContactDetailsById(id, updateType).pipe(mergeMap((data: any) => {
    const finance = this.mapDataToClientFinance(data);
    return of(finance);
  })).pipe(catchError(ExceptionHandler.handleError));
}

I am invoking the above function from a component and it is defined in my service. However, I am facing an issue where the error returned by catchError function gets overridden by the Observable ClientFinance. How can I handle this situation?

Answer №1

If you encounter an error, you can use the throwError method to handle it.

Here's an example implementation:

import { Observable, of, throwError } from "rxjs";
import { catchError, map } from "rxjs/operators";

getFinanceDetails(clientId: string, type: OnvioClientUpdateType): Observable <ClientFinance> {
  return this.getClientContactDetailsById(clientId, type)
    .pipe(
       mergeMap((data: any) => {
          const clientFinanceDetails = this.clientContactDetailsByJsonData(data);
          return of(clientFinanceDetails);
       }),
       catchError(error => throwError("An error occurred: ", error))
    );
}

Answer №2

To address the issue using catchError, you can follow this method:

catchError(error => {
    console.log('Handling error within local scope and re-throwing it:', error);
    ExceptionHandler.handleError;
    return throwError('An error has occurred.', error);    
})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When utilizing WPF WebBrowser with a local Angular 8 website, the window.external feature works flawlessly. However, complications arise when

Apologies for any formatting issues, this is my first post. I have been working on updating a legacy app with a new UI. This serves as a proof of concept to demonstrate our ability to create a thin client and host our project online. In my WPF window (.NE ...

Error encountered: Uncaught SyntaxError - An unexpected token '<' was found while matching all routes within the Next.js middleware

I am implementing a code in the middleware.ts file to redirect users to specific pages based on their role. Here is the code snippet: import { NextResponse } from 'next/server' import type { NextRequest } from 'next/server' import { get ...

What is the best way to incorporate Form Projection into Angular?

I have been attempting to incorporate form projection in Angular, inspired by Kara Erickson's presentation at Angular Connect in 2017, but I am encountering difficulties and errors along the way. view talk here The code provided in the slides is inco ...

What is the purpose of manually creating the vscode.d.ts file in the vscode source code?

Manually writing .d.ts files is typically only necessary when working with existing .js files. If you're developing a TypeScript project, it's recommended not to write .d.ts files by hand, as the compiler with the --declaration option can auto-ge ...

Dealing with delays in rendering certain values with Angular interpolation & ways to eliminate an element from an array of TypeScript objects

I am currently developing a web application using ASP.NET Core - Angular. The app allows users to select a customer as the starting point and then calculates the distance & duration to other customers using Google Maps Distance Matrix Service. Although I a ...

What is the reason behind ReplaySubject displaying all values?

My service includes: private publishSubscribeSubject_: ReplaySubject<any> = new ReplaySubject(); private emiter_: Observable<any>; public publish(channel: EventsChannels, event: any) { this.publishSubscribeSubject_.next({ channel: channel, ...

Exploring the World of Angular6 with the Incredible Power of tinyMCE Editor

Hello, I'm looking to incorporate the tinyMCE editor into my Angular 6 application. I followed a thread that explains how to integrate it, but I ran into an issue where it says the domain is not registered. I would prefer to do this without requiring ...

Issue with the <mat-chip> component in Angular Material Design

One issue I am facing is with a mat-chip list. When I close the first item it works fine, but when I close the last item, all chips are closed. https://i.sstatic.net/opj7f.png Here is the HTML code snippet: <mat-form-field> <mat-chip-list #ch ...

Ways to avoid redundancy in Gitlab CI/CD when utilizing multiple stages

Currently, my workflow involves utilizing gitlab CI/CD for deploying my angular app on firebase. This process consists of 2 stages: build and deploy. image: node:11.2.0 stages: - build - deploy cache: paths: - node_modules/ build: stage: bu ...

Jest is having difficulty locating a module while working with Next.js, resulting in

I am encountering some difficulties trying to make jest work in my nextjs application. Whenever I use the script "jest", the execution fails and I get the following result: FAIL __tests__/index.test.tsx ● Test suite failed to run ...

What techniques does Angular2 use to handle dependency injection?

When working with Angular2 components, I know that to inject a dependency, you simply annotate an argument in the constructor, like how ThingService is injected here. However, what I am wondering is how Angular actually knows what to inject at runtime. A ...

Is there a way to retrieve the initial item of a JSON array from an HTML document using Angular 2?

Within the src/assets/ directory, I have a json file called product.json with the following structure: [ { "images": "http://openclipart.org/image/300px/svg_to_png/26215/Anonymous_Leaf_Rake.png", "textBox": "empty", "comments": "empty" }, { "i ...

Type inference in Typescript is especially powerful when used in conjunction with decorators

I am curious as to why the compiler in Typescript cannot infer the new type of a class when decorators or annotations are used. Interestingly, if I use the traditional ES5 method (manually calling the decorator), it works without any issues. Here is an ex ...

I am eager to learn how to integrate the "fs" module from Node.js into an Electron project powered by Angular

As I venture into writing my first desktop app using Electron and Angular5, I have encountered a roadblock while working with the fs module. Despite importing fs correctly (without errors in Visual Studio Code and with code completion), I faced an issue wh ...

Unable to set a value to the immutable 'constructor' property of the object '[object Object]' in g2.js

I am currently working with Angular 11 and using the latest versions of @antv/g2 and @antv/data-set packages within Angular 11. I'm facing an error that I don't quite understand. Can someone please assist me in resolving this error? Thank you in ...

Encountering issues with NPM installation and a failed build process while working with

As someone new to working with Angular, I apologize if my question is basic. I've been given a new repository to work on, but I'm having trouble building it through the command line. When attempting to run npm install and npm start on my work p ...

The process of inserting data into MongoDB using Mongoose with TypeScript

Recently, I encountered an issue while trying to insert data into a MongoDB database using a TypeScript code for a CRUD API. The problem arises when using the mongoose package specifically designed for MongoDB integration. import Transaction from 'mon ...

Trouble with two dropdown selections in Angular and preset values

I am encountering an issue with a select input in angular 7. The scenario involves having 2 selects where selecting an option in the second select populates the corresponding values in the first select. The first select should display a default placeholder ...

The DefaultTheme in MaterialUI no longer recognizes the 'palette' property after transitioning from v4 to v5, causing it to stop functioning correctly

Currently in the process of transitioning my app from Material UI v4 to v5 and encountering a few challenges. One issue I'm facing is that the 'palette' property is not recognized by DefaultTheme from Material UI when used in makeStyles. Thi ...

Avoiding caching of GET requests in Angular 2 for Internet Explorer 11

My rest endpoint successfully returns a list when calling GET, and I can also use POST to add new items or DELETE to remove them. This functionality is working perfectly in Firefox and Chrome, with the additional note that POST and DELETE also work in IE ...