forked from Roblox/graphql-lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocatedError.lua
More file actions
67 lines (58 loc) · 2.3 KB
/
locatedError.lua
File metadata and controls
67 lines (58 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
--[[
* Copyright (c) GraphQL Contributors
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
]]
-- ROBLOX upstream: https://github.com/graphql/graphql-js/blob/00d4efea7f5b44088356798afff0317880605f4d/src/error/locatedError.js
local LuauPolyfill = require("@pkg/@jsdotlua/luau-polyfill")
local Array = LuauPolyfill.Array
local Error = LuauPolyfill.Error
local instanceof = LuauPolyfill.instanceof
type Array<T> = LuauPolyfill.Array<T>
type Error = LuauPolyfill.Error
local inspect = require("../jsutils/inspect").inspect
local astModule = require("../language/ast")
type ASTNode = astModule.ASTNode
local GraphQLErrorModule = require("./GraphQLError")
local GraphQLError = GraphQLErrorModule.GraphQLError
type GraphQLError = GraphQLErrorModule.GraphQLError
local function locatedError(
rawOriginalError,
nodes: ASTNode | Array<ASTNode> | nil,
path: Array<string | number> | nil
): GraphQLError
-- Sometimes a non-error is thrown, wrap it as an Error instance to ensure a consistent Error interface.
local originalError: Error | GraphQLError
if instanceof(rawOriginalError, Error) then
originalError = rawOriginalError :: Error
elseif
typeof(rawOriginalError) == "table"
and typeof(rawOriginalError.message) == "string"
and rawOriginalError.stack ~= nil
then
-- ROBLOX deviation: special case for Error-ish objects with a message and stack field
originalError = Error.new(rawOriginalError.message)
elseif typeof(rawOriginalError) == "table" and typeof(rawOriginalError.error) == "string" then
-- ROBLOX deviation: special case for errors thrown via 'error("error message")'
originalError = Error.new("Unexpected error value: " .. inspect(rawOriginalError.error))
else
originalError = Error.new("Unexpected error value: " .. inspect(rawOriginalError))
end
-- Note: this uses a brand-check to support GraphQL errors originating from other contexts.
if Array.isArray((originalError :: GraphQLError).path) then
return originalError :: GraphQLError
end
local output = GraphQLError.new(
originalError.message,
(originalError :: GraphQLError).nodes or nodes,
(originalError :: GraphQLError).source,
(originalError :: GraphQLError).positions,
path,
originalError
)
return output
end
return {
locatedError = locatedError,
}