Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit 1e797a9

Browse files
committed
Use extensions field for custom error fields
It is needed to avoid possible name clash with future versions of GraphQL standard. June, 2018 version of the standard strongly recommends to use `extensions` field for custom fields of an error. [1]: https://github.com/facebook/graphql/pull/407/files [2]: https://github.com/facebook/graphql/releases/tag/June2018
1 parent 349c3c3 commit 1e797a9

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

graphql/server/server.lua

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local fio = require('fio')
2+
local graphql_utils = require('graphql.utils')
23
local utils = require('graphql.server.utils')
34
local json = require('json')
45

@@ -100,11 +101,18 @@ function server.init(graphql, host, port)
100101

101102
local query = parsed.query
102103

103-
local ok, compiled_query = pcall(graphql.compile, graphql, query)
104+
local traceback
105+
local ok, compiled_query = xpcall(function()
106+
return graphql:compile(query)
107+
end, function(err)
108+
traceback = debug.traceback()
109+
return err
110+
end)
104111
if not ok then
112+
local err = graphql_utils.serialize_error(compiled_query, traceback)
105113
return {
106114
status = 200,
107-
body = json.encode({errors = {{message = compiled_query}}})
115+
body = json.encode({errors = {err}})
108116
}
109117
end
110118

graphql/utils.lua

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,20 +226,21 @@ function utils.optional_require_rex()
226226
end
227227

228228
function utils.serialize_error(err, traceback)
229+
local extensions = {traceback = traceback}
229230
if type(err) == 'string' then
230231
return {
231232
message = utils.strip_error(err),
232-
traceback = traceback,
233+
extensions = extensions,
233234
}
234235
elseif type(err) == 'cdata' and
235236
tostring(ffi.typeof(err)) == 'ctype<const struct error &>' then
236237
return {
237238
message = tostring(err),
238-
traceback = traceback,
239+
extensions = extensions,
239240
}
240241
elseif type(err) == 'table' and type(err.message) == 'string' then
241242
local err = table.copy(err)
242-
err.traceback = traceback
243+
err.extensions = extensions
243244
return err
244245
end
245246

@@ -248,10 +249,10 @@ function utils.serialize_error(err, traceback)
248249
json.cfg({encode_use_tostring = true})
249250
local orig_error = json.encode(err)
250251
json.cfg({encode_use_tostring = encode_use_tostring_orig})
252+
extensions.orig_error = orig_error
251253
return {
252254
message = message,
253-
orig_error = orig_error,
254-
traceback = traceback,
255+
extensions = extensions,
255256
}
256257
end
257258

0 commit comments

Comments
 (0)