Is the Bluebird.js custom Error catch function not effective for the initial promise?

I am currently experimenting with the custom error handlers in Bluebird.js.

In the code snippet below, I noticed that the catch-all handler gets called instead of the MyCustomError handler. However, when I moved the rejection inside the then function (and resolved the firstPromise...), the MyCustomError handler was triggered. Can anyone explain why this is happening? Did I make a mistake somewhere? Thank you.

var Promise = require('bluebird'),
debug = require('debug')('main');

firstPromise()
    .then(function (value) {
      debug(value);
    })
    .catch(MyCustomError, function (err) {
      debug('from MyCustomError catch: ' + err.message);
    })
    .catch(function (err) {
      debug('From catch all: ' + err.message);
    });

/*
 * Function that returns a promise.
 * */
function firstPromise() {
  return new Promise(function (resolve, reject) {
    reject(new MyCustomError('error from firstPromise'));
  });
}
/*
 * Custom Error Definition
 * */
function MyCustomError(message) {
  this.message = message;
  this.name = "MyCustomError";
  Error.captureStackTrace(this, MyCustomError);
}
MyCustomError.prototype = Object.create(Error.prototype);
MyCustomError.prototype.constructor = MyCustomError;

Answer №1

Make sure to declare the error class at the beginning of your code for it to function correctly (prototypes are not hoisted)

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

Is the camera.lookAt() method still available in the THREE.js library?

While searching for information on setting up the camera, I came across references to the lookAt function in various posts. However, when I checked the THREE.js documentation, I couldn't locate this method. Can anyone help me understand where it went ...

Displaying search results in various Angular components

On my home page (homePageComponent), I have a search feature. When the user clicks on the search button, they are redirected to a different page called the search list page (searchListComponent). Within the searchListComponent, there is another component c ...

Detect Empty Fields as Invalid in Angular 2+ Form Validation

I have been implementing Form Validations in my form using reactive form validation. However, I encountered a challenge when checking a field that is touched and dirty. For example: HTML: <input id="name" class="form-control" formControlName="n ...

Unexpected behavior encountered with Angular module dependency injection

Having some difficulty managing dependencies for my node app. Here's the current structure: app.js var app = angular.module('myApp', ['myController', 'myFactory', 'rzModule', 'chart.js', 'myServ ...

Exploring Unpredictable Motion with HTML5 and Javascript

I am currently diving into the world of HTML5 game development. I want to create a ball that moves randomly on the screen, rather than following a predictable left to right motion. Unfortunately, my current code only allows the ball to move in a set patter ...

Ensure to first close the existing global connection before attempting to establish a new one in your Node.js Post WebApi application

Currently, I am in the process of creating a small post WebAPI using node.js to collect user data such as names and numbers. However, when I have an excessive number of users accessing this web API, I encounter the following error message: "node.js Global ...

What is preventing me from renaming a file in PHP when passed through jQuery?

Hello to all users! I've encountered an issue where I can't seem to change the file name in PHP that is being passed from jQuery: Here is the JavaScript code where I pass the file to the PHP handler: var url = '/temp.php'; var xhr = ...

Displaying a Jquery slider by clicking on links

I am interested in setting up a slider for two different forms. Specifically, I plan to have one form labeled Form 1 and another labeled Form 2 displayed as text. When users click on Form 1, a table containing the form will slide out from underneath the ...

I am receiving an undefined value when using document.getElementsByClassName

<canvas id="can" height="500px" width="1200px"></canvas> <div class="name"> <h1>LEONARDO</h1> </div> <script> var name=['WATSON','LEONARDO',"SMITH","EMILY"] var counter=0 var dat ...

What is the best way to dynamically insert an item into an object?

Currently, I am storing the client and product details along with quantity in an array. Here is how it looks: var idCliente = $scope.myClient.codigo; var nombreCliente = $scope.myClient.nombre; var idProducto = $scope.myProduct.codigo; var ...

Encountering a jQuery error while attempting to initiate an AJAX request

I'm currently working on a project in SharePoint and I want to integrate JQuery to make an ajax call from the homepage. However, when I attempt to make the call, I encounter an error stating "Array.prototype.slice: 'this' is not a JavaScript ...

Is it possible to have Vue.js code within a v-for loop in such a way that it executes every time the v-for loop runs?

<div class="questions" v-for="(q,index) in questions " :key="index" {{this.idx+=1}} > <h3 > {{q.content}}</h3> <h3> A) {{q.a}} </h3> <h3> B) {q.b}} </h3> ...

Discover the largest prime number and display it using JavaScript

Currently learning about node.js and faced with an interesting challenge - Create a program to identify and display the largest prime number that is less than or equal to N. Input // Output - 13 // 13 126 // 113 26 // 23 During my previous course with ...

Issue with VueUse useStorage function failing to update stored object properties

Having difficulty updating a property in an object stored in localStorage using the useStorage function. Inside the App.vue file: routeStore.query = { tab: 'products', subTab: 1, search: '', article: '', } console.log ...

Converting Typescript to Javascript: How to export a default object

Perhaps this question has been addressed before in some manner, however, I was unsure of how to phrase it. In my Typescript file, there is a single class being exported: export class MyClass { ... } In another Javascript file, I import the transpile ...

JavaScript array displaying excess whitespace in its presentation

https://i.sstatic.net/5AdA7.png One issue I am facing is that when I use console.log(a), it adds extra spaces which then flow through to the backend. This leads to each element breaking after the first one. How can I resolve this problem and why does it o ...

What is the best method for extracting `window.initialState` from a webpage?

There is a specific section in the window.initialState at the bottom of the page that I would like to extract. How can I go about retrieving this information? Note: I am utilizing Selenium with a Chromedriver for web scraping, making it impossible to use ...

Why does e.target.value only capture the initial letter of a string input in ReactJS?

Is the current method correctly capturing only the first letter in the string input? class Example extends React.Component{ state={ name:'Ajith' } changeEvent = (e) => { console.log('change : '+this.s ...

Endless spinning using snap.svg

How do I make an SVG element rotate endlessly? Is there a way to make it rotate continuously without stopping? While Snap provides a way to rotate an SVG element by a specific amount, I want to know how to create an infinite rotation effect. Is there a so ...

Looking to replace an existing class with a new one in a div element?

I have developed a form builder where selecting a value option from 1 to 12 will append a col-xs-(1-12) class to a div. However, I am facing an issue where choosing a different option does not remove the previously added class. function addGrid() { ...