Skip to content

Commit a9fd145

Browse files
authored
Merge pull request #61 from serv-c/exceptions
feat(worker): adding in exit for 4xx
2 parents 45e638f + cd9b7c6 commit a9fd145

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

servc/svc/com/worker/__init__.py

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -107,30 +107,43 @@ def run_resolver(
107107
self, method: RESOLVER, context: RESOLVER_CONTEXT, args: Tuple[str, Any]
108108
) -> Tuple[StatusCode, ResponseArtifact | None]:
109109
id, payload = args
110+
statuscode: StatusCode = StatusCode.OK
111+
response: ResponseArtifact | None = None
112+
error: Any = None
113+
110114
try:
111-
response = method(id, payload, context)
112-
return StatusCode.OK, getAnswerArtifact(id, response)
115+
response = getAnswerArtifact(id, method(id, payload, context))
113116
except NotAuthorizedException as e:
114-
return StatusCode.NOT_AUTHORIZED, getErrorArtifact(
115-
id, str(e), StatusCode.NOT_AUTHORIZED
116-
)
117+
error = e
118+
statuscode = StatusCode.NOT_AUTHORIZED
119+
response = getErrorArtifact(id, str(e), StatusCode.NOT_AUTHORIZED)
117120
except InvalidInputsException as e:
118-
return StatusCode.INVALID_INPUTS, getErrorArtifact(
119-
id, str(e), StatusCode.INVALID_INPUTS
120-
)
121+
error = e
122+
statuscode = StatusCode.INVALID_INPUTS
123+
response = getErrorArtifact(id, str(e), StatusCode.INVALID_INPUTS)
121124
except NoProcessingException:
122-
return StatusCode.NO_PROCESSING, None
125+
statuscode = StatusCode.NO_PROCESSING
123126
except MethodNotFoundException as e:
124-
return StatusCode.METHOD_NOT_FOUND, getErrorArtifact(
125-
id, str(e), StatusCode.METHOD_NOT_FOUND
126-
)
127+
error = e
128+
statuscode = StatusCode.METHOD_NOT_FOUND
129+
response = getErrorArtifact(id, str(e), StatusCode.METHOD_NOT_FOUND)
127130
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-
)
131+
error = e
132+
statuscode = StatusCode.SERVER_ERROR
133+
response = getErrorArtifact(id, str(e), StatusCode.SERVER_ERROR)
134+
135+
if self._config.get(f"conf.{self.name}.exiton5xx") and statuscode.value >= 500:
136+
print("Exiting due to 5xx error", error, flush=True)
137+
exit(1)
138+
if (
139+
self._config.get(f"conf.{self.name}.exiton4xx")
140+
and statuscode.value >= 400
141+
and statuscode.value < 500
142+
):
143+
print("Exiting due to 5xx error", error, flush=True)
144+
exit(1)
145+
146+
return statuscode, response
134147

135148
def inputProcessor(self, message: Any) -> StatusCode:
136149
bus = self._busClass(

servc/svc/config/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
"conf.bus.prefix": "",
2020
"conf.worker.bindtoeventexchange": True,
2121
"conf.worker.exiton5xx": True,
22+
"conf.worker.exiton4xx": False,
2223
}
2324

2425
BOOLEAN_CONFIGS = os.getenv(
25-
"SERVC_BOOLEAN_CONFIGS", "conf.worker.exiton5xx,conf.worker.bindtoeventexchange"
26+
"SERVC_BOOLEAN_CONFIGS",
27+
"conf.worker.exiton4xx,conf.worker.exiton5xx,conf.worker.bindtoeventexchange",
2628
).split(",")
2729
DOT_MARKER = os.getenv("SERVC_DOT_MARKER", "_DOT_")
2830
DASH_MARKER = os.getenv("SERVC_DASH_MARKER", "_DASH_")

0 commit comments

Comments
 (0)