Skip to content

Commit

Permalink
better error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
spyoungtech committed Aug 30, 2023
1 parent 09bc676 commit 3eba8b8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
25 changes: 21 additions & 4 deletions ahk/_async/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,13 @@ async def readline(self) -> bytes:
assert isinstance(line, bytes)
return line

async def read(self) -> bytes:
assert self._proc is not None
assert self._proc.stdout is not None
b = await self._proc.stdout.read()
assert isinstance(b, bytes)
return b

def kill(self) -> None:
assert self._proc is not None, 'no process to kill'
self._proc.kill()
Expand All @@ -280,12 +287,12 @@ def communicate(self, input_bytes: Optional[bytes] = None, timeout: Optional[int

async def async_create_process(runargs: List[str]) -> asyncio.subprocess.Process: # unasync: remove
return await asyncio.subprocess.create_subprocess_exec(
*runargs, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
*runargs, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)


def sync_create_process(runargs: List[str]) -> subprocess.Popen[bytes]:
return subprocess.Popen(runargs, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
return subprocess.Popen(runargs, stdin=subprocess.PIPE, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)


class AhkExecutableNotFoundError(EnvironmentError):
Expand Down Expand Up @@ -775,8 +782,13 @@ async def _send_nonblocking(
try:
lines_to_read = int(num_lines) + 1
except ValueError as e:
try:
stdout = tom + num_lines + await proc.read()
except Exception:
stdout = b''
raise AHKProtocolError(
'Unexpected data received. This is usually the result of an unhandled error in the AHK process.'
'Unexpected data received. This is usually the result of an unhandled error in the AHK process'
+ (f': {stdout!r}' if stdout else '')
) from e
for _ in range(lines_to_read):
part = await proc.readline()
Expand Down Expand Up @@ -827,8 +839,13 @@ async def send(
try:
lines_to_read = int(num_lines) + 1
except ValueError as e:
try:
stdout = tom + num_lines + await self._proc.read()
except Exception:
stdout = b''
raise AHKProtocolError(
'Unexpected data received. This is usually the result of an unhandled error in the AHK process.'
'Unexpected data received. This is usually the result of an unhandled error in the AHK process'
+ (f': {stdout!r}' if stdout else '')
) from e
for _ in range(lines_to_read):
part = await self._proc.readline()
Expand Down
23 changes: 20 additions & 3 deletions ahk/_sync/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@ def readline(self) -> bytes:
assert isinstance(line, bytes)
return line

def read(self) -> bytes:
assert self._proc is not None
assert self._proc.stdout is not None
b = self._proc.stdout.read()
assert isinstance(b, bytes)
return b

def kill(self) -> None:
assert self._proc is not None, 'no process to kill'
self._proc.kill()
Expand All @@ -266,7 +273,7 @@ def communicate(self, input_bytes: Optional[bytes] = None, timeout: Optional[int


def sync_create_process(runargs: List[str]) -> subprocess.Popen[bytes]:
return subprocess.Popen(runargs, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
return subprocess.Popen(runargs, stdin=subprocess.PIPE, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)


class AhkExecutableNotFoundError(EnvironmentError):
Expand Down Expand Up @@ -747,8 +754,13 @@ def _send_nonblocking(
try:
lines_to_read = int(num_lines) + 1
except ValueError as e:
try:
stdout = tom + num_lines + proc.read()
except Exception:
stdout = b''
raise AHKProtocolError(
'Unexpected data received. This is usually the result of an unhandled error in the AHK process.'
'Unexpected data received. This is usually the result of an unhandled error in the AHK process'
+ (f': {stdout!r}' if stdout else '')
) from e
for _ in range(lines_to_read):
part = proc.readline()
Expand Down Expand Up @@ -791,8 +803,13 @@ def send(
try:
lines_to_read = int(num_lines) + 1
except ValueError as e:
try:
stdout = tom + num_lines + self._proc.read()
except Exception:
stdout = b''
raise AHKProtocolError(
'Unexpected data received. This is usually the result of an unhandled error in the AHK process.'
'Unexpected data received. This is usually the result of an unhandled error in the AHK process'
+ (f': {stdout!r}' if stdout else '')
) from e
for _ in range(lines_to_read):
part = self._proc.readline()
Expand Down

0 comments on commit 3eba8b8

Please sign in to comment.