Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions __tests__/__snapshots__/resolvers.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ exports[`dynamodb resolvers something 2`] = `
]
`;

exports[`error handling error 1`] = `
{
"message": "foo",
}
`;

exports[`error handling unauthorized 1`] = `
{
"message": "Unauthorized",
}
`;

exports[`rds resolvers attributeExists nested or 1`] = `
{
"statements": [
Expand Down
12 changes: 11 additions & 1 deletion __tests__/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const runResolverFunctionOnAWS = async (code, context, functionName) => {
});

const result = await client.send(command);
if (result.error) {
return result.error;
}
try {
return JSON.parse(result.evaluationResult);
} catch (e) {
Expand Down Expand Up @@ -72,7 +75,14 @@ export const checkResolverValid = async (code, context, functionName) => {
const fn = module[functionName];

transformContextForAppSync(context);
result = fn(context);
try {
result = fn(context);
} catch (e) {
if (e.name !== "AppSyncUserError") {
throw e;
}
result = {"message": e.message}
}
}
expect(result).toMatchSnapshot();
};
Expand Down
20 changes: 20 additions & 0 deletions __tests__/resolvers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1016,3 +1016,23 @@ describe("rds resolvers", () => {
});
});

describe("error handling", () => {
test("error", async () => {
const code = `
export function request(ctx) {
util.error("foo")
}
export function response(ctx) {}
`;
await checkResolverValid(code, {}, "request");
})
test("unauthorized", async () => {
const code = `
export function request(ctx) {
util.unauthorized()
}
export function response(ctx) {}
`;
await checkResolverValid(code, {}, "request");
})
})
28 changes: 26 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { v4 as uuidv4 } from 'uuid';
import { toJsonObject } from './rds/index.js'

class AppSyncUserError extends Error {
constructor(message, errorType, data, errorInfo) {
super(message);
this.name = "AppSyncUserError";
this.errorType = errorType;
this.data = data;
this.errorInfo = errorInfo;
}
}

export const dynamodbUtils = {
toDynamoDB: function(value) {
if (typeof (value) === "number") {
Expand Down Expand Up @@ -121,8 +131,22 @@ export const util = {
return uuidv4();
},
appendError: function(message, errorType, data, errorInfo) {
// This will be handled in LocalStack in a side channel by printing to stderr
console.error({ message, errorType, data, errorInfo });
const error = { message, errorType, data, errorInfo }
if( console.appendError ) {
// LocalStack is adding `appendError` to console, allowing to push errors to context.outErrors
console.appendError(error)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will impact users who want to use this package directly (outside of LocalStack 😱 ) right?

I guess the workaround is to add an appendError attribute to the console global in their own code, so perhaps this will encourage more people to use LocalStack!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it is inside a if block, the previous behaviour of console.error will apply when console.appendError is undefined. So it will still work as it previously did.

With that consideration in mind though, I will remove the TODO and leave a comment to explain.

} else {
// TODO: remove this legacy implementation. We are still supporting it for backward compatibility and prevent
// breaking user's code calling appendError.
// This will be handled in LocalStack in a side channel by printing to stderr
console.error({ message, errorType, data, errorInfo });
}
},
error: function(message, errorType, data, errorInfo) {
throw new AppSyncUserError(message, errorType, data, errorInfo)
},
unauthorized: function() {
throw new AppSyncUserError("Unauthorized", "UnauthorizedException")
},
time: {
nowFormatted: function(pattern) {
Expand Down