Control socket: minor redo exceptions

このコミットが含まれているのは:
n9k 2022-06-16 01:05:18 +00:00
コミット 309b2ad54f
7個のファイルの変更26行の追加23行の削除

ファイルの表示

@ -1,5 +1,5 @@
class Exit(Exception):
class ControlSocketExit(Exception):
pass
class Fail(Exception):
class CommandFailed(Exception):
pass

ファイルの表示

@ -1,4 +1,4 @@
from anonstream.control.spec import NoParse, Ambiguous, Parsed
from anonstream.control.spec import ParseException, Parsed
from anonstream.control.spec.common import Str
from anonstream.control.spec.methods.chat import SPEC as SPEC_CHAT
from anonstream.control.spec.methods.exit import SPEC as SPEC_EXIT
@ -25,10 +25,7 @@ async def parse(request):
while True:
try:
spec, n_consumed, more_args = spec.consume(words, index)
except NoParse as e:
normal, response = None, e.args[0] + '\n'
break
except Ambiguous as e:
except ParseException as e:
normal, response = None, e.args[0] + '\n'
break
except Parsed as e:

ファイルの表示

@ -1,6 +1,6 @@
import asyncio
from anonstream.control.exceptions import Exit, Fail
from anonstream.control.exceptions import ControlSocketExit, CommandFailed
from anonstream.control.parse import parse
def start_control_server_at(address):
@ -15,9 +15,9 @@ async def serve_control_client(reader, writer):
else:
try:
normal, response = await parse(request)
except Fail as e:
except CommandFailed as e:
normal, response = None, e.args[0] + '\n'
except Exit:
except ControlSocketExit:
writer.close()
break

ファイルの表示

@ -1,7 +1,13 @@
class NoParse(Exception):
class ParseException(Exception):
pass
class Ambiguous(Exception):
class NoParse(ParseException):
pass
class Ambiguous(ParseException):
pass
class BadArgument(ParseException):
pass
class Parsed(Exception):

ファイルの表示

@ -1,8 +1,8 @@
from anonstream.control.spec.common import Str, End
from anonstream.control.exceptions import Exit
from anonstream.control.exceptions import ControlSocketExit
async def cmd_exit():
raise Exit
raise ControlSocketExit
async def cmd_exit_help():
normal = ['exit', 'help']

ファイルの表示

@ -1,6 +1,6 @@
import json
from anonstream.control.exceptions import Fail
from anonstream.control.exceptions import CommandFailed
from anonstream.control.spec import Spec, NoParse
from anonstream.control.spec.common import Str, End, ArgsJsonString
from anonstream.control.spec.utils import get_item, json_dumps_contiguous
@ -27,7 +27,7 @@ async def cmd_title_set(title):
try:
await set_stream_title(title)
except OSError as e:
raise Fail(f'could not set title: {e}') from e
raise CommandFailed(f'could not set title: {e}') from e
normal = ['title', 'set', json_dumps_contiguous(title)]
response = ''
return normal, response

ファイルの表示

@ -2,8 +2,8 @@ import json
from quart import current_app
from anonstream.control.exceptions import Fail
from anonstream.control.spec import NoParse
from anonstream.control.exceptions import CommandFailed
from anonstream.control.spec import BadArgument
from anonstream.control.spec.common import Str, End, ArgsInt, ArgsString, ArgsJson, ArgsJsonString
from anonstream.control.spec.utils import get_item, json_dumps_contiguous
from anonstream.utils.user import USER_WEBSOCKET_ATTRS
@ -17,7 +17,7 @@ class ArgsJsonTokenUser(ArgsJsonString):
try:
user = USERS_BY_TOKEN[token]
except KeyError:
raise NoParse(f'no user with token {token!r}')
raise BadArgument(f'no user with token {token!r}')
return user
class ArgsJsonHashUser(ArgsString):
@ -26,7 +26,7 @@ class ArgsJsonHashUser(ArgsString):
if user['token_hash'] == token_hash:
break
else:
raise NoParse(f'no user with token_hash {token_hash!r}')
raise BadArgument(f'no user with token_hash {token_hash!r}')
return user
def ArgsUser(spec):
@ -69,11 +69,11 @@ async def cmd_user_get(user, attr):
try:
value = user[attr]
except KeyError as e:
raise Fail('user has no such attribute') from e
raise CommandFailed('user has no such attribute') from e
try:
value_json = json.dumps(value)
except (TypeError, ValueError) as e:
raise Fail('value is not representable in json') from e
raise CommandFailed('value is not representable in json') from e
normal = [
'user',
'get',
@ -86,7 +86,7 @@ async def cmd_user_get(user, attr):
async def cmd_user_set(user, attr, value):
if attr not in user:
raise Fail(f'user has no attribute {attr!r}')
raise CommandFailed(f'user has no attribute {attr!r}')
user[attr] = value
if attr in USER_WEBSOCKET_ATTRS:
USERS_UPDATE_BUFFER.add(user['token'])