Skip to content

Commit

Permalink
fix: improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Sep 13, 2022
1 parent 03b8167 commit 1dc21e2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
20 changes: 17 additions & 3 deletions telefunc/client/callTelefunc/makeHttpRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const method = 'POST'
const STATUS_CODE_SUCCESS = 200
const STATUS_CODE_ABORT = 403
const STATUS_CODE_BUG = 500
const STATUS_CODE_INVALID = 400

async function makeHttpRequest(callContext: {
telefuncUrl: string
Expand Down Expand Up @@ -50,7 +51,7 @@ async function makeHttpRequest(callContext: {
} else if (statusCode === STATUS_CODE_BUG) {
const responseBody = await response.text()
assertUsage(
responseBody === 'Internal Server Error (Telefunc Request)',
responseBody === 'Internal Server Error (Telefunc)',
installErr({
reason: 'an HTTP response body that Telefunc never generates',
method,
Expand All @@ -59,6 +60,19 @@ async function makeHttpRequest(callContext: {
)
const telefunctionCallError = new Error('Server Error')
return { telefunctionCallError }
} else if (statusCode === STATUS_CODE_INVALID) {
const responseBody = await response.text()
assertUsage(
responseBody === 'Invalid Request (Telefunc)',
installErr({
reason: 'an HTTP response body that Telefunc never generates',
method,
callContext
})
)
// In theory this error should never happen.
const telefunctionCallError = new Error('Invalid Telefunc Request')
return { telefunctionCallError }
} else {
assertUsage(
statusCode !== 404,
Expand All @@ -72,7 +86,7 @@ async function makeHttpRequest(callContext: {
assertUsage(
false,
installErr({
reason: `a status code \`${statusCode}\` which Telefunc does not return`,
reason: `a status code \`${statusCode}\` which Telefunc never uses`,
method,
callContext
})
Expand Down Expand Up @@ -115,6 +129,6 @@ function installErr({
if (reason) {
msg.push(...[`: the HTTP ${method} \`${callContext.telefuncUrl}\` request returned `, reason])
}
msg.push(`. See https://telefunc.com/install`)
msg.push(`, see https://telefunc.com/install`)
return msg.join('')
}
30 changes: 20 additions & 10 deletions telefunc/node/server/runTelefunc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,34 @@ import { globalContext } from './globalContext'
import { telefuncConfig } from './telefuncConfig'

type HttpResponse = {
statusCode: 200 | 500 | 400 | 403
statusCode: 200 | 403 | 500 | 400
body: string
contentType: 'text/plain'
etag: string | null
}
const malformedRequest = {
statusCode: 400 as const,
body: 'Malformed Request',

// Status code for `throw Abort()`
const abortedRequestStatusCode = 403

// HTTP Response for:
// - User's telefunction threw an error (that isn't `Abort()`).
// - Telefunc throw an error (i.e. Telefunc has a bug).
const serverError = {
statusCode: 500 as const,
body: 'Internal Server Error (Telefunc)',
contentType: 'text/plain' as const,
etag: null
}
const serverError = {
statusCode: 500 as const,
body: 'Internal Server Error (Telefunc Request)',

// HTTP Response for:
// - The telefunction couldn't be found.
// - Some non-telefunc client makes a malformed HTTP request.
const invalidRequest = {
statusCode: 400 as const,
body: 'Invalid Request (Telefunc)',
contentType: 'text/plain' as const,
etag: null
}
const abortedRequestStatusCode = 403

async function runTelefunc(runContext: Parameters<typeof runTelefunc_>[0]) {
try {
Expand Down Expand Up @@ -63,7 +73,7 @@ async function runTelefunc_(httpRequest: { url: string; method: string; body: un
{
const parsed = parseHttpRequest(runContext)
if (parsed.isMalformed) {
return malformedRequest
return invalidRequest
}
const { telefunctionName, telefunctionKey, telefunctionArgs, telefunctionFilePath, telefunctionFileExport } = parsed
objectAssign(runContext, {
Expand All @@ -90,7 +100,7 @@ async function runTelefunc_(httpRequest: { url: string; method: string; body: un
{
const telefunction = findTelefunction(runContext)
if (!telefunction) {
return malformedRequest
return invalidRequest
}
objectAssign(runContext, { telefunction })
}
Expand Down

0 comments on commit 1dc21e2

Please sign in to comment.