Skip to content

Conversation

arnabrahman
Copy link
Contributor

@arnabrahman arnabrahman commented Aug 13, 2025

Summary

This PR adds support for error handling in AppSync GraphQL described in the issue

Changes

  • exceptionHandler method is introduced to register an exception and its handler.
  • ExceptionHandlerRegistry is introduced to keep track of registered exception handlers
  • exceptionHandler is also exposed as a decorator method
  • Unit tests are written for 100% coverage
  • Added documentation for exception handling with examples
  • Minor housekeeping(ex: dockerfile nodejs version update due to error & endregion added)

Lambda handler:

import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
import { Logger } from '@aws-lambda-powertools/logger';
import { AssertionError } from 'assert';
import type { Context } from 'aws-lambda';

const logger = new Logger({
  serviceName: 'MyService',
});
const app = new AppSyncGraphQLResolver({ logger });

app.exceptionHandler(AssertionError, async (error) => {
  return {
    error: {
      message: error.message,
      type: error.name,
    },
  };
});

app.onQuery('createSomething', async () => {
  throw new AssertionError({
    message: 'This is an assertion Error',
  });
});

export const handler = async (event: unknown, context: Context) =>
  app.resolve(event, context);

APPSYNC JS resolver

export function request(ctx) {
  return {
    operation: 'Invoke',
    payload: ctx,
  };
}

export function response(ctx) {
  if (ctx.result.error) {
    return util.error(ctx.result.error.message, ctx.result.error.type);
  }
  return ctx.result;
}

Response mapping template

#if (!$util.isNull($ctx.result.error))
  $util.error($ctx.result.error.message, $ctx.result.error.type)
#end

$utils.toJson($ctx.result)

Response:

{
  "data": {
    "createSomething": null
  },
  "errors": [
    {
      "path": [
        "createSomething"
      ],
      "data": null,
      "errorType": "AssertionError",
      "errorInfo": null,
      "locations": [
        {
          "line": 2,
          "column": 3,
          "sourceName": null
        }
      ],
      "message": "This is an assertion Error"
    }
  ]
}

Issue number: closes #4130


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@pull-request-size pull-request-size bot added size/XXL PRs with 1K+ LOC, largely documentation related and removed size/XL PRs between 500-999 LOC, often PRs that grown with feedback labels Aug 21, 2025
@arnabrahman arnabrahman marked this pull request as ready for review August 21, 2025 05:14
@leandrodamascena
Copy link
Contributor

Hi @arnabrahman, thanks for responding to all the comments and making the necessary changes! We'll be back with this PR and review next week.

Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

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

Thank you for the PR and for the patience, I was meant to review this on Friday but I had some personal issues and couldn't get to it.

I've left a couple questions and a few suggestions - once they're addressed we're ready to merge.

arnabrahman and others added 7 commits August 27, 2025 10:06
Co-authored-by: Andrea Amorosi <dreamorosi@gmail.com>
…ing.ts

Co-authored-by: Andrea Amorosi <dreamorosi@gmail.com>
…ing.ts

Co-authored-by: Andrea Amorosi <dreamorosi@gmail.com>
…ver.ts

Co-authored-by: Andrea Amorosi <dreamorosi@gmail.com>
…QLResolver.test.ts

Co-authored-by: Andrea Amorosi <dreamorosi@gmail.com>
…QLResolver.test.ts

Co-authored-by: Andrea Amorosi <dreamorosi@gmail.com>
Co-authored-by: Andrea Amorosi <dreamorosi@gmail.com>
@arnabrahman arnabrahman marked this pull request as draft August 27, 2025 04:17
@arnabrahman arnabrahman marked this pull request as ready for review August 27, 2025 05:46
@arnabrahman arnabrahman requested a review from dreamorosi August 27, 2025 05:46
Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

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

Thank you for working on this feature @arnabrahman - apologies if this has taken a bit longer than usual to get merged, we really appreciate your contribution!

Copy link

@dreamorosi dreamorosi merged commit 77a992f into aws-powertools:main Aug 27, 2025
34 checks passed
@svozza
Copy link
Contributor

svozza commented Aug 27, 2025

Fantastic work @arnabrahman. Another fine addition!

@arnabrahman arnabrahman deleted the 4130-graphql-error-registry branch August 27, 2025 13:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation event-handler This item relates to the Event Handler Utility size/XXL PRs with 1K+ LOC, largely documentation related tests PRs that add or change tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: support error handling in AppSync GraphQL
4 participants