Skip to content

Commit 708321d

Browse files
authored
fix(tools): added transform response to handle non-json responses for internal tools (#1400)
1 parent e4d35af commit 708321d

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

apps/sim/tools/index.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -516,13 +516,17 @@ async function handleInternalRequest(
516516
// Many APIs (e.g., Microsoft Graph) return 202 with empty body
517517
responseData = { status }
518518
} else {
519-
try {
520-
responseData = await response.json()
521-
} catch (jsonError) {
522-
logger.error(`[${requestId}] JSON parse error for ${toolId}:`, {
523-
error: jsonError instanceof Error ? jsonError.message : String(jsonError),
524-
})
525-
throw new Error(`Failed to parse response from ${toolId}: ${jsonError}`)
519+
if (tool.transformResponse) {
520+
responseData = null
521+
} else {
522+
try {
523+
responseData = await response.json()
524+
} catch (jsonError) {
525+
logger.error(`[${requestId}] JSON parse error for ${toolId}:`, {
526+
error: jsonError instanceof Error ? jsonError.message : String(jsonError),
527+
})
528+
throw new Error(`Failed to parse response from ${toolId}: ${jsonError}`)
529+
}
526530
}
527531
}
528532

apps/sim/tools/utils.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,43 @@ describe('executeRequest', () => {
527527
error: 'Server Error', // Should use statusText in the error message
528528
})
529529
})
530+
531+
it('should handle transformResponse with non-JSON response', async () => {
532+
const toolWithTransform = {
533+
...mockTool,
534+
transformResponse: async (response: Response) => {
535+
const xmlText = await response.text()
536+
return {
537+
success: true,
538+
output: {
539+
parsedData: 'mocked xml parsing result',
540+
originalXml: xmlText,
541+
},
542+
}
543+
},
544+
}
545+
546+
mockFetch.mockResolvedValueOnce({
547+
ok: true,
548+
status: 200,
549+
statusText: 'OK',
550+
text: async () => '<xml><test>Mock XML response</test></xml>',
551+
})
552+
553+
const result = await executeRequest('test-tool', toolWithTransform, {
554+
url: 'https://api.example.com',
555+
method: 'GET',
556+
headers: {},
557+
})
558+
559+
expect(result).toEqual({
560+
success: true,
561+
output: {
562+
parsedData: 'mocked xml parsing result',
563+
originalXml: '<xml><test>Mock XML response</test></xml>',
564+
},
565+
})
566+
})
530567
})
531568

532569
describe('createParamSchema', () => {

0 commit comments

Comments
 (0)