JavaScript anti-silent techniques to indicate failure

末鹿安然 提交于 2019-12-10 02:09:40

问题


What would be a good way to report errors in JavaScript instead of relying on nulls, and undefineds when errors do occur and a function is unable to proceed forward. I can think of three approaches:

  1. do nothing
  2. throw an exception
  3. assert

Here's a simple example scenario - a function that credits a user account with the amount passed in. The function credit is part of an Account object.

Here's the naive solution.

function credit(amount) {
   this.balance += amount;
}

A major problem with this approach is invalid data. Let's fix it up, and use a return value to indicate that the operation failed.

function credit(amount) {
    if(!isInt(amount)) {
        return false;
    }
    this.balance += amount;
}

This is an improvement over the previous one, but client code will have to check the return value to ensure that the operation succeeded. Doing this for basically every method in the system can become cumbersome.

if(!johnDoe.credit(100)) {
    // do some error handling here
}

A third approach, similar to the second one, is to throw an exception. Since we are throwing the exception ourselves, could throw a specific type of exception rather than a general one.

function credit(amount) {
    if(!isInt(amount)) {
        throw new InvalidAmountError(amount);
    }
    this.balance += amount;
}

A fourth approach similar to throwing exceptions is to use assertions in your code. One shortcoming compared to the above approach is that since the assert is generic, we lose the ability to throw custom exceptions. It is still possible though by passing the object to throw in each assert call.

function credit(amount) {
    assert(!isInt(amount), "Amount to credit is not an integer");
    this.balance += amount;
}

A global assert function is easy to write and makes the code a little shorter.

function assert(value, message) {
    if(value !== true) {
        throw new AssertionError(message);
    }
}

function AssertionError(message) {
    this.message = message;
}

AssertionError.prototype.toString = function() {
    return 'AssertionError: ' + this.message;
}

Out of these approaches, what would be a good way to deal with unexpected values, and unhappy paths. Are there any other approaches not mentioned here that could be useful?


回答1:


Consider a client-side logging framework to report errors, such as Log4js. One can log messages (i.e., info, debug, warn, error) in the browser or persist messages to the server via Ajax, depending on the application.

The Log4js site also provides a list of other JavaScript logging frameworks.

A good approach, using your custom exception example, would be to throw the exception and log the error as you see fit.




回答2:


One shortcoming compared to the above approach is that since the assert is generic, we lose the ability to throw custom exceptions

Not necessarily:

function assert(value,message,Exctype) {
    if(value !== true) throw new Exctype(message);
}

Constructors are functions. Functions are first class values. Feel free to pass 'em around.



来源:https://stackoverflow.com/questions/3269802/javascript-anti-silent-techniques-to-indicate-failure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!