ferrijs_std/node/
system_error.rs1use std::io;
2
3use rquickjs::{Ctx, Exception, Result};
4
5pub fn throw(ctx: &Ctx<'_>, error: &io::Error, syscall: &str, path: &str) -> rquickjs::Error {
6 let code = match error.raw_os_error() {
7 Some(libc::ENOENT) => "ENOENT",
8 Some(libc::ENOTDIR) => "ENOTDIR",
9 Some(libc::EISDIR) => "EISDIR",
10 Some(libc::EACCES) => "EACCES",
11 Some(libc::EPERM) => "EPERM",
12 Some(libc::ELOOP) => "ELOOP",
13 Some(libc::ENAMETOOLONG) => "ENAMETOOLONG",
14 Some(libc::EIO) => "EIO",
15 Some(libc::ENOMEM) => "ENOMEM",
16 _ => "UNKNOWN",
17 };
18 let message = format!("{code}: {error}, {syscall} '{path}'");
19 let built: Result<Exception<'_>> = (|| {
20 let exception = Exception::from_message(ctx.clone(), &message)?;
21 exception.set("code", code)?;
22 if let Some(errno) = error.raw_os_error() {
23 exception.set("errno", -errno.abs())?;
24 }
25 exception.set("syscall", syscall)?;
26 exception.set("path", path)?;
27 Ok(exception)
28 })();
29 match built {
30 Ok(exception) => ctx.throw(exception.into_value()),
31 Err(error) => error,
32 }
33}