-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Error reports include line and column number, if available (#141)
- Loading branch information
Showing
4 changed files
with
202 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
"""ContextManager to help with error reporting in miniWDL parsed files.""" | ||
|
||
import re | ||
from types import TracebackType | ||
from typing import Any, Callable, Optional, Type, cast | ||
|
||
import WDL | ||
from WDL._error_util import SourcePosition | ||
|
||
# Inspired by https://github.com/common-workflow-language/schema_salad/blob/661fb0fa8c745ed70253dda93bd12002007f6b33/schema_salad/sourceline.py#L232 | ||
|
||
|
||
lineno_re = re.compile("^(.*?:[0-9]+:[0-9]+: )(( *)(.*))") | ||
|
||
|
||
class WDLSourceLine: | ||
"""Contextmanager wrap exceptions with WDL source file locations.""" | ||
|
||
def __init__( | ||
self, | ||
item: Any, | ||
raise_type: Callable[[str], Any] = str, | ||
): | ||
"""Which item and exception type to raise.""" | ||
self.item = item | ||
self.raise_type = raise_type | ||
|
||
def __enter__(self) -> "WDLSourceLine": | ||
"""Enter the context.""" | ||
return self | ||
|
||
def __exit__( | ||
self, | ||
exc_type: Optional[Type[BaseException]], | ||
exc_value: Optional[BaseException], | ||
exc_tb: Optional[TracebackType], | ||
) -> None: | ||
""" | ||
Process the exit from the context. | ||
If there was an exception, wrap it in the raise_type. | ||
""" | ||
if not exc_value: | ||
return | ||
raise self.makeError(str(exc_value)) from exc_value | ||
|
||
def makeLead(self) -> str: | ||
"""Caculate the error message prefix.""" | ||
pos: SourcePosition = cast(SourcePosition, self.item.pos) | ||
return f"{pos.uri}:{pos.line}:{pos.column}:" | ||
|
||
def makeError(self, msg: str) -> Any: | ||
"""Add the source info to the msg and instantiate the raise_type with it.""" | ||
if not isinstance( | ||
self.item, | ||
( | ||
WDL.Error.SourceNode, | ||
WDL.Tree.SourceComment, | ||
WDL.Tree.DocImport, | ||
WDL.Type.Base, | ||
), | ||
): | ||
return self.raise_type(msg) | ||
errs = [] | ||
lead = self.makeLead() | ||
for m in msg.splitlines(): | ||
if bool(lineno_re.match(m)): | ||
errs.append(m) | ||
else: | ||
errs.append(f"{lead} {m}") | ||
return self.raise_type("\n".join(errs)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.