Skip to content
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

fix: return 500 Internal Server Error on python error #1796

Merged
merged 6 commits into from
Aug 30, 2024
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
13 changes: 8 additions & 5 deletions src/events/http/HttpServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ export default class HttpServer {
/* RESPONSE SELECTION (among endpoint's possible responses) */

// Failure handling
let errorStatusCode = "502"
let errorStatusCode = "500"

if (err) {
const errorMessage = (err.message || err).toString()
Expand All @@ -655,14 +655,17 @@ export default class HttpServer {
if (found && found.length > 1) {
;[, errorStatusCode] = found
} else {
errorStatusCode = "502"
errorStatusCode = "500"
}

// Mocks Lambda errors
result = {
errorMessage,
errorType: err.constructor.name,
stackTrace: this.#getArrayStackTrace(err.stack),
body: JSON.stringify({
message: errorMessage,
stackTrace: this.#getArrayStackTrace(err.stack),
type: err.constructor.name,
}),
statusCode: errorStatusCode,
}

log.error(errorMessage)
Expand Down
4 changes: 2 additions & 2 deletions src/lambda/handler-runner/python-runner/PythonRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ export default class PythonRunner {
})

const onErr = (data) => {
// TODO

log.notice(data.toString())

rej(new Error("Internal Server Error"))
}

const onLine = (line) => {
Expand Down
3 changes: 3 additions & 0 deletions tests/runtimes/python/python3/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ def hello(event, context):
"body": json.dumps(body),
"statusCode": 200,
}

def error(event, context):
raise Exception("This is an error")
9 changes: 9 additions & 0 deletions tests/runtimes/python/python3/python3.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,13 @@ describe("Python 3 tests", function desc() {
assert.deepEqual(json, expected)
})
})

it("should return a 500 on error", async () => {
const url = new URL("/dev/error", BASE_URL)
const response = await fetch(url)
const json = await response.json()

assert.equal(response.status, 500)
assert.equal(json.message, "Internal Server Error")
})
})
6 changes: 6 additions & 0 deletions tests/runtimes/python/python3/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@ functions:
method: get
path: hello
handler: handler.hello
error:
events:
- http:
method: get
path: error
handler: handler.error
Loading