Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: use an Error userdata instead of a plain error code for I/O errors #1939

Merged
merged 5 commits into from
Nov 23, 2024

Conversation

sxyazi
Copy link
Owner

@sxyazi sxyazi commented Nov 23, 2024

Problem

In practice, most of these I/O errors need to be displayed to the end user through logs or prompts. Showing an error code to the user doesn't make much sense:

local _, code = fs.remove("file", Url("/foo"))
ya.dbg("Failed to remove `/foo`, error code: " .. code)

which prints:

Failed to remove `/foo`, error code: 2

Goal

This PR changes the type of I/O errors from a number to a new Error type.

The Error type implements the __tostring() and __concat() meta method, allowing it to generate more human-readable error messages:

local _, err = fs.remove("file", Url("/foo"))
ya.dbg("Failed to remove `/foo`, error: " .. err)

which prints:

Failed to remove `/foo`, error: No such file or directory (os error 2)

In certain cases where the raw error code is needed, it can be accessed via the .code property of the Error:

local _, err = fs.remove("file", Url("/foo"))
ya.dbg("Failed to remove `/foo`, error code: " .. err.code)

which prints:

Failed to remove `/foo`, error code: 2

Breaking changes

After this PR, the second return value of the following functions will change from a numeric error code to the new Error type:

fs API:

  • fs.cha()
  • fs.write()
  • fs.remove()
  • fs.read_dir()
  • fs.unique_name()

Command API:

  • Command:spawn()
  • Command:output()
  • Command:status()

Child API:

  • Child:write_all()
  • Child:flush()
  • Child:wait()
  • Child:wait_with_output()
  • Child:start_kill()

@sxyazi sxyazi merged commit 8055972 into main Nov 23, 2024
6 checks passed
@sxyazi sxyazi deleted the pr-626260a8 branch November 23, 2024 10:30
boydaihungst added a commit to boydaihungst/file-extra-metadata.yazi that referenced this pull request Nov 23, 2024
boydaihungst added a commit to boydaihungst/fuse-archive.yazi that referenced this pull request Nov 23, 2024
boydaihungst added a commit to boydaihungst/simple-mtpfs.yazi that referenced this pull request Nov 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant