11import asyncio
22from nitric .faas import FunctionServer
3+ from nitric .api .exception import NitricUnavailableException
34
45# from nitric.resources.base import BaseResource
56from 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 ("\n exiting" )
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