Erwin Kraan
Erwin Kraan

Reputation: 278

Custom exception. This undefined

I'm trying to create my own exception. So i can format the error based on the environment the nodejs server is running. When the error is thrown this is undefined.

(function () {
  'use strict';
  var states = require('../states');
  var env = states.config.env;
  var _   = require('underscore');

  /**
   * This is a error that is going to be thrown on server errors.
   * The application format the message for the specific environment
   * @param error The error
   */
  var unrecoverableError = function (error) {
    this.name = 'unrecoverableError';
    this.message = _.isEqual(env, 'production') ? 'There was a server error. Please contact server admin' : error.toString();
    this.code = 500;
  };

  unrecoverableError.prototype = Object.create(Error.prototype);
  unrecoverableError.prototype.constructor = unrecoverableError;

  module.exports = unrecoverableError;
}());

I also use sequelize as ORM.

organisation.findOne({
        where: {
          name: organisationName
        }
      })
        .then(function (organisation) {
          if (_.isEmpty(organisation)) {
            throw new modelNotFoundException();
          } else {
            resolve(organisation);
          }
        })
        .catch(function (error) {
          if (error instanceof modelNotFoundException) {
            reject(error);
          } else {
            throw new unrecoverableError(error);
          }
        })
        .catch(function (error) {
          reject(error);
        });

Then in my console i get the error.

[TypeError: Cannot set property 'name' of undefined]

I can't figure out what i did wrong. It's working in the browser. Here is a working fiddle example. https://jsfiddle.net/y3gk0hos/

Thanks in advance

Upvotes: 1

Views: 391

Answers (2)

Jason Livesay
Jason Livesay

Reputation: 6377

I dont see why its important to only show the error message in the production environment.

Here is some code to follow for creating a custom error in Node: https://gist.github.com/justmoon/15511f92e5216fa2624b So refactor to match that format.

But that seem like it might be more work than its worth. You can probably just do something like this: throw new Error('Unrecoverable: ' + e.message).

Upvotes: 1

gnerkus
gnerkus

Reputation: 12019

The error occurs because of the presence of the "use strict"; statement which prevents 'illegal' access of the global environment.

If the function unrecoverableError is called without the new keyword, the this object would point to the global environment. While permitted in a browser, the 'use strict'; statement disallows this.

To ensure the unrecoverableError method instantiates an object, you'll need to check if it was executed without the new keyword and force the proper use:

var unrecoverableError = function (error) {
  // check if called with 'new'
  if (this instanceof unrecoverableError) {
    this.name = 'unrecoverableError';
    this.message = 'There was a server error. Please contact server admin';
    this.code = 500;
  }
  else {
    // method was not called with 'new'
    return new unrecoverableError(error);
  }
};

Upvotes: 1

Related Questions