fix (Message): Harden parsing and display.
Significantly broaden error handling for initial parsing. Add error details upon parsing error. Add more error catchers to parser and formatter. Remove assumptions about data fields from format and output.
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import json
|
import json
|
||||||
import asyncio
|
import asyncio
|
||||||
import itertools
|
import itertools
|
||||||
|
import traceback
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
from enum import IntEnum
|
from enum import IntEnum
|
||||||
from typing import Any, Optional
|
from typing import Any, Optional
|
||||||
@@ -753,11 +754,19 @@ class Message(SettingType):
|
|||||||
if as_json:
|
if as_json:
|
||||||
try:
|
try:
|
||||||
args = json.loads(userstr)
|
args = json.loads(userstr)
|
||||||
except json.JSONDecodeError:
|
if not isinstance(args, dict) or (not args.get('content', None) and not args.get('embed', None)):
|
||||||
|
raise ValueError("At least one of the 'content' or 'embed' data fields are required.")
|
||||||
|
if 'embed' in args:
|
||||||
|
discord.Embed.from_dict(
|
||||||
|
args['embed']
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
only_error = "".join(traceback.TracebackException.from_exception(e).format_exception_only())
|
||||||
raise UserInputError(
|
raise UserInputError(
|
||||||
"Couldn't parse your message! "
|
"Couldn't parse your message! "
|
||||||
"You can test and fix it on the embed builder "
|
"You can test and fix it on the embed builder "
|
||||||
"[here](https://glitchii.github.io/embedbuilder/?editor=json)."
|
"[here](https://glitchii.github.io/embedbuilder/?editor=json).\n"
|
||||||
|
"```{}```".format(only_error)
|
||||||
)
|
)
|
||||||
if 'embed' in args and 'timestamp' in args['embed']:
|
if 'embed' in args and 'timestamp' in args['embed']:
|
||||||
args['embed'].pop('timestamp')
|
args['embed'].pop('timestamp')
|
||||||
@@ -770,6 +779,8 @@ class Message(SettingType):
|
|||||||
if data is None:
|
if data is None:
|
||||||
return "Empty"
|
return "Empty"
|
||||||
value = cls._data_to_value(id, data, **kwargs)
|
value = cls._data_to_value(id, data, **kwargs)
|
||||||
|
if 'embed' not in value and 'content' not in value:
|
||||||
|
return "Invalid"
|
||||||
if 'embed' not in value and len(value['content']) < 100:
|
if 'embed' not in value and len(value['content']) < 100:
|
||||||
return "`{}`".format(value['content'])
|
return "`{}`".format(value['content'])
|
||||||
else:
|
else:
|
||||||
@@ -788,9 +799,9 @@ class Message(SettingType):
|
|||||||
value = self.value
|
value = self.value
|
||||||
substitutions = self.substitution_keys(ctx, **kwargs)
|
substitutions = self.substitution_keys(ctx, **kwargs)
|
||||||
args = {}
|
args = {}
|
||||||
if 'content' in value:
|
if value.get('content', None):
|
||||||
args['content'] = multiple_replace(value['content'], substitutions)
|
args['content'] = multiple_replace(value['content'], substitutions)
|
||||||
if 'embed' in value:
|
if value.get('embed', None):
|
||||||
args['embed'] = discord.Embed.from_dict(
|
args['embed'] = discord.Embed.from_dict(
|
||||||
json.loads(multiple_replace(json.dumps(value['embed']), substitutions))
|
json.loads(multiple_replace(json.dumps(value['embed']), substitutions))
|
||||||
)
|
)
|
||||||
@@ -800,7 +811,7 @@ class Message(SettingType):
|
|||||||
value = self.value
|
value = self.value
|
||||||
args = self.args(ctx, **kwargs)
|
args = self.args(ctx, **kwargs)
|
||||||
|
|
||||||
if not value:
|
if not value or not args:
|
||||||
return await ctx.reply(embed=self.embed)
|
return await ctx.reply(embed=self.embed)
|
||||||
|
|
||||||
current_str = None
|
current_str = None
|
||||||
|
|||||||
Reference in New Issue
Block a user