SuppressedError

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Đối tượng SuppressedError đại diện cho một lỗi được tạo ra trong quá trình xử lý một lỗi khác. Nó được tạo ra trong quá trình giải phóng tài nguyên khi sử dụng using hoặc await using.

So với AggregateError, SuppressedError đại diện cho một lỗi xảy ra trong quá trình xử lý một lỗi khác, trong khi AggregateError đại diện cho một danh sách các lỗi không liên quan. Tuy nhiên, một SuppressedError có thể chứa một chuỗi các lỗi bị che khuất (e.suppressed.suppressed.suppressed...). Nó cũng có ngữ nghĩa khác với cause vì lỗi không phải do một lỗi khác gây ra, mà xảy ra trong khi xử lý một lỗi khác.

SuppressedError là một subclass của Error.

Constructor

SuppressedError()

Tạo một đối tượng SuppressedError mới.

Thuộc tính instance

Cũng kế thừa các thuộc tính instance từ lớp cha Error.

Các thuộc tính này được định nghĩa trên SuppressedError.prototype và được chia sẻ bởi tất cả các instance của SuppressedError.

SuppressedError.prototype.constructor

Hàm constructor đã tạo ra đối tượng instance. Với các instance của SuppressedError, giá trị ban đầu là constructor SuppressedError.

SuppressedError.prototype.name

Đại diện cho tên của loại lỗi. Với SuppressedError.prototype.name, giá trị ban đầu là "SuppressedError".

Note: SuppressedError không bao giờ có thuộc tính cause, vì ngữ nghĩa của cause trùng lặp với suppressed.

Các thuộc tính sau là thuộc tính riêng của mỗi instance SuppressedError.

error

Tham chiếu đến lỗi gây ra việc bị che khuất.

suppressed

Tham chiếu đến lỗi bị che khuất bởi error.

Phương thức instance

Kế thừa các phương thức instance từ lớp cha Error.

Ví dụ

Bắt một SuppressedError

Một SuppressedError được ném khi có lỗi xảy ra trong quá trình giải phóng tài nguyên. Việc ném một lỗi gây ra quá trình dọn dẹp scope, và mỗi disposer trong quá trình dọn dẹp có thể ném lỗi riêng của nó. Tất cả các lỗi này được thu thập thành một chuỗi các instance SuppressedError, với lỗi ban đầu là thuộc tính suppressed và lỗi mới do disposer tiếp theo ném là thuộc tính error.

js
try {
  using resource1 = {
    [Symbol.dispose]() {
      throw new Error("Error while disposing resource1");
    },
  };
  using resource2 = {
    [Symbol.dispose]() {
      throw new Error("Error while disposing resource2");
    },
  };
  throw new Error("Original error");
} catch (e) {
  console.log(e instanceof SuppressedError); // true
  console.log(e.message); // "An error was suppressed during disposal"
  console.log(e.name); // "SuppressedError"
  console.log(e.error); // Error: Error while disposing resource1
  console.log(e.suppressed); // SuppressedError: An error was suppressed during disposal
  console.log(e.suppressed.error); // Error: Error while disposing resource2
  console.log(e.suppressed.suppressed); // Error: Original error
}

Chuỗi trông như thế này:

       SuppressedError --suppressed--> SuppressedError --suppressed--> Original error
              |                               |
            error                           error
              v                               v
Error while disposing resource1   Error while disposing resource2
    (Disposal happens later)        (Disposal happens earlier)

Tạo một SuppressedError

js
try {
  throw new SuppressedError(
    new Error("New error"),
    new Error("Original error"),
    "Hello",
  );
} catch (e) {
  console.log(e instanceof SuppressedError); // true
  console.log(e.message); // "Hello"
  console.log(e.name); // "SuppressedError"
  console.log(e.error); // Error: "New error"
  console.log(e.suppressed); // Error: "Original error"
}

Đặc tả

Specification
ECMAScript Async Explicit Resource Management
# sec-suppressederror-objects

Tương thích trình duyệt

Xem thêm