2

I've created a class with es6 that requires a property to be defined in the options argument.

class MyClass{
     constructor(options){
         if(typeof options.name === "undefined")
            console.error("MyClass.constructor: options.name is not defined");
         return this;
     }
}

I can log an error, but I don't want the user of this class to continue. What do I return? Should I still return the instance of the class?

Andy G
  • 19,232
  • 5
  • 47
  • 69
Tester232323
  • 301
  • 1
  • 11

3 Answers3

4

I would throw an error in this case. I'd do something like this:

class MyClass{
     constructor(options){
         if(typeof options.name === "undefined")
            throw new Error("MyClass.constructor: options.name is not defined");
         return this;
     }
}

You can read more about throw here and here. This is a similar SO Q&A.

Community
  • 1
  • 1
Tamas Rev
  • 7,008
  • 5
  • 32
  • 49
2

If you are going to throw then you should use the appropriate error type so that the interface user can take the correct action. Include the filename and as much details as you can. You want to help the API user as much as possible.

class MyClass{
   constructor ( options ) {
       if(options === undefined || options === null) {
           throw new ReferenceError("MyClass constructor missing required argument `options`.", "filename.js");
       } else
       if(options.name === undefined) {
           throw new ReferenceError("MyClass constructor missing required property `options.name`.", "filename.js");
       } else
       if( typeof options.name !== "string") {
            throw new TypeError("Argument `options.name` should be a string.","filename.js"); 
       } else 
       if( options.name === "" || options.name.trim() === ""){
            throw new RangeError("Argument `options.name` has an invalid value. It requiers one or more alphaNumeric chanracters.","filename.js");
       } else
       if( options.name.length < 5 ){
            console.warning("MyClass constructor. It is recogmended that 'options.name' has more than 5 characters");
       }
       ... all good continue construction
   }
}

This way the programmer has a choice of what to do. It may require additional client input (a mistyped form) allowing the app to try again. Or the error can be handed on to reporting interfaces that can log the more serious problems. We must always provide every opportunity to resolve the problems, and provide as much information as possible. There is nothing worse than non descriptive generic errors, the last thing someone using your API wants to do is have to go into it and find out what is going on and how to get past the error if there is a way.

Blindman67
  • 51,134
  • 11
  • 73
  • 136
1

If there aren't too many properties in the options I would probably define a default so I don't have to throw an error or prevent the code from continuing.

class A {
  constructor({ name = 'Andy', age }) {
    console.log(name, age) // Andy, 20
  }
}

var a = new A({ age: 20 });
Andy
  • 61,948
  • 13
  • 68
  • 95