Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gracefully fail when log messages are not valid json #246 #253

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions awslogs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,11 @@ def consumer():

message = event['message']
if self.query is not None and message[0] == '{':
parsed = json.loads(event['message'])
message = self.query_expression.search(parsed)
try:
parsed = json.loads(event['message'])
message = self.query_expression.search(parsed)
except ValueError: # includes JSONDecodeError
raise exceptions.JsonFormattingOrDecodingError()
if not isinstance(message, six.string_types):
message = json.dumps(message)
output.append(message.rstrip())
Expand Down
8 changes: 8 additions & 0 deletions awslogs/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ class NoStreamsFilteredError(BaseAWSLogsException):

def hint(self):
return ("No streams match your pattern '{0}' for the given time period.").format(*self.args)


class JsonFormattingOrDecodingError(BaseAWSLogsException):

code = 8

def hint(self):
return ("Invalid JSON string or string could not be decoded")