Skip to content

Commit

Permalink
Improve error message for streaming on old host version (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
ejizba authored Feb 27, 2024
1 parent d488b48 commit 344e4d5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { SetupOptions } from '../types';
import { AzFuncSystemError } from './errors';
import { tryGetCoreApiLazy } from './utils/tryGetCoreApiLazy';
import { workerSystemLog } from './utils/workerSystemLog';

let options: SetupOptions = {};
Expand All @@ -16,6 +17,16 @@ export function setup(opts: SetupOptions): void {
if (setupLocked) {
throw new AzFuncSystemError("Setup options can't be changed after app startup has finished.");
}

if (opts.enableHttpStream) {
// NOTE: coreApi.log was coincidentally added the same time as http streaming,
// so we can use that to validate the host version instead of messing with semver parsing
const coreApi = tryGetCoreApiLazy();
if (coreApi && !coreApi.log) {
throw new AzFuncSystemError(`HTTP streaming requires Azure Functions Host v4.28 or higher.`);
}
}

options = opts;
workerSystemLog('information', `Setup options: ${JSON.stringify(options)}`);
}
Expand Down
3 changes: 2 additions & 1 deletion src/utils/workerSystemLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { tryGetCoreApiLazy } from './tryGetCoreApiLazy';

export function workerSystemLog(level: types.LogLevel, ...args: unknown[]): void {
const coreApi = tryGetCoreApiLazy();
if (coreApi) {
// NOTE: coreApi.log doesn't exist on older versions of the worker
if (coreApi && coreApi.log) {
coreApi.log(level, 'system', format(...args));
} else {
fallbackLogHandler(level, ...args);
Expand Down

0 comments on commit 344e4d5

Please sign in to comment.