Skip to content

Commit 3c260c9

Browse files
authored
fix: return error response if destination request fails (#85)
1 parent 0b058dd commit 3c260c9

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

lambda/proxy.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,39 @@ describe('proxy', () => {
220220
expect(result).toEqual(expectedResponseObject);
221221
expect(axios.post).toHaveBeenCalled();
222222
});
223+
224+
it('should return error response from axios', async () => {
225+
const axiosErrorResponse: AxiosResponse = {
226+
status: 400,
227+
data: {
228+
some: 'error'
229+
},
230+
headers: {
231+
response: 'headers'
232+
},
233+
statusText: 'bad request',
234+
config: {
235+
headers: {} as AxiosRequestHeaders
236+
}
237+
};
238+
(axios.post as jest.Mock).mockRejectedValue({ response: axiosErrorResponse });
239+
240+
process.env.ENTERPRISE_MANAGED_USER_SUFFIX = 'suffix';
241+
const destinationUrl = 'https://approved.host/github-webhook/';
242+
const endpointId = encodeURIComponent(destinationUrl);
243+
const event: APIGatewayProxyWithLambdaAuthorizerEvent<any> = {
244+
...baseEvent,
245+
body: JSON.stringify(VALID_PUSH_PAYLOAD_USER_REPO),
246+
pathParameters: {
247+
endpointId
248+
}
249+
};
250+
const result = await handler(event);
251+
expect(result).toEqual({
252+
statusCode: 400,
253+
body: '{"some":"error"}',
254+
headers: { response: 'headers' }
255+
});
256+
expect(axios.post).toHaveBeenCalled();
257+
});
223258
});

lambda/proxy.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ See the License for the specific language governing permissions and
1111
limitations under the License.
1212
*/
1313

14-
import axios from 'axios';
14+
import axios, { AxiosError, AxiosResponse } from 'axios';
1515
import { APIGatewayProxyWithLambdaAuthorizerEvent } from 'aws-lambda';
1616
import { requestPayloadIsValid } from './request-payload-is-valid';
1717
import { destinationHostIsAllowed } from './destination-host-is-allowed';
@@ -47,10 +47,18 @@ export async function handler(event: APIGatewayProxyWithLambdaAuthorizerEvent<an
4747
data,
4848
headers: responseHeaders
4949
} = await axios.post(url, body, { headers, httpsAgent: getHttpsAgent() });
50+
console.log('Request was forwarded successfully!');
5051
console.log('result', JSON.stringify({ statusCode, body: data, headers: responseHeaders }));
5152
return { statusCode, body: JSON.stringify(data), headers: responseHeaders };
52-
} catch (e) {
53-
console.error(e);
53+
} catch (error) {
54+
if (typeof error === 'object' && error && 'response' in error) {
55+
console.log('Request was forwarded but got an error response.');
56+
const { status: statusCode, data, headers: responseHeaders } = error.response as AxiosResponse;
57+
console.log('result', JSON.stringify({ statusCode, body: data, headers: responseHeaders }));
58+
return { statusCode, body: JSON.stringify(data), headers: responseHeaders };
59+
}
60+
61+
console.error('An unknown error occurred.', error);
5462
return { statusCode: 500, body: 'Internal server error' };
5563
}
5664
}

0 commit comments

Comments
 (0)