2

I have a method for handling errors:

private handleError<T>(operation = 'operation', result?: T) {
    return (error: any): Observable<T> => {
      console.error(error);
      this.log(`${operation} failed: ${error.message}`);

      return of(result as T);
    };
  }

  private log(message: string) {
    console.log(message);
  }

and I want to use it inside my method:

getMyStructure(): Observable<HttpResponse<StructureResponse>> {
    return this.http.get< StructureResponse >(
        localUrl).pipe(retry(3), catchError(this.handleError<StructureResponse>('getMyStructure', {})));
}

but the last {} throws me an error. The StructureResponse is an Interface with some fields. In my understanding I should put there some (empty?) object of StructureResponse but I'm no longer sure. Can you help me with that?

this is the StructureResponse:

interface SomeStructure {
  content: MyStructure[];
  page: number;
  size: number;
  totalElements: number;
  totalPages: number;
  last: boolean
}

2 Answers 2

3

Add ? to each property.

interface SomeStructure {
  content?: MyStructure[];
  page?: number;
  size?: number;
  totalElements?: number;
  totalPages?: number;
  last?: boolean
}

Use Observable<StructureResponse> instead Observable<HttpResponse<StructureResponse>> as below.

getMyStructure(): Observable<StructureResponse> {
    return this.http.get< StructureResponse >(
        localUrl).pipe(retry(3), catchError(this.handleError<StructureResponse>('getMyStructure', {})));
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you pass an empty object, the only way that it would assign to a interface, would be that the attributes from the interface are all optional (with ?), otherwise you need to at least show your attributes in your object with null or another value.

2 Comments

can you please check my interface? I added it to the question - knowing that, could you please help me with resolving my problem?
I added the ? but I'm getting the following error now during compilation: TS2322: Type 'Observable< SomeStructure >' is not assignable to type 'Observable<HttpResponse< SomeStructure >>'.   Type 'SomeStructure' is missing the following properties from type 'HttpResponse< SomeStructure >': body, type, clone, headers, and 4 more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.