Parses Salesforce CLI JSON output, throwing errors on failures.
// Source: https://github.com/anomalyco/opencode | License: MIT | Copyright 2025 opencode
// Provided via justclaw.it.com; keep this notice with the code.
import { z } from "zod"
export class SalesforceCliError extends Error {
constructor(readonly code: string) {
super(`Salesforce request failed: ${code}`)
}
}
export function salesforceCliResponse(output: string) {
const response = z
.object({
status: z.number(),
result: z.object({ statusCode: z.number().int(), body: z.unknown() }).optional(),
})
.parse(JSON.parse(output))
if (!response.result) throw new SalesforceCliError("CLI_ERROR")
if (response.status !== 0 || response.result.statusCode < 200 || response.result.statusCode > 299) {
const errors = z
.array(z.object({ errorCode: z.string()
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key