Error.isError()

Limited availability

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

Phương thức tĩnh Error.isError() xác định xem giá trị được truyền vào có phải là một Error hay không.

Cú pháp

js
Error.isError(value)

Tham số

value

Giá trị cần kiểm tra.

Giá trị trả về

true nếu value là một Error; ngược lại là false.

Mô tả

Error.isError() kiểm tra xem giá trị được truyền vào có phải là một Error hay không. Nó thực hiện điều này bằng cách thực hiện kiểm tra nhãn hiệu (branded check) cho một trường private được khởi tạo bởi constructor Error(). Đây là cơ chế tương tự được sử dụng bởi Array.isArray(), tương tự như cơ chế được sử dụng bởi toán tử in.

Đây là một lựa chọn thay thế mạnh mẽ hơn cho instanceof Error vì nó tránh được kết quả dương tính giả và âm tính giả:

  • Error.isError() từ chối các giá trị không phải là các instance Error thực sự, ngay cả khi chúng có Error.prototype trong chuỗi prototype của chúng — instanceof Error sẽ chấp nhận những giá trị này vì nó kiểm tra chuỗi prototype.
  • Error.isError() chấp nhận các đối tượng Error được tạo trong một realm khác — instanceof Error trả về false cho những đối tượng này vì danh tính của constructor Error khác nhau giữa các realm.

Error.isError() trả về true cho các instance DOMException. Điều này là vì, mặc dù DOMException không được chỉ định là một lớp con thực sự của Error (constructor Error không phải là prototype của constructor DOMException), DOMException vẫn hoạt động như Error cho tất cả các mục đích kiểm tra nhãn hiệu.

Ví dụ

Sử dụng Error.isError()

js
// all following calls return true
Error.isError(new Error());
Error.isError(new TypeError());
Error.isError(new DOMException());
try {
  1 + 1n;
} catch (e) {
  console.log(Error.isError(e)); // The operation threw a TypeError, so this returns true
}

// all following calls return false
Error.isError();
Error.isError({});
Error.isError(null);
Error.isError(undefined);
Error.isError(17);
Error.isError("Error");
Error.isError(true);
Error.isError(false);
// This is not an error, because the object does not have the private field
// initialized by the Error constructor
Error.isError({ __proto__: Error.prototype });

instanceof vs. Error.isError()

Khi kiểm tra instance Error, Error.isError() được ưu tiên hơn instanceof vì nó hoạt động trên nhiều realm.

js
const iframe = document.createElement("iframe");
document.body.appendChild(iframe);
const xError = window.frames[window.frames.length - 1].Error;
const error = new xError();

// Correctly checking for Error
Error.isError(error); // true
// The prototype of error is xError.prototype, which is a
// different object from Error.prototype
error instanceof Error; // false

Chuẩn hóa các lỗi bị bắt

Bạn có thể sử dụng Error.isError() để phát hiện xem giá trị bị bắt có phải là lỗi hay không và chuẩn hóa nó thành một đối tượng lỗi.

js
try {
  throw "Oops; this is not an Error object";
} catch (e) {
  if (!Error.isError(e)) {
    e = new Error(e);
  }
  console.error(e.message);
}

Đặc tả

Specification
ECMAScript® 2027 Language Specification
# sec-error.iserror

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

Xem thêm