Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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");
})
})
26 changes: 24 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,20 @@ 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)
} else {
// To avoid breaking code where `appendError` is not implemented, we instead print 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