I’m currently trying to implement error handling with Dgraph lambdas. It has been working if I manually check for errors on the return type of eg. a query within the lambda function. However, if I wrap the entire lambda logic inside a try-catch
block, and try to return the error from catch, the return object remains an empty object.
To clarify
Assume a lambda like this:
const someFunc = async ({parent, args, graphql}) => {
try {
// do some graphql query
const getNode = await graphql(query, vars);
if (getNode.errors) {
throw new CustomError(getNode.errors[0].message);
}
return {
__typename: "CustomResult"
success: "You did it!"
}
} catch (error) {
return {
__typename: "CustomError"
error: error.message
}
}
}
self.addGraphQLResolvers({
"Query.someFunc": someFunc,
});
For a query:
query SomeFunc {
someFunc {
...on CustomError {
error
}
...on CustomResult {
success
}
}
}
The query result on error would then look like:
{
"data": {
"someFunc": {}
},
"extensions": {
"tracing": {
"version": 1,
"startTime": "2022-07-01T10:04:53.538202875Z",
"endTime": "2022-07-01T10:04:54.133881073Z",
"duration": 595678278
}
}
}
However, if I remove the try-catch
and test directly
const someFunc = async ({parent, args, graphql}) => {
// do some graphql query
const getNode = await graphql(query, vars);
if (getNode.errors) {
return {
__typename: "CustomError",
error: getNode.errors[0].message
}
}
return {
__typename: "CustomResult"
success: "You did it!"
}
}
self.addGraphQLResolvers({
"Query.someFunc": someFunc,
});
it works as expected and I get the on CustomError
result.
Any ideas why this is not working?