Skip to content
This repository was archived by the owner on Sep 13, 2023. It is now read-only.

better errors #253

Merged
merged 1 commit into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions mlem/api/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,7 @@ def import_object(
echo(EMOJI_LOAD + f"Importing object from {loc.uri_repr}")
if type_ is not None:
type_, modifier = parse_import_type_modifier(type_)
if type_ not in ImportHook.__type_map__:
raise ValueError(f"Unknown import type {type_}")
meta = ImportHook.__type_map__[type_].process(
meta = ImportHook.load_type(type_).process(
loc, copy_data=copy_data, modifier=modifier
)
else:
Expand Down
2 changes: 2 additions & 0 deletions mlem/contrib/docker/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
create_docker_client,
image_exists_at_dockerhub,
print_docker_logs,
wrap_docker_error,
)
from mlem.core.base import MlemABC
from mlem.core.errors import DeploymentError
Expand Down Expand Up @@ -336,6 +337,7 @@ def package(self, obj: MlemModel) -> DockerImage:

return self.build(tempdir)

@wrap_docker_error
def build(self, context_dir: str) -> DockerImage:
tag = self.image.uri
logger.debug("Building docker image %s from %s...", tag, context_dir)
Expand Down
14 changes: 14 additions & 0 deletions mlem/contrib/docker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re
import time
from contextlib import contextmanager
from functools import wraps
from threading import Lock
from typing import Any, Iterator, Tuple, Union

Expand All @@ -13,6 +14,8 @@
from docker.models.images import Image
from docker.utils.json_stream import json_stream

from mlem.core.errors import MlemError

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -124,6 +127,17 @@ def repository_tags_at_dockerhub(repo):
return {tag["name"] for tag in resp.json()}


def wrap_docker_error(f):
@wraps(f)
def inner(*args, **kwargs):
try:
return f(*args, **kwargs)
except DockerException as e:
raise MlemError(f"Error calling docker: {e}") from e

return inner


# Copyright 2019 Zyfra
# Copyright 2021 Iterative
#
Expand Down
8 changes: 8 additions & 0 deletions mlem/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing_extensions import Literal
from yaml import safe_load

from mlem.core.errors import UnknownImplementation
from mlem.polydantic import PolyModel
from mlem.utils.importing import import_string
from mlem.utils.path import make_posix
Expand Down Expand Up @@ -102,6 +103,13 @@ def non_abstract_subtypes(cls: Type[MT]) -> Dict[str, Type["MT"]]:
and v is not cls
}

@classmethod
def load_type(cls, type_name: str):
try:
return cls.__resolve_subtype__(type_name)
except ValueError as e:
raise UnknownImplementation(type_name, cls.abs_name) from e


def set_or_replace(obj: dict, key: str, value: Any, subkey: str = "type"):
if key in obj:
Expand Down
7 changes: 7 additions & 0 deletions mlem/core/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,10 @@ def __init__(self, wrong, missing, fix):
super().__init__(
f"Wrong requirements: {self.wrong} {self.missing}\nTo fix it, run `{fix}`"
)


class UnknownImplementation(MlemError):
def __init__(self, type_name: str, abs_name: str):
self.abs_name = abs_name
self.type_name = type_name
super().__init__(f"Unknown {abs_name} implementation: {type_name}")