Skip to content

Commit

Permalink
fix: Adapt to Stan change: consistent error logging
Browse files Browse the repository at this point in the history
Commit 5f4f01f74ada379e698e95784495c9d2a6853918 in stan-dev/stan makes
the following change:

> Currently, the services functions are inconsistent on whether or not
> they will throw an exception. The versions which accept num_chains
> should never throw, but the older ones will throw if initialization
> fails. This consistently puts utils::initialize in a try block.

> Additionally, we are currently using logger.info for messages which
> are errors, and in several places not logging an error where we could
> (e.g., the catch blocks accompanying the above trys).

This commit updates httpstan code to adapt to this change.
  • Loading branch information
riddell-stan committed Sep 14, 2023
1 parent c78cc53 commit d21ffb6
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions httpstan/services_stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,28 +157,28 @@ async def call(
fh.close()
httpstan.cache.dump_fit(b"".join(compressed_parts), fit_name)

# if an exception has already occurred, grab relevant info messages, add as context
# if an exception has already occurred, grab relevant error messages, add as context
exception = future.exception()
if exception and len(exception.args) == 1:
import gzip
import json

original_exception_message = exception.args[0] # e.g., from ValueError("Initialization failed.")
info_messages_for_context = []
error_messages_for_context = []
num_context_messages = 4

jsonlines = gzip.decompress(b"".join(compressed_parts)).decode()
for line in jsonlines.split("\n")[:num_context_messages]:
try:
message = json.loads(line)
info_message = message["values"].pop().replace("info:", "")
info_messages_for_context.append(info_message.strip())
error_message = message["values"].pop().replace("error:", "")
error_messages_for_context.append(error_message.strip())
except json.JSONDecodeError:
pass
# add the info messages to the original exception message. For example,
# add the error messages to the original exception message. For example,
# ValueError("Initialization failed.") -> ValueError("Initialization failed. Rejecting initial value: Log probability ...")
if info_messages_for_context:
new_exception_message = f"{original_exception_message} {' '.join(info_messages_for_context)} ..."
if error_messages_for_context:
new_exception_message = f"{original_exception_message} {' '.join(error_messages_for_context)} ..."
exception.args = (new_exception_message,)

# `result()` method will raise exceptions, if any
Expand Down

0 comments on commit d21ffb6

Please sign in to comment.