Improved error handling

This commit is contained in:
Syrus Akbary 2015-11-28 13:05:34 -08:00
parent 60fba63c45
commit f3d7eedafd

View File

@ -76,7 +76,29 @@ class Playground extends React.Component {
} }
this.pypyjs = this.pypy_interpreter.ready().then(() => { this.pypyjs = this.pypy_interpreter.ready().then(() => {
return this.pypy_interpreter.exec("import graphene"); return this.pypy_interpreter.exec(`
import graphene
import js
from collections import OrderedDict
from graphql.core.execution.executor import Executor
from graphql.core.execution.middlewares.sync import SynchronousExecutionMiddleware
from graphql.core.error import GraphQLError, format_error
def get_wrapped(f):
if hasattr(f, 'func_closure') and f.func_closure:
return get_wrapped(f.func_closure[0].cell_contents)
return f
class TrackResolver(SynchronousExecutionMiddleware):
@staticmethod
def run_resolve_fn(resolver, original_resolver):
if resolver.func.__module__ == '__main__':
line = get_wrapped(resolver.func).resolver.func_code.co_firstlineno
js.globals.markLine(line-3)
return SynchronousExecutionMiddleware.run_resolve_fn(resolver, original_resolver)
__graphene_executor = Executor([TrackResolver()], map_type=OrderedDict)
`);
}).then(() => { }).then(() => {
this.createSchema(baseCode); this.createSchema(baseCode);
}).then(() => { }).then(() => {
@ -110,6 +132,7 @@ class Playground extends React.Component {
this.createSchema(this.editor.getValue()); this.createSchema(this.editor.getValue());
} }
createSchema(code) { createSchema(code) {
console.log('createSchema');
this.validSchema = null; this.validSchema = null;
this.pypyjs.then(() => { this.pypyjs.then(() => {
return this.pypy_interpreter.exec(` return this.pypy_interpreter.exec(`
@ -117,14 +140,13 @@ schema = None
${code} ${code}
assert schema, 'You have to define a schema' assert schema, 'You have to define a schema'
`) `)
}).then(() => {
console.log('NO ERRORS');
this.validSchema = true;
}, (err) => { }, (err) => {
console.log('ERROR', err);
this.validSchema = false; this.validSchema = false;
// this.editor.setGutterMarker(5, "breakpoints", syntaxError()); // this.editor.setGutterMarker(5, "breakpoints", syntaxError());
console.log('ERROR', err);
}).then(() => {
if (this.validSchema === null) {
this.validSchema = true;
}
}).then(this.updateGraphiQL.bind(this)); }).then(this.updateGraphiQL.bind(this));
} }
updateGraphiQL() { updateGraphiQL() {
@ -145,29 +167,8 @@ assert schema, 'You have to define a schema'
// console.log('execute', query); // console.log('execute', query);
return this.pypyjs.then(() => { return this.pypyjs.then(() => {
var x = ` var x = `
import js
import json import json
from collections import OrderedDict result = __graphene_executor.execute(schema.schema, '''${query}''')
from graphql.core.execution.executor import Executor
from graphql.core.execution.middlewares.sync import SynchronousExecutionMiddleware
from graphql.core.error import GraphQLError, format_error
def get_wrapped(f):
if hasattr(f, 'func_closure') and f.func_closure:
return get_wrapped(f.func_closure[0].cell_contents)
return f
class TrackResolver(SynchronousExecutionMiddleware):
@staticmethod
def run_resolve_fn(resolver, original_resolver):
if resolver.func.__module__ == '__main__':
line = get_wrapped(resolver.func).resolver.func_code.co_firstlineno
js.globals.markLine(line-3)
return SynchronousExecutionMiddleware.run_resolve_fn(resolver, original_resolver)
executor = Executor([TrackResolver()], map_type=OrderedDict)
result = executor.execute(schema.schema, '''${query}''')
result_dict = {}; result_dict = {};
if result.errors: if result.errors:
result_dict['errors'] = [format_error(e) for e in result.errors] result_dict['errors'] = [format_error(e) for e in result.errors]