Skip to content
This repository was archived by the owner on Mar 23, 2026. It is now read-only.

Commit fd07dd0

Browse files
Integrate translation within the handler chain
1 parent 65fdd7b commit fd07dd0

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

localstack-core/localstack/aws/handlers/logging.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Handlers for logging."""
22

3+
import contextlib
4+
import importlib
35
import logging
46
from functools import cached_property
57

@@ -21,14 +23,20 @@ class ExceptionLogger(ExceptionHandler):
2123
def __init__(self, logger=None):
2224
self.logger = logger or LOG
2325

26+
self._moto_service_exception = object
27+
with contextlib.suppress(ModuleNotFoundError, AttributeError):
28+
self._moto_service_exception = importlib.import_module(
29+
"moto.core.exceptions"
30+
).ServiceException
31+
2432
def __call__(
2533
self,
2634
chain: HandlerChain,
2735
exception: Exception,
2836
context: RequestContext,
2937
response: Response,
3038
):
31-
if isinstance(exception, ServiceException):
39+
if isinstance(exception, (ServiceException, self._moto_service_exception)):
3240
# We do not want to log an error/stacktrace if the handler is working as expected, but chooses to throw
3341
# a service exception
3442
return

localstack-core/localstack/aws/handlers/service.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""A set of common handlers to parse and route AWS service requests."""
22

3+
import importlib
34
import logging
45
import traceback
56
from collections import defaultdict
@@ -156,6 +157,14 @@ class ServiceExceptionSerializer(ExceptionHandler):
156157
def __init__(self):
157158
self.handle_internal_failures = True
158159

160+
self._moto_service_exception = object
161+
try:
162+
self._moto_service_exception = importlib.import_module(
163+
"moto.core.exceptions"
164+
).ServiceException
165+
except (ModuleNotFoundError, AttributeError) as exc:
166+
LOG.debug("Unable to set up Moto ServiceException translation: %s", exc)
167+
159168
def __call__(
160169
self,
161170
chain: HandlerChain,
@@ -183,6 +192,15 @@ def create_exception_response(self, exception: Exception, context: RequestContex
183192
LOG.info(message)
184193
context.service_exception = error
185194

195+
elif isinstance(exception, self._moto_service_exception):
196+
# Parse Moto ServiceException native if Moto is available.
197+
# This allows handler chain to gracefully handles Moto errors when provider handlers invoke Moto methods directly.
198+
# Moto may not be available in stripped-down versions of LocalStack, like LocalStack S3 image.
199+
error = CommonServiceException(
200+
code=exception.code,
201+
message=exception.message,
202+
)
203+
186204
elif not isinstance(exception, ServiceException):
187205
if not self.handle_internal_failures:
188206
return

0 commit comments

Comments
 (0)