Skip to content

Commit

Permalink
fix: replace exception_fallthrough with PipelineFallthroughException
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Feb 17, 2021
1 parent 96d928b commit 35ace13
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 34 deletions.
4 changes: 3 additions & 1 deletion beet/toolchain/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

from beet.core.utils import FileSystemPath, JsonDict, TextComponent

from .pipeline import PipelineFallthroughException

class InvalidProjectConfig(Exception):

class InvalidProjectConfig(PipelineFallthroughException):
"""Raised when trying to load an invalid project config."""


Expand Down
21 changes: 10 additions & 11 deletions beet/toolchain/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"Task",
"GenericPlugin",
"GenericPluginSpec",
"PipelineFallthroughException",
"PluginError",
"PluginImportError",
]
Expand All @@ -18,8 +19,6 @@
Optional,
Protocol,
Set,
Tuple,
Type,
TypeVar,
Union,
cast,
Expand All @@ -40,11 +39,15 @@ def __call__(self, ctx: T) -> Any:
GenericPluginSpec = Union[GenericPlugin[T], str]


class PluginError(Exception):
class PipelineFallthroughException(Exception):
"""Exceptions inheriting from this class will fall through the pipeline exception handling."""


class PluginError(PipelineFallthroughException):
"""Raised when a plugin raises an exception."""


class PluginImportError(PluginError):
class PluginImportError(PipelineFallthroughException):
"""Raised when a plugin couldn't be imported."""


Expand All @@ -54,7 +57,6 @@ class Task(Generic[T]):

plugin: GenericPlugin[T]
iterator: Optional[Iterator[Any]] = None
exception_fallthrough: Tuple[Type[Exception], ...] = ()

def advance(self, ctx: T) -> Optional["Task[T]"]:
"""Make progress on the task and return it unless no more work is necessary."""
Expand All @@ -66,7 +68,7 @@ def advance(self, ctx: T) -> Optional["Task[T]"]:
)
for _ in self.iterator:
return self
except (PluginError,) + self.exception_fallthrough:
except PipelineFallthroughException:
raise
except Exception as exc:
raise PluginError(self.plugin) from exc.with_traceback(
Expand All @@ -81,7 +83,6 @@ class GenericPipeline(Generic[T]):

ctx: T
default_symbol: str = "beet_default"
exception_fallthrough: Tuple[Type[Exception], ...] = ()

plugins: Set[GenericPlugin[T]] = field(default_factory=set)
tasks: List[Task[T]] = field(default_factory=list)
Expand All @@ -94,9 +95,7 @@ def require(self, spec: GenericPluginSpec[T]):

self.plugins.add(plugin)

task = Task(plugin, exception_fallthrough=self.exception_fallthrough)

if remaining_work := task.advance(self.ctx):
if remaining_work := Task(plugin).advance(self.ctx):
self.tasks.append(remaining_work)

def resolve(self, spec: GenericPluginSpec[T]) -> GenericPlugin[T]:
Expand All @@ -107,7 +106,7 @@ def resolve(self, spec: GenericPluginSpec[T]) -> GenericPlugin[T]:
if isinstance(spec, str)
else spec
)
except (PluginError,) + self.exception_fallthrough:
except PipelineFallthroughException:
raise
except Exception as exc:
raise PluginImportError(spec) from exc
Expand Down
27 changes: 6 additions & 21 deletions beet/toolchain/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,14 @@
from beet.core.utils import FileSystemPath, JsonDict, intersperse
from beet.core.watch import DirectoryWatcher, FileChanges

from .config import (
InvalidProjectConfig,
PackConfig,
ProjectConfig,
load_config,
locate_config,
)
from .config import PackConfig, ProjectConfig, load_config, locate_config
from .context import Context, Pipeline, Plugin
from .pipeline import PluginError
from .template import TemplateError, TemplateManager
from .pipeline import PipelineFallthroughException
from .template import TemplateManager
from .utils import locate_minecraft


class ErrorMessage(Exception):
class ErrorMessage(PipelineFallthroughException):
"""Exception used to display nice error messages when something goes wrong."""


Expand Down Expand Up @@ -218,17 +212,8 @@ def build(self) -> Context:
)

with ctx, ctx.cache:
pipeline = ctx.inject(Pipeline)
pipeline.exception_fallthrough = (
ErrorMessage,
InvalidProjectConfig,
PluginError,
TemplateError,
)

pipeline.require(self.bootstrap)

pipeline.run(
ctx.require(self.bootstrap)
ctx.inject(Pipeline).run(
(
item
if isinstance(item, str)
Expand Down
4 changes: 3 additions & 1 deletion beet/toolchain/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
from beet.core.file import TextFileBase
from beet.core.utils import FileSystemPath

from .pipeline import PipelineFallthroughException

TextFileType = TypeVar("TextFileType", bound=TextFileBase[Any])


class TemplateError(Exception):
class TemplateError(PipelineFallthroughException):
"""Raised when an error occurs during template rendering."""


Expand Down

0 comments on commit 35ace13

Please sign in to comment.