1- from typing import Any , List
1+ from typing import Any , List , Tuple
22
33from servc .svc import ComponentType , Middleware
44from servc .svc .com .bus import BusComponent , OnConsuming
55from servc .svc .com .cache import CacheComponent
66from servc .svc .com .worker .hooks import evaluate_post_hooks , evaluate_pre_hooks
7- from servc .svc .com .worker .types import RESOLVER_CONTEXT , RESOLVER_MAPPING
7+ from servc .svc .com .worker .types import RESOLVER , RESOLVER_CONTEXT , RESOLVER_MAPPING
88from servc .svc .config import Config
99from servc .svc .io .input import InputType
1010from servc .svc .io .output import (
1111 InvalidInputsException ,
1212 MethodNotFoundException ,
1313 NoProcessingException ,
1414 NotAuthorizedException ,
15+ ResponseArtifact ,
1516 StatusCode ,
1617)
1718from servc .svc .io .response import getAnswerArtifact , getErrorArtifact
@@ -102,6 +103,35 @@ def connect(self):
102103 bindEventExchange = self ._bindToEventExchange ,
103104 )
104105
106+ def run_resolver (
107+ self , method : RESOLVER , context : RESOLVER_CONTEXT , args : Tuple [str , Any ]
108+ ) -> Tuple [StatusCode , ResponseArtifact | None ]:
109+ id , payload = args
110+ try :
111+ response = method (id , payload , context )
112+ return StatusCode .OK , getAnswerArtifact (id , response )
113+ except NotAuthorizedException as e :
114+ return StatusCode .NOT_AUTHORIZED , getErrorArtifact (
115+ id , str (e ), StatusCode .NOT_AUTHORIZED
116+ )
117+ except InvalidInputsException as e :
118+ return StatusCode .INVALID_INPUTS , getErrorArtifact (
119+ id , str (e ), StatusCode .INVALID_INPUTS
120+ )
121+ except NoProcessingException :
122+ return StatusCode .NO_PROCESSING , None
123+ except MethodNotFoundException as e :
124+ return StatusCode .METHOD_NOT_FOUND , getErrorArtifact (
125+ id , str (e ), StatusCode .METHOD_NOT_FOUND
126+ )
127+ except Exception as e :
128+ if self ._config .get (f"conf.{ self .name } .exiton5xx" ):
129+ print ("Exiting due to 5xx error" , e , flush = True )
130+ exit (1 )
131+ return StatusCode .SERVER_ERROR , getErrorArtifact (
132+ id , str (e ), StatusCode .SERVER_ERROR
133+ )
134+
105135 def inputProcessor (self , message : Any ) -> StatusCode :
106136 bus = self ._busClass (
107137 self ._config .get (f"conf.{ self ._bus .name } " ),
@@ -126,8 +156,14 @@ def inputProcessor(self, message: Any) -> StatusCode:
126156 return StatusCode .INVALID_INPUTS
127157 if message ["event" ] not in self ._eventResolvers :
128158 return StatusCode .METHOD_NOT_FOUND
129- self ._eventResolvers [message ["event" ]]("" , {** message }, context )
130- return StatusCode .OK
159+
160+ status_code , response = self .run_resolver (
161+ self ._eventResolvers [message ["event" ]],
162+ context ,
163+ ("" , {** message }),
164+ )
165+
166+ return status_code
131167
132168 if message ["type" ] in [InputType .INPUT .value , InputType .INPUT ]:
133169 if "id" not in message :
@@ -177,45 +213,17 @@ def inputProcessor(self, message: Any) -> StatusCode:
177213 if not continueExecution :
178214 return StatusCode .OK
179215
180- statusCode : StatusCode = StatusCode .OK
181- try :
182- response = self ._resolvers [artifact ["method" ]](
183- message ["id" ],
184- artifact ["inputs" ],
185- context ,
186- )
187- cache .setKey (message ["id" ], getAnswerArtifact (message ["id" ], response ))
188- except NotAuthorizedException as e :
189- cache .setKey (
190- message ["id" ],
191- getErrorArtifact (message ["id" ], str (e ), StatusCode .NOT_AUTHORIZED ),
192- )
193- statusCode = StatusCode .NOT_AUTHORIZED
194- except InvalidInputsException as e :
195- cache .setKey (
196- message ["id" ],
197- getErrorArtifact (message ["id" ], str (e ), StatusCode .INVALID_INPUTS ),
198- )
199- statusCode = StatusCode .INVALID_INPUTS
200- except NoProcessingException :
216+ statusCode , response = self .run_resolver (
217+ self ._resolvers [artifact ["method" ]],
218+ context ,
219+ (message ["id" ], artifact ["inputs" ]),
220+ )
221+ if statusCode == StatusCode .NO_PROCESSING :
201222 return StatusCode .NO_PROCESSING
202- except MethodNotFoundException as e :
203- cache .setKey (
204- message ["id" ],
205- getErrorArtifact (
206- message ["id" ], str (e ), StatusCode .METHOD_NOT_FOUND
207- ),
208- )
209- statusCode = StatusCode .METHOD_NOT_FOUND
210- except Exception as e :
211- cache .setKey (
212- message ["id" ],
213- getErrorArtifact (message ["id" ], str (e ), StatusCode .SERVER_ERROR ),
214- )
215- statusCode = StatusCode .SERVER_ERROR
216- finally :
217- evaluate_post_hooks (bus , cache , message , artifact )
218- return statusCode
223+
224+ cache .setKey (message ["id" ], response )
225+ evaluate_post_hooks (bus , cache , message , artifact )
226+ return statusCode
219227
220228 cache .setKey (
221229 message ["id" ],
0 commit comments