Skip to content

Commit

Permalink
Merge pull request #149 from lifeomic/no-task/adding-response-to-requ…
Browse files Browse the repository at this point in the history
…est-error

fix: Adding response to request error
  • Loading branch information
MorpheusNephew authored Oct 3, 2022
2 parents 9a59c60 + 9ee420f commit 8522265
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/adapters/lambda-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { AlphaOptions, AlphaAdapter, HandlerRequest } from '../types';
import { v4 as uuid } from 'uuid';
import { Context, Handler } from 'aws-lambda';
import { Alpha } from '../alpha';
import { AxiosResponse } from 'axios';

const createContext = (provided?: Partial<Context>): Context => {
const defaultCtx: Context = {
Expand Down Expand Up @@ -39,7 +40,7 @@ const lambdaHandlerAdapter: AlphaAdapter = async (config) => {
const result = await handler(request.event, request.context as Context) as Payload;
return lambdaResponse(config, request, result);
} catch (error: any | Error) {
throw new RequestError(error.message as string, config, request);
throw new RequestError(error.message as string, config, request, error.response as AxiosResponse);
}
};

Expand Down
23 changes: 23 additions & 0 deletions test/lambda-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,29 @@ const registerSpecs = (isCallbackStyleHandler: boolean) => {

expect(ctx.handler).toBeCalledWith(expect.objectContaining(event), expect.any(Object), expect.any(Function));
});

test(`When a local handler returns an error the request fails with a response (callbackStyle=${isCallbackStyleHandler})`, async () => {
const failure = new Error('simulated failure');
const failureResponse = {
status: 400,
data: {
error: 'simulated failure response',
},
};
(failure as any).response = failureResponse;

setupHandlerBehavior({
handlerStub: ctx.handler,
error: failure,
isCallbackStyleHandler,
});

const promise = ctx.client.get('/some/path');
await expect(promise).rejects.toThrow(failure.message);
const error = await promise.catch((error) => error);

expect(error.response).toBe(failureResponse);
});
};

registerSpecs(true);
Expand Down

0 comments on commit 8522265

Please sign in to comment.