Skip to content

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

Closed
@G-Rath

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(

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions