Skip to content

Commit

Permalink
Print readable error when cannot connect to a nitric instance. (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjholm authored Nov 24, 2022
2 parents 80614d0 + 8220fea commit 4476419
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
10 changes: 10 additions & 0 deletions nitric/api/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,19 @@ class UnknownException(NitricServiceException):

pass


class NitricResourceException(Exception):
"""Illegal nitric resource creation."""

pass


class NitricUnavailableException(Exception):
"""Unable to connect to a nitric server."""

pass


def exception_from_grpc_error(error: GRPCError):
"""Translate a gRPC error to a nitric api exception."""
return exception_from_grpc_code(error.status.value, error.message)
Expand Down
18 changes: 14 additions & 4 deletions nitric/application.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
from nitric.faas import FunctionServer
from nitric.api.exception import NitricUnavailableException

# from nitric.resources.base import BaseResource
from typing import Dict, List, Type, Any, TypeVar
Expand Down Expand Up @@ -28,11 +29,16 @@ def _register_worker(cls, srv: FunctionServer):

@classmethod
def _create_resource(cls, resource: Type[BT], name: str, *args, **kwargs) -> BT:
resource_type = resource.__name__.lower()
if cls._cache.get(resource_type).get(name) is None:
cls._cache[resource_type][name] = resource.make(name, *args, **kwargs)
try:
resource_type = resource.__name__.lower()
if cls._cache.get(resource_type).get(name) is None:
cls._cache[resource_type][name] = resource.make(name, *args, **kwargs)

return cls._cache[resource_type][name]
return cls._cache[resource_type][name]
except ConnectionRefusedError:
raise NitricUnavailableException(
'Unable to connect to a nitric server! If you\'re running locally make sure to run "nitric start"'
)

@classmethod
def run(cls):
Expand All @@ -50,3 +56,7 @@ def run(cls):
loop.run_until_complete(asyncio.gather(*[wkr.start() for wkr in cls._workers]))
except KeyboardInterrupt:
print("\nexiting")
except ConnectionRefusedError:
raise NitricUnavailableException(
'Unable to connect to a nitric server! If you\'re running locally make sure to run "nitric start"'
)

0 comments on commit 4476419

Please sign in to comment.