Skip to content

Commit 1dae3f2

Browse files
committed
Better error if plugin file in baseline not found
Fixes: #718
1 parent 44095a0 commit 1dae3f2

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

detect_secrets/core/plugins/initialize.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,17 @@ def from_plugin_classname(classname: str) -> Plugin:
3131
"""
3232
:raises: TypeError
3333
"""
34-
for plugin_type in get_mapping_from_secret_type_to_class().values():
34+
try:
35+
plugin_types = get_mapping_from_secret_type_to_class().values()
36+
except FileNotFoundError as e:
37+
log.error(f'Error: Failed to load `{classname}` plugin: {e}')
38+
log.error(
39+
'This error can occur when using a baseline that references a '
40+
'custom plugin with a path that does not exist.',
41+
)
42+
raise
43+
44+
for plugin_type in plugin_types:
3545
if plugin_type.__name__ == classname:
3646
break
3747
else:

detect_secrets/core/scan.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,12 @@ def scan_line(line: str) -> Generator[PotentialSecret, None, None]:
138138

139139

140140
def scan_file(filename: str) -> Generator[PotentialSecret, None, None]:
141-
if not get_plugins(): # pragma: no cover
142-
log.error('No plugins to scan with!')
141+
try:
142+
if not get_plugins(): # pragma: no cover
143+
log.error('No plugins to scan with!')
144+
return
145+
except FileNotFoundError:
146+
log.error('Unable to load plugins!')
143147
return
144148

145149
if _is_filtered_out(required_filter_parameters=['filename'], filename=filename):

detect_secrets/util/importlib.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import errno
12
import importlib.util
23
import os
34
import pkgutil
@@ -85,7 +86,8 @@ def import_file_as_module(filename: str, name: Optional[str] = None) -> ModuleTy
8586
for you.
8687
"""
8788
if not os.path.exists(filename):
88-
raise FileNotFoundError
89+
# Source: https://stackoverflow.com/a/36077407
90+
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), filename)
8991

9092
if not name:
9193
# NOTE: After several trial and error attempts, I could not discern the importance

0 commit comments

Comments
 (0)