Prevent native Error class hi-jacking in FS errors
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- JavaScript
- Star
- 122k
- Fork
- 37.3k
- Merge trung bình
- 4 ngày 2 giờ
- Pull request đã merge (30 ngày)
- 283
Mô tả
Version
v16.9.0
Platform
Linux vagrant-docker 4.19.0-6-amd64 #1 SMP Debian 4.19.67-2+deb10u2 (2019-11-11) x86_64 GNU/Linux
Subsystem
fs
What steps will reproduce the bug?
In the current state of the promises section of the native fs module, when an error occurs, the native type Error is hi-jacked and a code property is being added to the error object.
This can easily be reproduced with the following code:
import { promises as FileSystem } from "fs";
try
{
FileSystem.stat("/a/path/leading/to/nothing");
}
catch (error)
{
console.log(Object.getPrototypeOf(error); // Will display the native class "Error"
console.log(Object.getOwnPropertyDescriptors(error); // Will display the added code property that isn't native to Error.
}
How often does it reproduce? Is there a required condition?
No required conditions outside of the use of the fs native module.
What is the expected behavior?
This isn't a bug per-se but is a bad practice. A custom error of type FileSystemError should be thrown instead of hi-jacking the Error class to dynamically add a property to the object.
This behavior leads to problematic linting and testing with super-sets such as TypeScript or Eslint. Since code is not a native property of Error and dynamic property setting is often considered bad practice, it leads to situations where handling those specific issues requires extra effort to circumvent this design.
What do you see instead?
No response
Additional information
Ideally a custom FileSystemError should be created such as (Typescript example for types clarity):
class FileSystemError extends Error
{
public code: string; // Ideally should be an enum listing all the possible values.
}
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng cách lần theo đường dẫn fs promises gốc được FileSystem.stat sử dụng khi báo cáo một đường dẫn không tồn tại, sau đó kiểm tra cách đối tượng Error hiện tại nhận thuộc tính code của nó. Công việc được xem là hoàn tất khi các lỗi sử dụng một kiểu lỗi hệ thống tệp chuyên dụng với thông tin code cần thiết, và các bài kiểm thử bao quát hành vi của promises API.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- javascript
- Lĩnh vực
- backend, operating-systems
- Loại issue
- Tính năng
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Ít trao đổi
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 38/100