Unable to suppress error in AngularJS $http.get() call when using catch() method

Below is a simplified version of the code I'm working with, running in CodePen for example:

var app = angular.module("httptest", []);

app.controller("getjson", ["$scope", "$http", function($scope, $http) {
  $http.get("https://codepen.io/anon/pen/LVEwdw.js").
  then((response) => {
    console.log(response.data)
    console.log('in then')
    throw 'the error'
  }).catch((e) => {
    console.log('in the catch')
    console.log(e);
  });
}]);

I expected that if the catch() block is executed, the error would not display in red on the console unless specifically logged. However, the error still appears in red before entering the catch() block. I tried to create an equivalent example without using AngularJS's $http where the behavior matched my expectation:

var promise1 = new Promise(function(resolve, reject) {
  resolve();
});

promise1.then((result) => {
  console.log('about to throw error')
  throw 'hey'
 }).catch(function(error) {
  console.log(error);
});

In this alternative example, the error does not appear in red.

Why is the handled error displaying in red in the $http scenario and how can it be suppressed?

Answer №1

That issue was resolved in the latest version of AngularJS 1.6:

According to recent GitHub commits:

fix($q): handling thrown errors as regular rejections

Prior to this fix, errors thrown within a promise's onFulfilled or onRejected handlers were handled differently than normal rejections: They were reported to the $exceptionHandler() (in addition to being converted into rejections).

AngularJS GitHub Commit e12eea

For more information, visit the AngularJS Developer Guide - Migrating to V1.6 - $q

Answer №2

To discover the rationale, refer to line 8416 within your associated angularjs module. Their approach involves utilizing console.error in the catch statement. It is possible to customize this functionality by implementing an empty handler:

angular.module('exceptionOverride', []).factory('$exceptionHandler', () => (exception, cause) });

var app = angular.module("httptest", ['exceptionOverride']);

....

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

Transform an array of object's designated key values into a string that is separated by commas

Is there a way to convert specific key values of an object into a comma-separated string? I have been able to do this with arrays, but my current challenge is that my data is an array of objects. I want to convert each 'id' value into an array of ...

I am puzzled by the fact that my data is showing as undefined even though I have logged it to the console in

I am currently working on an app using React for the frontend and Node.js for the backend, which connects to a cloud-based MongoDB instance. Although I can successfully retrieve data from the database and see it logged in the console, the returned result i ...

How can I modify the value of a CSS animation rule in AngularJS?

I am looking to dynamically change the value assigned to stroke-dashoffset based on a custom input. @-webkit-keyframes donut-chart-1 { to { stroke-dashoffset: 100; } } @keyframes donut-chart-1 { to { stroke-d ...

Using Inline Styling to Showcase a Background Image in a REACTJS Component

import React from 'react'; import Slick from 'react-slick'; import style from './Slider.module.css'; import {Link} from 'react-router-dom'; const SliderTemplates = (props) => { let template = null; const ...

Unable to transmit an object containing a property that includes an array of other objects

I am attempting to send an object that has the following structure: var postBody = { id: userID, objectID: [0, 1], objects: [ {id: 0, x: 0.33930041152263374, y: 0.08145246913580247, width: 0.0823045267489712, height: 0. ...

AJAX is not meeting my expectations

Is there an issue with this code? It's supposed to insert data into the database but I'm not getting any errors. I am new to using ajax, and I found this code which I modified slightly. I thought it would work, but it's not functioning as e ...

Is there a specific method to access a JSON file with (js/node.js)?

Searching for a way to access user information stored in a JSON file using the fs module in node.js. Specifically looking to read only one user at a time. app.get("/1", function(req, res) { fs.readFile("users.json",function(data, err){res.write(data)}} W ...

Ensure the computed variable is always up-to-date using Firebase in VueJS

After a successful sign-in through Firebase authentication, I need to update a navigation link. The user login changes are managed within the created hook using .onAuthStateChanged() data () { return { user: null, additionaluserinfo: nul ...

Encountering a fatal error in the Next.js application: "Mark-compacts near heap limit allocation failed issue is hindering the smooth

When I'm working in Next.js, I often encounter the issue of not being able to run my project after work. https://i.stack.imgur.com/IA5w3.png ...

What could be causing the onclick function to not activate on the iOS safari browser?

My Shopify site works perfectly on all browsers except iOS Safari. When users try to click the "add to cart" button on this specific browser, it does not trigger the onclick function. This issue is unique to iOS Safari as the button works fine on desktop a ...

Error! D3.js is throwing an Uncaught TypeError because it cannot find the function .enter(...).append(...).merge

Currently, I am facing an issue while attempting to construct a table with multiple nested dropdowns using d3.js. The problem arises when switching from a newer version of d3 where my code functions flawlessly to d3.js v3. Upon replacing the newer version ...

Enhance the Angular KendoGrid excelExport feature with a custom column addition event

When utilizing the excelExport event in KendoGrid to modify certain column data before exporting it, is there a way to insert a new column between two existing columns? Here's the current code I'm using to manipulate dates. I would like to add a ...

Have you ever wondered why MEAN.js applications use the #! symbol at the start of their URLs

Embarking on my first MEAN application build using MEAN.JS, I was intrigued by the structure of how mean.js organizes the application, particularly why URLs begin with /#!/. For instance, for the login page, I envisioned: http://example.com/login instea ...

Navigating to the present child state with modified parameters can be achieved using the following steps

Check out this demo: https://jsfiddle.net/explorer/622qzqsc/ In the platform I'm working on, we have an advanced concept of state management, specifically for building timelines. In the template of this system, there's a code snippet (which is c ...

By default, Nuxt 2.15.7 is automatically installed when you create a new Nuxt app

I am looking to develop a Nuxt app (version 3) using Vue. However, when I run the command npm create nuxt-app@version mousa, it automatically installs Nuxt2. How can I install Nuxt3 instead with this command? ...

Setting up Stylelint in a Vue 3 app with VSCode to automatically lint on save

I am looking to perform linting on my scss files and scss scope within .vue components. Here is what my configuration looks like in stylelint.config: module.exports = { extends: [ 'stylelint-config-standard', 'stylelint-config-rece ...

The issue arises when Angular routing fails to locate the Django template files

I have been working on a project that involves utilizing Django along with AngularJs routing. Within my URL configurations for Angular routing, I have the following code: app.config(function($routeProvider) { $routeProvider .when( ...

Is there a way to remove any incomplete information from the output (window.alert) in JavaScript when a user does not fill in all the prompts?

I am seeking input for 8 pieces of information to be displayed in an alert box, only if the user enters something in each field. All the gathered data will be shown in a single alert box once the user confirms their desire to review it. If the user choos ...

Merging Objects and inserting them into an array - Harnessing the power of JavaScript and

Having various objects stored in an array, each with a unique labelId assigned to them. Whenever there are matching label ids, the goal is to merge these objects together by consolidating some values inside a new combined object array. [ { fi ...

I attempted to post a collection of strings in ReactJS, only to receive a single string response from the Web

I was troubleshooting an issue where only one string from ReactJS was being sent to WebApi instead of an array of strings. Below is the ReactJS code snippet I used: import React, { useState } from "react"; import axios from "axios"; e ...