Skip to content

Commit

Permalink
refactor: incorrect exception usage (#2147)
Browse files Browse the repository at this point in the history
  • Loading branch information
bsherifi committed Mar 17, 2021
1 parent c278ba1 commit 1bb8f2b
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 10 deletions.
4 changes: 2 additions & 2 deletions jina/clients/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .helper import callback_exec
from .request import GeneratorSourceType
from ..enums import RequestType
from ..excepts import BadClient, BadClientInput
from ..excepts import BadClient, BadClientInput, ValidationError
from ..helper import typename
from ..logging import default_logger, JinaLogger
from ..logging.profile import TimeContext, ProgressBar
Expand Down Expand Up @@ -84,7 +84,7 @@ def check_input(inputs: Optional[InputType] = None, **kwargs) -> None:
kwargs['data'] = inputs

if inspect.isasyncgenfunction(inputs) or inspect.isasyncgen(inputs):
raise NotImplementedError(
raise ValidationError(
'checking the validity of an async generator is not implemented yet'
)

Expand Down
4 changes: 2 additions & 2 deletions jina/clients/request/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from ... import Document, Request
from ...enums import DataInputType, RequestType
from ...excepts import BadDocType, BadRequestType
from ...excepts import BadDocType, BadRequestType, RequestTypeError


def _new_doc_from_data(
Expand Down Expand Up @@ -47,7 +47,7 @@ def _new_request_from_batch(_kwargs, batch, data_type, mode, queryset):
elif mode == RequestType.DELETE:
_add_ids(req, batch)
else:
raise NotImplementedError(
raise RequestTypeError(
f'generating request from {mode} is not yet supported'
)
except Exception as ex:
Expand Down
16 changes: 16 additions & 0 deletions jina/excepts.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,19 @@ class DaemonConnectivityError(Exception):

class NotSupportedError(Exception):
"""Exeception when user accidentally using a retired argument."""


class RequestTypeError(Exception):
"""Raised when such request type does not exist."""


class ValidationError(Exception):
"""Raised when a certain validation cannot be completed."""


class MetricTypeError(Exception):
"""Raised when such metric type does not exist."""


class SocketTypeError(Exception):
"""Raised when such socket type is not supported or does not exist."""
2 changes: 0 additions & 2 deletions jina/executors/indexers/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,6 @@ def query(
elif self.metric == 'cosine':
_query_vectors = _ext_A(_norm(vectors))
dist = self._cosine(_query_vectors, self.query_handler)
else:
raise NotImplementedError(f'{self.metric} is not implemented')

idx, dist = self._get_sorted_top_k(dist, top_k)
indices = self._int2ext_id[self.valid_indices][idx]
Expand Down
4 changes: 2 additions & 2 deletions jina/flow/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from .. import __default_host__
from ..enums import SocketType, FlowBuildLevel, PodRoleType
from ..excepts import FlowBuildLevelError
from ..excepts import FlowBuildLevelError, SocketTypeError
from ..peapods.pods import _fill_in_host

# noinspection PyUnreachableCode
Expand Down Expand Up @@ -153,4 +153,4 @@ def _connect(
) # the hostname of s_pod
second.head_args.port_in = first.tail_args.port_out
else:
raise NotImplementedError(f'{first_socket_type!r} is not supported here')
raise SocketTypeError(f'{first_socket_type!r} is not supported here')
4 changes: 3 additions & 1 deletion jina/peapods/peas/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ def _get_event(obj) -> Event:
elif isinstance(obj, multiprocessing.Process):
return multiprocessing.Event()
else:
raise NotImplementedError
raise TypeError(
f'{obj} is not an instance of "threading.Thread" nor "multiprocessing.Process"'
)


def _make_or_event(obj, *events) -> Event:
Expand Down
5 changes: 4 additions & 1 deletion jina/peapods/pods/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ def _set_peas_args(
elif args.scheduling == SchedulerType.LOAD_BALANCE:
_args.socket_in = SocketType.DEALER_CONNECT
else:
raise NotImplementedError
raise ValueError(
f'{args.scheduling} is not supported as a SchedulerType!'
)

else:
_args.socket_in = SocketType.SUB_CONNECT
if head_args:
Expand Down

0 comments on commit 1bb8f2b

Please sign in to comment.