Node.js의 오류 소개

10283 단어 javascriptnode
강력한 Node.js 애플리케이션을 빌드하려면 적절한 방식으로 오류를 처리해야 합니다. Node.js의 오류 처리는 의견이 분분한 주제입니다. 이것은 시리즈의 첫 번째 기사입니다. Node.js의 다양한 종류의 오류와 오류 생성 및 발생에 대한 개요를 제공하는 것을 목표로 합니다.

Node.js의 오류 처리:
  • Node.js의 오류 개요(이 문서)
  • Node.js에서 오류 처리(향후)

  • Node.js에는 어떤 종류의 오류가 있습니까?

    There are basically two groups:

    • Operational Errors
    • Developer Errors

    Operational Errors are errors that happen while a program is working on a task, like a network failure. Handling of operational errors should be covered by applying an appropriate scenario strategy. In case of a network error, a strategy would be to retry the network operation.

    Operation errors are:

    • failed to connect to server
    • failed to resolve hostname
    • invalid user input
    • request timeout
    • server returned a 500 response
    • system is out of memory
    • etc.

    Developer Errors are mistakes of developers, for example invalid input. In these cases the application should not attempt to continue running and should crash with a helpful description so that the developer can fix this issue.

    Developer Errors are:

    • tried to read property of undefined
    • called an asynchronous function without a callback
    • passed a string where an object was expected
    • passed an object where a property is missing but required
    • etc.

    오류 발생

    Typically, an error is dealt with by using the throw keyword to throw an exception. The throw statement throws a user-defined exception and execution of the current function will stop. Statements after throw won't be executed, and the first catch block will receive the error. If no catch block exists in the function context, the program will terminate.

    For example:

    function divideByTwo(amount) {
      if (typeof amount !== 'number')
        throw new Error('amount must be a number');
      return amount / 2;
    }
    

    When divideByTwo is called with an invalid input, a string instead of number, the application will crash, and the stack trace is printed in the console. This stack trace comes from the error object which was created after using the throw keyword. The Error constructor is native to JavaScript, takes a string as the Error message and auto-generates the stack trace when created.

    It is recommended to throw an Error Object , but theoretically any value can be thrown. The stack trace will be lost in that case.

    function divideByTwo(amount) {
      if (typeof amount !== 'number') throw 'amount must be a number'; // NOT RECOMMENDED
      return amount / 2;
    }
    

    네이티브 오류 생성자

    To create an error, call new Error('message') and pass a string value as a message.

    new Error('this is a error message');
    

    There are six other native error constructors that inherit from the base Error constructor in JavaScript:

    • EvalError
    • SyntaxError
    • RangeError
    • ReferenceError
    • TypeError
    • URIError

    A ReferenceError will be automatically thrown, when attempting to refer to a non-existing reference. This node -p 'thisReference' will throw a ReferenceError since the reference does not exist.

    An error object can also have its instance verified, like node -p "const err = new SyntaxError(); err instanceof SyntaxError will return true. This node -p "const err = new SyntaxError(); err instanceof Error will also be valid, since any native error constructor inherits from Error .

    Native errors objects also have a name property, which contains the name of the error that created it. node -p "const err = new RangeError(); console.log('error is: ', err.name);"

    사용자 정의 오류

    The native errors are a rudimentary set of errors that can't replicate all errors that can occur in an application. For that we have custom errors. There are several ways of communicating various errors, the most common two are subclassing native error constructors and using the code property.

    Let's look at an example to see how a custom error with the code property looks like:

    function divideByTwo(amount) {
      if (typeof amount !== 'number')
        throw new TypeError('amount must be a number');
      if (amount <= 0)
        throw new RangeError('amount must be greater than zero');
      if (amount % 2) {
        const err = Error('amount must be even');
        err.code = 'ERR_MUST_BE_EVEN';
        throw err;
      }
      return amount / 2;
    }
    

    Now run the function with divideByTwo(3) in the REPL or create a file and execute the function add the end. The outcome will be something like this:

    # ... filepath
    
    throw err;
    ^
    
    Error: amount must be even
    # ... stack trace
    
    The error can be identified by the code value that was added and then handled accordingly. The code API in Node.js uses a similar approach to create native errors. For a list of possible error codes see in the official docs - Node.js v16.5 - List of Error Codes .

    사용자 지정 오류를 만드는 또 다른 방법은 Error 개체에서 자신을 상속하고 사용자 지정 오류 인스턴스를 만드는 것입니다. OddError 생성자를 만들어 봅시다.

    class OddError extends Error {
      constructor(varName = '') {
        super(varName + ' must be even');
      }
      get name() {
        return 'OddError';
      }
    }
    


    이제 divideByTwo() 를 사용하도록 OddError 를 업데이트합니다. 사용자 지정 오류는 동일한 파일에 있거나 가져와야 합니다.

    function divideByTwo(amount) {
      if (typeof amount !== 'number')
        throw new TypeError('amount must be a number');
      if (amount <= 0)
        throw new RangeError('amount must be greater than zero');
      if (amount % 2) throw new OddError('amount');
      return amount / 2;
    }
    


    출력은 다음과 같습니다.

    # ... file path
        if (amount % 2) throw new OddError('amount');
                        ^
    
    OddError: amount must be even
    # ... stack trace
    


    사용자 지정 오류 생성자를 사용하는 전략과 코드 속성을 추가하는 전략은 상호 배타적이지 않으므로 동시에 사용할 수 있습니다. OddError 예제를 업데이트해 보겠습니다.

    class OddError extends Error {
      constructor(varName = '') {
        super(varName + ' must be even');
        this.code = 'ERR_MUST_BE_EVEN';
      }
      get name() {
        return `OddError [${this.code}]`;
      }
    }
    


    실행 후 출력은 다음과 같습니다.

    # ... file path
    if (amount % 2) throw new OddError('amount');
                        ^
    OddError [ERR_MUST_BE_EVEN]: amount must be even
    # ... stack trace
    


    TL;DR

    • Errors in Node.js are handled through exceptions.
    • An error can be created with using the constructor new Error('error message') and thrown using the throw keyword.
    • Always throw Error object instead of value to keep stack trace.
    • There are six native error constructors which inherit from Error .
    • Custom errors can be created with the code property and/or using a constructor with inheriting from the Error object.

    Thanks for reading and if you have any questions , use the comment function or send me a message .

    If you want to know more about Node , 이것들을 보세요 Node Tutorials .

    참조(그리고 큰 감사):

    JSNAD , MDN Errors , MDN throw , Node.js Error Codes , Joyent

    좋은 웹페이지 즐겨찾기