Skip to content

Commit c8ccba9

Browse files
committed
adds troubleshooting for 500
1 parent 253e9fd commit c8ccba9

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

packages/service-library/src/servicelib/aiohttp/rest_middlewares.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
from aiohttp import web
1313
from aiohttp.web_request import Request
1414
from aiohttp.web_response import StreamResponse
15+
from models_library.errors_classes import OsparcErrorMixin
1516
from models_library.utils.json_serialization import json_dumps
17+
from servicelib.error_codes import create_error_code
1618

19+
from ..logging_utils import create_troubleshotting_log_message, get_log_record_extra
1720
from ..mimetype_constants import MIMETYPE_APPLICATION_JSON
1821
from ..utils import is_production_environ
1922
from .rest_models import ErrorItemType, ErrorType, LogMessageType
@@ -28,6 +31,7 @@
2831
from .typing_extension import Handler, Middleware
2932

3033
DEFAULT_API_VERSION = "v0"
34+
FMSG_INTERNAL_ERROR_USER_FRIENDLY = "Oops! Something went wrong, but we've noted it down and we'll sort it out ASAP. Thanks for your patience! [{}]"
3135

3236

3337
_logger = logging.getLogger(__name__)
@@ -40,29 +44,44 @@ def is_api_request(request: web.Request, api_version: str) -> bool:
4044

4145
def error_middleware_factory(
4246
api_version: str,
43-
log_exceptions: bool = True,
4447
) -> Middleware:
4548
_is_prod: bool = is_production_environ()
4649

4750
def _process_and_raise_unexpected_error(request: web.BaseRequest, err: Exception):
51+
52+
error_code = create_error_code(err)
53+
error_context = {
54+
"request.remote",
55+
request.remote,
56+
"request.method",
57+
request.method,
58+
"request.path",
59+
request.path,
60+
}
61+
if isinstance(err, OsparcErrorMixin):
62+
error_context.update(err.error_context())
63+
64+
frontend_msg = FMSG_INTERNAL_ERROR_USER_FRIENDLY.format(error_code)
65+
log_msg = create_troubleshotting_log_message(
66+
message_to_user=frontend_msg,
67+
error=err,
68+
error_code=error_code,
69+
error_context=error_context,
70+
)
71+
4872
http_error = create_http_error(
4973
err,
50-
"Unexpected Server error",
74+
frontend_msg,
5175
web.HTTPInternalServerError,
5276
skip_internal_error_details=_is_prod,
5377
)
54-
55-
if log_exceptions:
56-
_logger.error(
57-
'Unexpected server error "%s" from access: %s "%s %s". Responding with status %s',
58-
type(err),
59-
request.remote,
60-
request.method,
61-
request.path,
62-
http_error.status,
63-
exc_info=err,
64-
stack_info=True,
65-
)
78+
_logger.exception(
79+
log_msg,
80+
extra=get_log_record_extra(
81+
error_code=error_code,
82+
user_id=error_context.get("user_id", None),
83+
),
84+
)
6685
raise http_error
6786

6887
@web.middleware

services/web/server/src/simcore_service_webserver/rest/plugin.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ def setup_rest(app: web.Application):
5454
# NOTE: using safe get here since some tests use incomplete configs
5555
app.middlewares.extend(
5656
[
57-
error_middleware_factory(
58-
api_version=API_VTAG,
59-
log_exceptions=not is_diagnostics_enabled,
60-
),
57+
error_middleware_factory(api_version=API_VTAG),
6158
envelope_middleware_factory(api_version=API_VTAG),
6259
]
6360
)

0 commit comments

Comments
 (0)