The error message encountered states that the property 'status' is not found within the object type 'Error'

While working with Angular 2, I encountered an issue when passing an Error object from an HTTP request into the logAndPassOn method. The error message "Property 'status' does not exist on type 'Error'" was displayed. Although the typeof Error is recognized as an object, it doesn't behave in a similar way to how typeof File would be handled.

private logAndPassOn (error: Error) {
    if (error.status == 403) {
      // Do things
    }
    return Observable.throw(error);
}

To work around this problem, I had to set the error's type as 'any'. However, I believe there should be a more optimal solution available. Is there a specific library that needs to be imported for handling the typeof Error? Are there alternative approaches to tackling this issue instead of creating a custom Error class and importing it?

Answer №1

When handling errors in the Http error callback, it's important to note that the object being passed is of type Response from the @angular/http module. This Response is the same one received from the initial Http call. In case of an error response, this Response is sent as the error to the subscriber.

import { Response } from '@angular/json';

http.get(..)
    .subscribe(
         (res: Response) => { doSomething(res.json()) }
         (res: Response) => { ... }); 

It's worth noting that using Error in this context is incorrect. Instead, utilizing any will work, since during runtime the actual Response object being passed does contain a status property.

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

After defining Partial<T>, encountering an error trying to access an undefined property is unexpected

In my function, I am attempting to standardize certain values by specifying the whole function type as Partial. However, despite declaring the interaction variable as Partial Type, I keep encountering the error message saying "Cannot read property endTime ...

Stop allowing the entry of zero after a minus sign

One of the features on our platform allows users to input a number that will be automatically converted to have a negative sign. However, we want to ensure that users are unable to manually add a negative sign themselves. We need to find a solution to pre ...

The error TS2769 occurs when using the spread operator to flatten an array

I've been working on flattening an array, but unfortunately I keep encountering an error - TS2769: No overload matches this call. Oddly enough, when I tested this in stackblitz, it worked perfectly. The issue seems to be related to the spread operat ...

Tips for designing a personalized payment page in PayPal for flexible one-time and subscription-based payments

How can I display a PayPal checkout page with custom fields such as tax and total amount when a user makes a payment for a custom amount? Can multiple fields like sales tax and total amount be added? In addition to that, our web application already has Pa ...

What is the reason for the inability to import a router for express in TypeScript?

I have been working on setting up a basic Hello World REST service using NodeJS and Typescript. I attempted to organize the routers into separate files, but encountered some issues along the way. Specifically, when making a GET call to http://localhost:30 ...

Step-by-step guide on integrating jQuery-ui with Asp Core+ Angular 4 template using Visual Studio 2017

Using the Asp Core+webpack+ Angular 4 template from VS 2017, I am trying to figure out how to load jQuery-ui. I attempted to put it in webpack.config.vendor.js: const treeShakableModules = [ '@angular/animations', '@angular/common&a ...

Displaying or concealing list elements in Angular 6

I am working on a simple Angular app that displays a list of items. I want the behavior to be such that when the first item in the list is clicked, its description (card) should be displayed. And, if the second item is clicked and its description is displa ...

Leveraging Apostrophe CMS for building a decoupled single page application

I am currently developing a single page application inspired by the design of My goal is to separate the content management system from the presentation layer (most likely using Vue or Angular) and fetch data from a JSON API. The client initially prefers ...

Transitioned to Angular 16: Issues with Compiling Due to Google Places Autocomplete Plugin (ngx-google-places-autocomplete)

I am currently seeking support for implementing Google Places Autocomplete on mobile devices in portrait mode for iOS version 16.x (as well as Android). After upgrading our Angular web application from version 14 to 15 and then to 16, we encountered an iss ...

Ionic2: Access your account via an External Source

Challenges have arisen in implementing the authentication process for our app according to the client's requirements. I am seeking guidance on how to set up the app so that users are redirected to an external webpage upon opening, where they can comp ...

Issues with Angular 5 application functionality on IOS8 using Browserstack

I encountered an issue while testing my Angular 5 app on an iPhone 6 with IOS8 (Safari) and in Browserstack. The app fails to run, only showing an empty page along with a Javascript error. Any advice or suggestions on how to resolve this? https://i.sstat ...

typescript Object that consists of properties from an array with a defined data type

I have an array consisting of strings. I am trying to transform this array into an object where each string is a property with specific attributes assigned to them. export interface SomeNumbers { name: string; value: number; } const arr = ['apple& ...

Recommendations for organizing code with respect to models in Angular using TypeScript

Within my C# backend, I structure my entities and entity lists using classes. Any needed functions or methods are simply added to the corresponding class. On the contrary, in Angular/TypeScript examples, models are typically defined as interfaces. This ra ...

The React table column definition inexplicably transforms into a string

When working with react-table in Typescript, I encountered an issue while defining the type for my columns within a custom hook. It seems that when importing the hook and passing the columns to my Table component, they are being interpreted as strings inst ...

Creating consistent heights for buttons and input fields in Angular Material can be achieved by adjusting the

I have a simple form with two outline fields and a button. <form [formGroup]="filterForm"> <section> <ng-container> <mat-form-field appearance="outline" class="mr-1"> ...

Adding a parameter to each route by default."

In my Angular app, the route structure is as follows: const routes: Routes = [ { path: ':lang', children: [ { path: 'home', component: HomeComponent }, { path: 'dashboard', component: DashboardC ...

I am experiencing an issue in Angular 6 where the close button is unresponsive when I add a carousel within a

I successfully added a carousel in a modal popup and it is working properly. However, I encountered an issue when trying to close the modal using the close button - it is not functioning. Here is a link to the stackblitz demo for reference. You can also ...

Why is it that TypeScript's flow analysis does not extend to the 'else' block?

Consider the code below: function f(x : number) { if (x === 1) { if (x === 2) {} // error } else { if (x === 1) {} // OK } } The compiler flags an error on x === 2. This is because if the code reaches this block, x must be ...

The connection could not be established due to an error, and there was also a failure in loading the resource with the error message "net::ERR

I'm attempting to implement this particular example utilizing SignalR with .NET Core and Angular while incorporating the ABP Framework, However, upon running the example, I continuously encounter the following Errors in the browser console: https:// ...

Sample test scenario for a service function that includes an HTTP subscription using Angular's HTTP RxJS

I have a service method that includes an HTTP call and subscribes immediately to the response code in order to execute the rest of the action command based on it. Here is an example of the service method: processData(id): void { const url = `http://l ...