Skip to content

Sentry.AWSLambda.wrapHandler is incorrectly marked as returning TResult | undefined #3364

Closed

Description

@G-Rath

Package + Version

  • @sentry/browser
  • @sentry/node
  • raven-js
  • raven-node (raven for node)
  • other: @sentry/serverless

Version:

6.2.5

Description

export function wrapHandler<TEvent, TResult>(
handler: Handler<TEvent, TResult>,
wrapOptions: Partial<WrapperOptions> = {},
): Handler<TEvent, TResult | undefined> {

By doing | undefined, the result is not assignable to a handler from @types/aws-lambda.

i.e:

import * as Sentry from '@sentry/serverless';
import { APIGatewayTokenAuthorizerHandler, PolicyDocument } from 'aws-lambda';

declare global {
  namespace NodeJS {
    interface ProcessEnv {
      BASIC_AUTH_USERNAME: string;
      BASIC_AUTH_PASSWORD: string;
    }
  }
}

const credentials = Buffer.from(
  `${process.env.BASIC_AUTH_USERNAME}:${process.env.BASIC_AUTH_PASSWORD}`
).toString('base64');

const AllowPolicyDocument: PolicyDocument = {
  Version: '2012-10-17',
  Statement: [
    {
      Effect: 'Allow',
      Action: ['execute-api:Invoke'],
      Resource: ['arn:aws:execute-api:ap-southeast-2:*']
    }
  ]
};

export const handler: APIGatewayTokenAuthorizerHandler = Sentry.AWSLambda.wrapHandler(
  async event => {
    if (event.authorizationToken === `Basic ${credentials}`) {
      console.log('successfully authenticated');

      return Promise.resolve({
        principalId: 'user',
        policyDocument: AllowPolicyDocument
      });
    }

    console.info(event);

    throw new Error('Unauthorized');
  }
);

This produces this error:

src/authorizer/index.ts:28:14 - error TS2322: Type 'Handler<APIGatewayTokenAuthorizerEvent, { principalId: string; policyDocument: PolicyDocument; } | undefined>' is not assignable to type 'APIGatewayTokenAuthorizerHandler'.
  Type 'void | Promise<{ principalId: string; policyDocument: PolicyDocument; } | undefined>' is not assignable to type 'void | Promise<APIGatewayAuthorizerResult>'.
    Type 'Promise<{ principalId: string; policyDocument: PolicyDocument; } | undefined>' is not assignable to type 'void | Promise<APIGatewayAuthorizerResult>'.
      Type 'Promise<{ principalId: string; policyDocument: PolicyDocument; } | undefined>' is not assignable to type 'Promise<APIGatewayAuthorizerResult>'.
        Type '{ principalId: string; policyDocument: PolicyDocument; } | undefined' is not assignable to type 'APIGatewayAuthorizerResult'.
          Type 'undefined' is not assignable to type 'APIGatewayAuthorizerResult'.

28 export const handler: APIGatewayTokenAuthorizerHandler = Sentry.AWSLambda.wrapHandler(

Activity

  1. xr0master commented on Dec 25, 2021

    @xr0master
    Contributor

    Hello, I have the same question. Was there a reason to add the undefined to types? Since it does not correspond to the AWS lambda types. Of course, this is easy to fix through casting, but it slightly increases the code and constantly needs to do it for each function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions