Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdc2bf52

Browse files
authored
Merge pull requestalexmojaki#10 from alexmojaki/errors
Refactoring worker, showing internal errors
2 parentsdaa0881 +ee8cf1c commitdc2bf52

File tree

19 files changed

+596
-470
lines changed

19 files changed

+596
-470
lines changed

‎backend/book/settings.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"""
1212

1313
importos
14+
importsys
1415
frompathlibimportPath
1516

1617
importbirdseye
@@ -27,7 +28,7 @@
2728

2829
DEBUG=os.environ.get('DEBUG','True')[0].upper()=='T'
2930

30-
snoop.install(enabled=DEBUG)
31+
snoop.install(enabled=DEBUG,out=sys.__stderr__,columns=['thread'])
3132

3233
GITHUB_TOKEN=os.environ.get('GITHUB_TOKEN')
3334

‎backend/main/exercises.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def check_result(func, inputs, expected_result):
117117
try:
118118
result=func(**inputs)
119119
exceptExceptionase:
120-
result=format_exception_string(e)
120+
result=format_exception_string()
121121

122122
result=clean_result(result)
123123
expected_result=clean_result(expected_result)

‎backend/main/tests.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
frommain.chapters.c05_if_statementsimportUnderstandingProgramsWithSnoop
88
frommain.textimportpages
9-
frommainimportworker
9+
frommain.workersimportmaster
1010

1111
client=Client()
12-
worker.TESTING=True
12+
master.TESTING=True
1313

1414

1515
defapi(method,**kwargs):

‎backend/main/utils/__init__.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,12 @@ def unwrapped_markdown(s):
5454
returns
5555

5656

57-
defformat_exception_string(e):
58-
return''.join(traceback.format_exception_only(type(e),e))
57+
defformat_exception_string():
58+
return''.join(traceback.format_exception_only(*sys.exc_info()[:2]))
59+
60+
61+
defprint_exception():
62+
print(format_exception_string(),file=sys.stderr)
5963

6064

6165
defrow_to_dict(row):

‎backend/main/views.py‎

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
importinspect
21
importjson
32
importlogging
3+
importtraceback
44
fromdatetimeimportdatetime
55
fromioimportStringIO
66
frompathlibimportPath
@@ -15,7 +15,7 @@
1515
fromdjango.contrib.auth.mixinsimportLoginRequiredMixin
1616
fromdjango.contrib.messages.viewsimportSuccessMessageMixin
1717
fromdjango.formsimportModelForm
18-
fromdjango.httpimportJsonResponse,HttpResponseBadRequest,HttpResponse
18+
fromdjango.httpimportJsonResponse,HttpResponse
1919
fromdjango.viewsimportView
2020
fromdjango.views.genericimportCreateView
2121
fromdjango_user_agents.utilsimportget_user_agent
@@ -28,47 +28,35 @@
2828
frommain.modelsimportCodeEntry,ListEmail
2929
frommain.textimportPage,page_slugs_list,pages,ExerciseStep,clean_program
3030
frommain.utils.djangoimportPlaceHolderForm
31-
frommain.workerimportworker_result
31+
frommain.workers.masterimportworker_result
3232

3333
log=logging.getLogger(__name__)
3434

3535

3636
defapi_view(request,method_name):
37-
body=request.body
3837
try:
3938
method=getattr(API(request),method_name)
40-
exceptAttributeError:
41-
log.error('Unknown method %s, body = %s',method_name,body)
42-
returnHttpResponseBadRequest()
43-
try:
39+
body=request.body
4440
body=body.decode('utf8')
45-
exceptUnicodeDecodeError:
46-
log.exception('Failed to decode %s',body)
47-
returnHttpResponseBadRequest()
48-
log.info('API request: method = %s, body = %s',method_name,body)
49-
try:
5041
args=json.loads(body)
51-
exceptValueErrorase:
52-
log.error('JSON decode error: %s',e)
53-
returnHttpResponseBadRequest()
54-
signature=inspect.signature(method)
55-
try:
56-
signature.bind(**args)
57-
exceptTypeErrorase:
58-
log.error(e)
59-
returnHttpResponseBadRequest()
60-
forarg_name,hintinget_type_hints(method).items():
61-
ifarg_name=='return':
62-
continue
63-
arg=args[arg_name]
64-
ifnotisinstance(arg,hint):
65-
log.warning(
66-
'Incorrect type for argument %s = %r of method %s: found %s, expected %s',
67-
arg_name,arg,method_name,arg.__class__.__name__,hint.__name__)
68-
result=method(**args)
69-
ifnotisinstance(result,dict):
70-
result= {'result':result}
71-
returnJsonResponse(result,json_dumps_params=dict(indent=4,sort_keys=True))
42+
forarg_name,hintinget_type_hints(method).items():
43+
ifarg_name=='return':
44+
continue
45+
arg=args[arg_name]
46+
ifnotisinstance(arg,hint):
47+
log.warning(
48+
'Incorrect type for argument %s = %r of method %s: found %s, expected %s',
49+
arg_name,arg,method_name,arg.__class__.__name__,hint.__name__)
50+
result=method(**args)
51+
ifnotisinstance(result,dict):
52+
result= {'result':result}
53+
exceptException:
54+
result=dict(
55+
error=dict(
56+
traceback=traceback.format_exc(),
57+
)
58+
)
59+
returnJsonResponse(result)
7260

7361

7462
classAPI:
@@ -107,6 +95,9 @@ def run_code(self, code, source):
10795
entry.output=result["output"]
10896
entry.save()
10997

98+
ifresult["error"]:
99+
returndict(error=result["error"])
100+
110101
ifresult["passed"]:
111102
self.move_step(1)
112103

@@ -128,7 +119,8 @@ def run_code(self, code, source):
128119

129120
forcallinbirdseye_objects["calls"]:
130121
call["function_id"]=function_ids[call["function_id"]]
131-
call["start_time"]=datetime.fromisoformat(call["start_time"])
122+
ifisinstance(call["start_time"],str):
123+
call["start_time"]=datetime.fromisoformat(call["start_time"])
132124
call=eye.db.Call(**call)
133125
session.add(call)
134126
# TODO get correct call from top level

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp