@@ -11,7 +11,7 @@ See the License for the specific language governing permissions and
1111limitations under the License.
1212*/
1313
14- import axios from 'axios' ;
14+ import axios , { AxiosError , AxiosResponse } from 'axios' ;
1515import { APIGatewayProxyWithLambdaAuthorizerEvent } from 'aws-lambda' ;
1616import { requestPayloadIsValid } from './request-payload-is-valid' ;
1717import { 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