Skip to content

Commit

Permalink
feat: make BuildError newtype (#326)
Browse files Browse the repository at this point in the history
<!-- Thank you for contributing! -->

### Description

This is a preparation for supporting warnings and error tracing. We need add some fields on `BuildError`.

<!-- Please insert your description here and provide especially info about the "what" this PR is solving -->

### Test Plan

<!-- e.g. is there anything you'd like reviewers to focus on? -->

---
  • Loading branch information
hyf0 authored Nov 19, 2023
1 parent cba0349 commit 475907e
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 74 deletions.
55 changes: 55 additions & 0 deletions crates/rolldown_error/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::{
borrow::Cow,
path::{Path, PathBuf},
};

use miette::Diagnostic;
use thiserror::Error;

use crate::error_kind::{
external_entry::ExternalEntry, unresolved_entry::UnresolvedEntry,
unresolved_import::UnresolvedImport, ErrorKind,
};

type StaticStr = Cow<'static, str>;

#[derive(Error, Debug, Diagnostic)]
#[error(transparent)]
#[diagnostic(transparent)]
pub struct BuildError(ErrorKind);

impl BuildError {
pub fn new_with_kind(kind: ErrorKind) -> Self {
Self(kind)
}

// --- Aligned with rollup
pub fn entry_cannot_be_external(unresolved_id: impl AsRef<Path>) -> Self {
Self::new_with_kind(ErrorKind::ExternalEntry(
ExternalEntry { id: unresolved_id.as_ref().to_path_buf() }.into(),
))
}

pub fn unresolved_entry(unresolved_id: impl AsRef<Path>) -> Self {
Self::new_with_kind(ErrorKind::UnresolvedEntry(
UnresolvedEntry { unresolved_id: unresolved_id.as_ref().to_path_buf() }.into(),
))
}

pub fn unresolved_import(specifier: impl Into<StaticStr>, importer: impl Into<PathBuf>) -> Self {
Self::new_with_kind(ErrorKind::UnresolvedImport(
UnresolvedImport { specifier: specifier.into(), importer: importer.into() }.into(),
))
}

// --- rolldown specific
pub fn napi_error(status: String, reason: String) -> Self {
Self::new_with_kind(ErrorKind::Napi { status, reason })
}
}

impl From<std::io::Error> for BuildError {
fn from(e: std::io::Error) -> Self {
Self::new_with_kind(ErrorKind::Io(Box::new(e)))
}
}
73 changes: 0 additions & 73 deletions crates/rolldown_error/src/error/mod.rs

This file was deleted.

40 changes: 40 additions & 0 deletions crates/rolldown_error/src/error_kind/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::borrow::Cow;

pub mod external_entry;
// pub mod impl_to_diagnostic;
pub mod unresolved_entry;
pub mod unresolved_import;

use miette::Diagnostic;
use thiserror::Error;

use self::{
external_entry::ExternalEntry, unresolved_entry::UnresolvedEntry,
unresolved_import::UnresolvedImport,
};

type StaticStr = Cow<'static, str>;

#[derive(Error, Debug, Diagnostic)]
pub enum ErrorKind {
#[diagnostic(transparent)]
#[error(transparent)]
UnresolvedEntry(Box<UnresolvedEntry>),

#[diagnostic(transparent)]
#[error(transparent)]
ExternalEntry(Box<ExternalEntry>),

#[diagnostic(transparent)]
#[error(transparent)]
UnresolvedImport(Box<UnresolvedImport>),

#[diagnostic(code = "IO_ERROR")]
#[error(transparent)]
Io(Box<std::io::Error>),

// TODO: probably should remove this error
#[diagnostic(code = "NAPI_ERROR")]
#[error("Napi error: {status}: {reason}")]
Napi { status: String, reason: String },
}
2 changes: 1 addition & 1 deletion crates/rolldown_error/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod error;
// mod error_code;
mod error_kind;
mod utils;

use std::path::Path;
Expand Down

0 comments on commit 475907e

Please sign in to comment.