Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor(jsm): extract parseJsmErrorMessage helper to deduplicate err…
…or handling
  • Loading branch information
waleedlatif1 committed Apr 9, 2026
commit e513fd18896012bcfa6c5f3587172509188bea30
48 changes: 22 additions & 26 deletions apps/sim/app/api/tools/jsm/request/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ export const dynamic = 'force-dynamic'

const logger = createLogger('JsmRequestAPI')

function parseJsmErrorMessage(status: number, statusText: string, errorText: string): string {
try {
const errorData = JSON.parse(errorText)
if (errorData.errorMessage) {
return `JSM API error: ${errorData.errorMessage}`
}
} catch {
if (errorText) {
return `JSM API error: ${errorText}`
}
}
return `JSM API error: ${status} ${statusText}`
}

export async function POST(request: NextRequest) {
const auth = await checkInternalAuth(request)
if (!auth.success || !auth.userId) {
Expand Down Expand Up @@ -127,20 +141,11 @@ export async function POST(request: NextRequest) {
error: errorText,
})

let errorMessage = `JSM API error: ${response.status} ${response.statusText}`
try {
const errorData = JSON.parse(errorText)
if (errorData.errorMessage) {
errorMessage = `JSM API error: ${errorData.errorMessage}`
}
} catch {
if (errorText) {
errorMessage = `JSM API error: ${errorText}`
}
}

return NextResponse.json(
{ error: errorMessage, details: errorText },
{
error: parseJsmErrorMessage(response.status, response.statusText, errorText),
details: errorText,
},
{ status: response.status }
)
}
Expand Down Expand Up @@ -205,20 +210,11 @@ export async function POST(request: NextRequest) {
error: errorText,
})

let errorMessage = `JSM API error: ${response.status} ${response.statusText}`
try {
const errorData = JSON.parse(errorText)
if (errorData.errorMessage) {
errorMessage = `JSM API error: ${errorData.errorMessage}`
}
} catch {
if (errorText) {
errorMessage = `JSM API error: ${errorText}`
}
}

return NextResponse.json(
{ error: errorMessage, details: errorText },
{
error: parseJsmErrorMessage(response.status, response.statusText, errorText),
details: errorText,
},
{ status: response.status }
)
}
Expand Down
Loading