feat(resovler): impl Into for IOError (#1223)

patch for rspack issue
[#4564](https://github.com/web-infra-dev/rspack/issues/4564)
This commit is contained in:
zhangpeng 2023-11-11 23:21:51 +08:00 committed by GitHub
parent b7e8feb7c1
commit 0fa24bcafc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View file

@ -129,6 +129,13 @@ impl PartialEq for IOError {
}
}
impl From<IOError> for std::io::Error {
fn from(error: IOError) -> Self {
let io_error = error.0.as_ref();
Self::new(io_error.kind(), io_error.to_string())
}
}
impl From<io::Error> for ResolveError {
fn from(err: io::Error) -> Self {
Self::IOError(IOError(Arc::new(err)))

View file

@ -5,7 +5,7 @@
use serde_json::json;
use crate::{MatchObject, PathUtil, ResolveContext, ResolveError, ResolveOptions, Resolver};
use std::path::Path;
use std::{io::ErrorKind, path::Path};
#[test]
fn test() {
@ -1296,3 +1296,17 @@ fn test_cases() {
}
}
}
#[test]
fn test_into_io_error() {
let error_string = "IOError occurred";
let string_error = std::io::Error::new(ErrorKind::Interrupted, error_string.to_string());
let resolve_io_error: ResolveError = string_error.into();
if let ResolveError::IOError(io_error) = resolve_io_error {
// fix for https://github.com/web-infra-dev/rspack/issues/4564
let std_io_error: std::io::Error = io_error.into();
assert_eq!(std_io_error.kind(), ErrorKind::Interrupted);
assert_eq!(std_io_error.to_string(), error_string);
}
}