Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

309
Views
Property 'error' does not exist on type '"" | Promise<any>'

I'm trying to add some error handling to a service, following the Angular guide.

Relevant snippet:

private handleError (error: Response | any) {
  // In a real world app, you might use a remote logging infrastructure
  let errMsg: string;
  if (error instanceof Response) {
    const body = error.json() || '';
    const err = body.error || JSON.stringify(body);
    errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
  } else {
    errMsg = error.message ? error.message : error.toString();
  }
  console.error(errMsg);
  return Observable.throw(errMsg);
}

However, I'm getting a TypeScript error:

error TS2339: Property 'error' does not exist on type '"" | Promise<any>'. Property 'error' does not exist on type '""'.

I can understand why it's happening-- error.json() returns a Promise<any> and then the subsequent line with body.error wouldn't work because there is no error property. However, it seems like it should expect a JSON object to be returned from .json(). Why is that, and what am I missing that the Angular guide isn't?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Same thing just happened to me. This happens when you fail to import the Response object.

import { Response } from '@angular/http';
about 4 years ago · Santiago Trujillo Report

0

No, it won't work because you wrote:

const body = error.json() || '';

Which means that body can be an empty string, and a string doesn't have the error property.

This should be better:

const body = error.json() || { error: null };

Edit

Oh, error.json() returns a Promise, which means that you won't be able to use this synchronous block, instead you'll need to:

error.json().then(body => {
    if (!body) {
        body = "";
    }

    const err = body.error || JSON.stringify(body);
    const errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
});
about 4 years ago · Santiago Trujillo Report

0

I had the same issue, I finally found this solution.

.catch(error => {
  let errMsg: string;
  const body = JSON.parse(error._body);
  if (body) {
    errMsg = body.error
  } else {
    errMsg = error.message ? error.message : error.toString();
  }
  console.error(errMsg);
  return Promise.reject(errMsg);
});

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!