Skip to content

Commit 12a6000

Browse files
committed
Added logging to 'murfey.util.api'
1 parent cb499fb commit 12a6000

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/murfey/util/api.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010

1111
import re
1212
from functools import lru_cache
13+
from logging import getLogger
1314
from pathlib import Path
1415
from typing import Any
1516

1617
import yaml
1718

1819
import murfey.util
1920

21+
logger = getLogger("murfey.util.api")
22+
2023
route_manifest_file = Path(murfey.util.__path__[0]) / "route_manifest.yaml"
2124

2225

@@ -44,9 +47,13 @@ def find_unique_index(
4447
matches.append(candidate)
4548
index = i
4649
if counter == 0:
47-
raise KeyError(f"No match found for {pattern!r}")
50+
message = f"No match found for {pattern!r}"
51+
logger.error(message)
52+
raise KeyError(message)
4853
if counter > 1:
49-
raise KeyError(f"Ambiguous match for {pattern!r}: {matches}")
54+
message = f"Ambiguous match for {pattern!r}: {matches}"
55+
logger.error(message)
56+
raise KeyError(message)
5057
return index
5158

5259

@@ -62,7 +69,9 @@ def replace(match):
6269
raw_str = match.group(1)
6370
param_name = raw_str.split(":")[0] # Ignore :converter in the field
6471
if param_name not in kwargs:
65-
raise KeyError(f"Missing path parameter: {param_name}")
72+
message = f"Missing path parameter: {param_name}"
73+
logger.error(message)
74+
raise KeyError(message)
6675
return str(kwargs[param_name])
6776

6877
return pattern.sub(replace, path_template)
@@ -100,16 +109,20 @@ def url_path_for(
100109
for param, value in kwargs.items():
101110
# Check if the name is not a match
102111
if param not in [p["name"] for p in path_params] and path_params:
103-
raise KeyError(f"Unknown path parameter provided: {param}")
112+
message = f"Unknown path parameter provided: {param}"
113+
logger.error(message)
114+
raise KeyError(message)
104115
for path_param in path_params:
105116
if (
106117
path_param["name"] == param
107118
and type(value).__name__ != path_param["type"]
108119
):
109-
raise TypeError(
120+
message = (
110121
f"'{param}' must be {path_param['type']!r}; "
111122
f"received {type(value).__name__!r}"
112123
)
124+
logger.error(message)
125+
raise TypeError(message)
113126

114127
# Render and return the path
115128
return render_path(route_path, kwargs)
@@ -118,8 +131,7 @@ def url_path_for(
118131
if __name__ == "__main__":
119132
# Run test on some existing routes
120133
url_path = url_path_for(
121-
"api.router",
122-
"register_processing_parameters",
123-
session_id=3,
134+
"bootstrap.pypi",
135+
"get_pypi_index",
124136
)
125137
print(url_path)

0 commit comments

Comments
 (0)