Skip to content

Fix crashes on axios errors #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lambda-feedback-segp-sandbox/ef-test-server",
"version": "0.3.0",
"version": "0.4.0",
"main": "dist/index.js",
"scripts": {
"build": "yarn tsc",
Expand Down
19 changes: 15 additions & 4 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,22 @@ export function parseRequestBody(body: unknown): {
return { url, evaluationFunctionRequestData }
}

export async function handleRawRequest(body: unknown): Promise<object | null> {
export enum HandlerError {
ParseFailed,
ForwardingFailed,
}

export async function handleRawRequest(
body: unknown,
): Promise<object | HandlerError> {
const parseResult = parseRequestBody(body)
if (parseResult == null) return null
if (parseResult == null) return HandlerError.ParseFailed

const { url, evaluationFunctionRequestData } = parseResult
const response = await axios.post(url, evaluationFunctionRequestData)
return response.data
try {
const response = await axios.post(url, evaluationFunctionRequestData)
return response.data
} catch (_) {
return HandlerError.ForwardingFailed
}
}
26 changes: 16 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import cors from 'cors'
import express from 'express'
import dotenv from 'dotenv'

import { handleRawRequest } from './handler'
import { HandlerError, handleRawRequest } from './handler'

dotenv.config()

Expand All @@ -14,15 +14,21 @@ app.use(express.json())
app.post('/', async (req, res) => {
console.log(`[INFO] Received POST request:`, req.body)
const responseData = await handleRawRequest(req.body)
if (responseData == null) {
console.log('[WARN] Invalid request received and ignored.')
res.status(400).send()
} else {
console.log(
'[INFO] Received response from evaluation function:',
responseData,
)
res.send(responseData)
switch (responseData) {
case HandlerError.ParseFailed:
console.log('[WARN] Invalid request received and ignored.')
res.status(400).send()
break
case HandlerError.ForwardingFailed:
console.log('[WARN] Failed to forward request to evaluation function.')
res.status(400).send()
break
default:
console.log(
'[INFO] Received response from evaluation function:',
responseData,
)
res.send(responseData)
}
})

Expand Down
Loading