Catch json errors a little better (#3437)
Co-authored-by: Douwe Osinga <douwe@squareup.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { getApiUrl, getSecretKey } from '../config';
|
||||
import { safeJsonParse } from './jsonUtils';
|
||||
|
||||
const getQuestionClassifierPrompt = (messageContent: string): string => `
|
||||
You are a simple classifier that takes content and decides if it is asking for input
|
||||
@@ -167,7 +168,7 @@ export async function ask(prompt: string): Promise<string> {
|
||||
throw new Error('Failed to get response');
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
const data = await safeJsonParse<{ response: string }>(response, 'Failed to get AI response');
|
||||
return data.response;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// Import the proper type from ConfigContext
|
||||
import { getApiUrl, getSecretKey } from '../config';
|
||||
import { safeJsonParse } from './jsonUtils';
|
||||
|
||||
export interface ModelCostInfo {
|
||||
input_token_cost: number; // Cost per token for input (in USD)
|
||||
@@ -47,7 +48,15 @@ async function fetchPricingForModel(
|
||||
throw new Error(`API request failed with status ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
const data = await safeJsonParse<{
|
||||
pricing: Array<{
|
||||
provider: string;
|
||||
model: string;
|
||||
input_token_cost: number;
|
||||
output_token_cost: number;
|
||||
currency: string;
|
||||
}>;
|
||||
}>(response, 'Failed to parse pricing data');
|
||||
|
||||
// Find the specific model pricing using the lookup provider/model
|
||||
const pricing = data.pricing?.find(
|
||||
|
||||
@@ -4,6 +4,7 @@ import * as fs from 'fs/promises';
|
||||
import * as path from 'path';
|
||||
import * as os from 'os';
|
||||
import log from './logger';
|
||||
import { safeJsonParse } from './jsonUtils';
|
||||
|
||||
interface GitHubRelease {
|
||||
tag_name: string;
|
||||
@@ -53,7 +54,10 @@ export class GitHubUpdater {
|
||||
throw new Error(`GitHub API returned ${response.status}: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const release: GitHubRelease = await response.json();
|
||||
const release: GitHubRelease = await safeJsonParse<GitHubRelease>(
|
||||
response,
|
||||
'Failed to get GitHub release information'
|
||||
);
|
||||
log.info(`GitHubUpdater: Found release: ${release.tag_name} (${release.name})`);
|
||||
log.info(`GitHubUpdater: Release published at: ${release.published_at}`);
|
||||
log.info(`GitHubUpdater: Release assets count: ${release.assets.length}`);
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
export async function safeJsonParse<T>(
|
||||
response: Response,
|
||||
errorMessage: string = 'Failed to parse server response'
|
||||
): Promise<T> {
|
||||
try {
|
||||
return (await response.json()) as T;
|
||||
} catch (error) {
|
||||
if (error instanceof SyntaxError) {
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user