Skip to content
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
18 changes: 11 additions & 7 deletions apps/sim/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,13 +516,17 @@ async function handleInternalRequest(
// Many APIs (e.g., Microsoft Graph) return 202 with empty body
responseData = { status }
} else {
try {
responseData = await response.json()
} catch (jsonError) {
logger.error(`[${requestId}] JSON parse error for ${toolId}:`, {
error: jsonError instanceof Error ? jsonError.message : String(jsonError),
})
throw new Error(`Failed to parse response from ${toolId}: ${jsonError}`)
if (tool.transformResponse) {
responseData = null
} else {
try {
responseData = await response.json()
} catch (jsonError) {
logger.error(`[${requestId}] JSON parse error for ${toolId}:`, {
error: jsonError instanceof Error ? jsonError.message : String(jsonError),
})
throw new Error(`Failed to parse response from ${toolId}: ${jsonError}`)
}
}
}

Expand Down
37 changes: 37 additions & 0 deletions apps/sim/tools/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,43 @@ describe('executeRequest', () => {
error: 'Server Error', // Should use statusText in the error message
})
})

it('should handle transformResponse with non-JSON response', async () => {
const toolWithTransform = {
...mockTool,
transformResponse: async (response: Response) => {
const xmlText = await response.text()
return {
success: true,
output: {
parsedData: 'mocked xml parsing result',
originalXml: xmlText,
},
}
},
}

mockFetch.mockResolvedValueOnce({
ok: true,
status: 200,
statusText: 'OK',
text: async () => '<xml><test>Mock XML response</test></xml>',
})

const result = await executeRequest('test-tool', toolWithTransform, {
url: 'https://api.example.com',
method: 'GET',
headers: {},
})

expect(result).toEqual({
success: true,
output: {
parsedData: 'mocked xml parsing result',
originalXml: '<xml><test>Mock XML response</test></xml>',
},
})
})
})

describe('createParamSchema', () => {
Expand Down