Skip to content

Commit a1cd7ff

Browse files
committed
minor refactor email
1 parent 44e0e4e commit a1cd7ff

File tree

3 files changed

+22
-82
lines changed

3 files changed

+22
-82
lines changed

.codecov.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ component_management:
2525
branches:
2626
- "!master"
2727
individual_components:
28-
- component_id: api
29-
paths:
30-
- api/**
3128
- component_id: pkg_aws_library
3229
paths:
3330
- packages/aws-library/**

services/web/server/src/simcore_service_webserver/email/_handlers.py

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,18 @@
55
from models_library.emails import LowerCaseEmailStr
66
from pydantic import BaseModel, Field
77
from servicelib.aiohttp.requests_validation import parse_request_body_as
8+
from servicelib.logging_errors import create_troubleshootting_log_kwargs
89

910
from .._meta import API_VTAG
1011
from ..login.decorators import login_required
1112
from ..products import products_web
1213
from ..products.models import Product
1314
from ..security.decorators import permission_required
14-
from ..utils import get_traceback_string
1515
from ..utils_aiohttp import envelope_json_response
1616
from ._core import check_email_server_responsiveness, send_email_from_template
1717
from .settings import get_plugin_settings
1818

19-
logger = logging.getLogger(__name__)
20-
21-
22-
#
23-
# API schema models
24-
#
19+
_logger = logging.getLogger(__name__)
2520

2621

2722
class TestEmail(BaseModel):
@@ -39,28 +34,15 @@ class TestEmail(BaseModel):
3934

4035
class EmailTestFailed(BaseModel):
4136
test_name: str
42-
error_type: str
43-
error_message: str
44-
traceback: str
45-
46-
@classmethod
47-
def create_from_exception(cls, error: Exception, test_name: str):
48-
return cls(
49-
test_name=test_name,
50-
error_type=f"{type(error)}",
51-
error_message=f"{error}",
52-
traceback=get_traceback_string(error),
53-
)
37+
error_code: str | None = None
38+
user_message: str = "Email test failed"
5439

5540

5641
class EmailTestPassed(BaseModel):
5742
fixtures: dict[str, Any]
5843
info: dict[str, Any]
5944

6045

61-
#
62-
# API routes
63-
#
6446
routes = web.RouteTableDef()
6547

6648

@@ -109,10 +91,24 @@ async def test_email(request: web.Request):
10991
)
11092

11193
except Exception as err: # pylint: disable=broad-except
112-
logger.exception(
113-
"test_email failed for %s",
114-
f"{settings.model_dump_json(indent=1)}",
94+
95+
_logger.exception(
96+
**create_troubleshootting_log_kwargs(
97+
user_error_msg="Email test failed",
98+
error=err,
99+
error_context={
100+
"template_name": body.template_name,
101+
"to": body.to,
102+
"from_": body.from_ or product.support_email,
103+
"settings": settings.model_dump(),
104+
},
105+
tip="Check SMTP settings and network connectivity",
106+
)
115107
)
116108
return envelope_json_response(
117-
EmailTestFailed.create_from_exception(error=err, test_name="test_email")
109+
EmailTestFailed(
110+
test_name="test_email",
111+
error_code=getattr(err, "error_code", None),
112+
user_message="Email test failed. Please check the logs for more details.",
113+
)
118114
)

services/web/server/src/simcore_service_webserver/utils.py

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,18 @@
33
"""
44

55
import asyncio
6-
import hashlib
76
import logging
87
import os
9-
import sys
10-
import traceback
118
import tracemalloc
129
from datetime import datetime
13-
from pathlib import Path
1410

1511
from common_library.error_codes import ErrorCodeStr
1612
from typing_extensions import ( # https://docs.pydantic.dev/latest/api/standard_library_types/#typeddict
1713
TypedDict,
1814
)
1915

20-
_CURRENT_DIR = (
21-
Path(sys.argv[0] if __name__ == "__main__" else __file__).resolve().parent
22-
)
2316
_logger = logging.getLogger(__name__)
2417

25-
26-
def is_osparc_repo_dir(path: Path) -> bool:
27-
return all(
28-
any(path.glob(expression)) for expression in [".github", "packages", "services"]
29-
)
30-
31-
32-
def search_osparc_repo_dir(max_iter=8):
33-
"""Returns path to root repo dir or None
34-
35-
NOTE: assumes this file within repo, i.e. only happens in edit mode!
36-
"""
37-
root_dir = _CURRENT_DIR
38-
if "services/web/server" in str(root_dir):
39-
it = 1
40-
while not is_osparc_repo_dir(root_dir) and it < max_iter:
41-
root_dir = root_dir.parent
42-
it += 1
43-
44-
if is_osparc_repo_dir(root_dir):
45-
return root_dir
46-
return None
47-
48-
49-
def gravatar_hash(email: str) -> str:
50-
return hashlib.md5(email.lower().encode("utf-8")).hexdigest() # nosec
51-
52-
53-
# -----------------------------------------------
54-
#
55-
# DATE/TIME
56-
#
57-
#
58-
5918
DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"
6019

6120
SECOND: int = 1
@@ -69,8 +28,6 @@ def now() -> datetime:
6928

7029

7130
def format_datetime(snapshot: datetime) -> str:
72-
# TODO: this fullfills datetime schema!!!
73-
7431
# FIXME: ensure snapshot is ZULU time!
7532
return "{}Z".format(snapshot.isoformat(timespec="milliseconds"))
7633

@@ -163,13 +120,3 @@ def compose_support_error_msg(
163120
)
164121

165122
return ". ".join(sentences)
166-
167-
168-
# -----------------------------------------------
169-
#
170-
# FORMATTING
171-
#
172-
173-
174-
def get_traceback_string(exception: BaseException) -> str:
175-
return "".join(traceback.format_exception(exception))

0 commit comments

Comments
 (0)