Skip to content

Commit 8220fea

Browse files
committed
fix: Print readable error when cannot connect to a nitric instance.
1 parent 80614d0 commit 8220fea

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

nitric/api/exception.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,19 @@ class UnknownException(NitricServiceException):
141141

142142
pass
143143

144+
144145
class NitricResourceException(Exception):
146+
"""Illegal nitric resource creation."""
147+
145148
pass
146149

150+
151+
class NitricUnavailableException(Exception):
152+
"""Unable to connect to a nitric server."""
153+
154+
pass
155+
156+
147157
def exception_from_grpc_error(error: GRPCError):
148158
"""Translate a gRPC error to a nitric api exception."""
149159
return exception_from_grpc_code(error.status.value, error.message)

nitric/application.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22
from nitric.faas import FunctionServer
3+
from nitric.api.exception import NitricUnavailableException
34

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

2930
@classmethod
3031
def _create_resource(cls, resource: Type[BT], name: str, *args, **kwargs) -> BT:
31-
resource_type = resource.__name__.lower()
32-
if cls._cache.get(resource_type).get(name) is None:
33-
cls._cache[resource_type][name] = resource.make(name, *args, **kwargs)
32+
try:
33+
resource_type = resource.__name__.lower()
34+
if cls._cache.get(resource_type).get(name) is None:
35+
cls._cache[resource_type][name] = resource.make(name, *args, **kwargs)
3436

35-
return cls._cache[resource_type][name]
37+
return cls._cache[resource_type][name]
38+
except ConnectionRefusedError:
39+
raise NitricUnavailableException(
40+
'Unable to connect to a nitric server! If you\'re running locally make sure to run "nitric start"'
41+
)
3642

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

0 commit comments

Comments
 (0)