Skip to content

Marketing skills for Claude Code and AI agents. CRO, copywriting, SEO, analytics, and growth engineering.

fix(clis): exit unsuccessfully on rejected HTTP requests

Rudy Celekli404930373a8d
diff --git a/tests/clis/calendly-webhooks.test.cjs b/tests/clis/calendly-webhooks.test.cjs
index bd8c8ce..561ddb6 100644
--- a/tests/clis/calendly-webhooks.test.cjs
+++ b/tests/clis/calendly-webhooks.test.cjs
@@ -11,7 +11,7 @@ function run(args, network = false) {
if (!${network}) throw new Error('Unexpected request');
const query = new URL(url).searchParams;
if (query.get('scope') === 'user' && !query.get('user')) throw new Error('400 missing user');
- return {status: 200, text: async () => JSON.stringify({ url, method: options.method, body: options.body ? JSON.parse(options.body) : null })};
+ return {ok: true, status: 200, text: async () => JSON.stringify({ url, method: options.method, body: options.body ? JSON.parse(options.body) : null })};
}; process.argv = ['node', ${JSON.stringify(cli)}, ...${JSON.stringify(args)}]; require(${JSON.stringify(cli)});`
return spawnSync(process.execPath, ['-e', code], {encoding:'utf8', env:{...process.env, CALENDLY_API_KEY:'test-only-key'}})
}
diff --git a/tests/clis/email-boolean-states.test.cjs b/tests/clis/email-boolean-states.test.cjs
index 7c9b4e1..11427aa 100644
--- a/tests/clis/email-boolean-states.test.cjs
+++ b/tests/clis/email-boolean-states.test.cjs
@@ -6,7 +6,7 @@ function run(vendor, args, fetch = true) {
const cli = path.resolve(__dirname, `../../tools/clis/${vendor}.js`)
const source = `global.fetch = async (url, options) => {
if (!${fetch}) throw new Error('Unexpected network request');
- return { status: 200, text: async () => JSON.stringify({ url, method: options.method, body: options.body ? JSON.parse(options.body) : null }) };
+ return { ok: true, status: 200, text: async () => JSON.stringify({ url, method: options.method, body: options.body ? JSON.parse(options.body) : null }) };
}; process.argv = ['node', ${JSON.stringify(cli)}, ...${JSON.stringify(args)}]; require(${JSON.stringify(cli)});`
return spawnSync(process.execPath, ['-e', source], { encoding: 'utf8', timeout: 5000, env: {
...process.env, REWARDFUL_API_KEY: 'fake-key', RESEND_API_KEY: 'fake-key', POSTMARK_API_KEY: 'fake-key', BEEHIIV_API_KEY: 'fake-key', KLAVIYO_API_KEY: 'fake-key',
diff --git a/tests/clis/email-colon-values.test.cjs b/tests/clis/email-colon-values.test.cjs
index d6b2850..07ae929 100644
--- a/tests/clis/email-colon-values.test.cjs
+++ b/tests/clis/email-colon-values.test.cjs
@@ -6,7 +6,7 @@ function run(vendor, args, fetch = true) {
const cli = path.resolve(__dirname, `../../tools/clis/${vendor}.js`)
const source = `global.fetch = async (url, options) => {
if (!${fetch}) throw new Error('Unexpected network request');
- return { status: 200, text: async () => JSON.stringify({ url, method: options.method, body: options.body ? JSON.parse(options.body) : null }) };
+ return { ok: true, status: 200, text: async () => JSON.stringify({ url, method: options.method, body: options.body ? JSON.parse(options.body) : null }) };
}; process.argv = ['node', ${JSON.stringify(cli)}, ...${JSON.stringify(args)}]; require(${JSON.stringify(cli)});`
return spawnSync(process.execPath, ['-e', source], { encoding: 'utf8', timeout: 5000, env: {
...process.env, REWARDFUL_API_KEY: 'fake-key', RESEND_API_KEY: 'fake-key', POSTMARK_API_KEY: 'fake-key', BEEHIIV_API_KEY: 'fake-key', KLAVIYO_API_KEY: 'fake-key',
diff --git a/tests/clis/http-rejection-status.test.cjs b/tests/clis/http-rejection-status.test.cjs
new file mode 100644
index 0000000..751a04c
--- /dev/null
+++ b/tests/clis/http-rejection-status.test.cjs
@@ -0,0 +1,112 @@
+const { test } = require('node:test')
+const assert = require('node:assert/strict')
+const { spawnSync } = require('node:child_process')
+const path = require('node:path')
+
+// Each subprocess runs the unchanged CLI entry point with a real Response object.
+// Fake credentials and a controlled fetch keep rejected mutations/reports offline.
+const commands = [
+ {"name": "activecampaign", "args": ["contacts", "list"]},
+ {"name": "adobe-analytics", "args": ["reportsuites", "list"]},
+ {"name": "calendly", "args": ["users", "me"]},
+ {"name": "clay", "args": ["tables", "list"]},
+ {"name": "clearbit", "args": ["company", "find", "--domain", "example.com"]},
+ {"name": "coupler", "args": ["importers", "list"]},
+ {"name": "exa", "args": ["search", "--query", "fixture"]},
+ {"name": "firecrawl", "args": ["scrape", "--url", "https://example.com"]},
+ {"name": "g2", "args": ["products", "list"]},
+ {"name": "google-ads", "args": ["campaigns", "list"]},
+ {"name": "mention-me", "args": ["referrals", "get", "--id", "fixture-referral"]},
+ {"name": "meta-ads", "args": ["accounts", "list"]},
+ {"name": "outreach", "args": ["prospects", "list"]},
+ {"name": "paddle", "args": ["products", "list"]},
+ {"name": "partnerstack", "args": ["partnerships", "list"]},
+ {"name": "plausible", "args": ["sites", "list"]},
+ {"name": "postmark", "args": ["server", "get"]},
+ {"name": "rankparse", "args": ["credits"]},
+ {"name": "segment", "args": ["track", "event", "--user-id", "fixture", "--event", "Example"]},
+ {"name": "typeform", "args": ["forms", "list"]},
+ {"name": "segment", "label": "segment profiles", "args": ["profiles", "traits", "--space-id", "fixture", "--user-id", "fixture"]},
+]
+const fixtureEnv = {
+ "ACTIVECAMPAIGN_API_KEY": "fixture-secret",
+ "ACTIVECAMPAIGN_API_URL": "https://fixture.api-us1.com",
+ "ADOBE_ACCESS_TOKEN": "fixture-secret",
+ "ADOBE_CLIENT_ID": "fixture-secret",
+ "ADOBE_COMPANY_ID": "fixture-secret",
+ "CALENDLY_API_KEY": "fixture-secret",
+ "CLAY_API_KEY": "fixture-secret",
+ "CLEARBIT_API_KEY": "fixture-secret",
+ "COUPLER_API_KEY": "fixture-secret",
+ "EXA_API_KEY": "fixture-secret",
+ "FIRECRAWL_API_KEY": "fixture-secret",
+ "G2_API_TOKEN": "fixture-secret",
+ "GOOGLE_ADS_TOKEN": "fixture-secret",
+ "GOOGLE_ADS_DEVELOPER_TOKEN": "fixture-secret",
+ "GOOGLE_ADS_CUSTOMER_ID": "5556667777",
+ "GOOGLE_ADS_LOGIN_CUSTOMER_ID": "1234567890",
+ "MENTIONME_API_KEY": "fixture-secret",
+ "META_ACCESS_TOKEN": "fixture-secret",
+ "META_AD_ACCOUNT_ID": "fixture-secret",
+ "OUTREACH_ACCESS_TOKEN": "fixture-secret",
+ "PADDLE_API_KEY": "fixture-secret",
+ "PADDLE_SANDBOX": "false",
+ "PARTNERSTACK_PUBLIC_KEY": "fixture-secret",
+ "PARTNERSTACK_SECRET_KEY": "fixture-secret",
+ "PLAUSIBLE_API_KEY": "fixture-secret",
+ "PLAUSIBLE_BASE_URL": "https://plausible.io",
+ "POSTMARK_API_KEY": "fixture-secret",
+ "RANKPARSE_API_KEY": "fixture-secret",
+ "SEGMENT_WRITE_KEY": "fixture-secret",
+ "SEGMENT_ACCESS_TOKEN": "fixture-secret",
+ "TYPEFORM_API_KEY": "fixture-secret",
+}
+function run(command, status, body, preview = false) {
+ const cli = path.resolve(__dirname, `../../tools/clis/${command.name}.js`)
+ const args = preview ? [...command.args, '--dry-run'] : command.args
+ const setup = preview
+ ? "global.fetch = async () => { throw new Error('dry-run reached fetch') }"
+ : `global.fetch = async () => new Response(${JSON.stringify(body)}, { status: ${status} })`
+ const code = `${setup}; process.argv = ['node', ${JSON.stringify(cli)}, ...${JSON.stringify(args)}]; require(${JSON.stringify(cli)});`
+ return spawnSync(process.execPath, ['-e', code], { encoding: 'utf8', timeout: 10000, env: { ...process.env, ...fixtureEnv } })
+}
+function output(result) {
+ assert.equal(result.signal, null, result.stderr)
+ assert.equal(result.stderr, '')
+ return JSON.parse(result.stdout)
+}
+for (const command of commands) {
+ const label = command.label || command.name
+ test(`${label}: HTTP403 sets failure status and preserves JSON diagnostics`, () => {
+ const payload = { error: { message: 'permission denied', code: 'fixture_denied' }, request_id: 'fixture-request' }
+ const result = run(command, 403, JSON.stringify(payload))
+ assert.deepEqual(output(result), payload)
+ assert.equal(result.status, 1)
+ })
+ test(`${label}: HTTP429 sets failure status and preserves non-JSON diagnostics`, () => {
+ const result = run(command, 429, 'retry later')
+ assert.deepEqual(output(result), { status: 429, body: 'retry later' })
+ assert.equal(result.status, 1)
+ })
+ test(`${label}: empty HTTP503 is a failure without losing its status`, () => {
+ const result = run(command, 503, '')
+ assert.deepEqual(output(result), { status: 503, body: '' })
+ assert.equal(result.status, 1)
+ })
+ test(`${label}: successful HTTP200 output and exit status are unchanged`, () => {
+ const payload = { data: [{ id: 'fixture-id' }], request_id: 'fixture-ok' }
+ const result = run(command, 200, JSON.stringify(payload))
+ assert.deepEqual(output(result), payload)
+ assert.equal(result.status, 0)
+ })
+ test(`${label}: empty HTTP204 remains successful`, () => {
+ const result = run(command, 204, null)
+ assert.deepEqual(output(result), { status: 204, body: '' })
+ assert.equal(result.status, 0)
+ })
+ test(`${label}: dry-run stays offline and successful`, () => {
+ const result = run(command, undefined, undefined, true)
+ assert.equal(output(result)._dry_run, true)
+ assert.equal(result.status, 0)
+ })
+}
diff --git a/tests/clis/paddle-discount-limit.test.cjs b/tests/clis/paddle-discount-limit.test.cjs
index 8f39d3c..4547c71 100644
--- a/tests/clis/paddle-discount-limit.test.cjs
+++ b/tests/clis/paddle-discount-limit.test.cjs
@@ -6,7 +6,7 @@ const cli = path.resolve(__dirname, '../../tools/clis/paddle.js')
function run(args, fetch = false) {
const source = `global.fetch = async (url, options) => {
if (!${fetch}) throw new Error('Unexpected network request');
- return { status: 200, text: async () => JSON.stringify({ url, method: options.method, body: options.body ? JSON.parse(options.body) : null }) };
+ return { ok: true, status: 200, text: async () => JSON.stringify({ url, method: options.method, body: options.body ? JSON.parse(options.body) : null }) };
}; process.argv = ['node', ${JSON.stringify(cli)}, ...${JSON.stringify(args)}]; require(${JSON.stringify(cli)});`
const result = spawnSync(process.execPath, ['-e', source], { encoding: 'utf8', env: { ...process.env, PADDLE_API_KEY: 'fake-key' }, timeout: 5000 })
assert.equal(result.status, 0, result.stderr)
diff --git a/tests/clis/postmark-layout-template.test.cjs b/tests/clis/postmark-layout-template.test.cjs
index 9586f12..aed93dd 100644
--- a/tests/clis/postmark-layout-template.test.cjs
+++ b/tests/clis/postmark-layout-template.test.cjs
@@ -6,7 +6,7 @@ function run(vendor, args, fetch = true) {
const cli = path.resolve(__dirname, `../../tools/clis/${vendor}.js`)
const source = `global.fetch = async (url, options) => {
if (!${fetch}) throw new Error('Unexpected network request');
- return { status: 200, text: async () => JSON.stringify({ url, method: options.method, body: options.body ? JSON.parse(options.body) : null }) };
+ return { ok: true, status: 200, text: async () => JSON.stringify({ url, method: options.method, body: options.body ? JSON.parse(options.body) : null }) };
}; process.argv = ['node', ${JSON.stringify(cli)}, ...${JSON.stringify(args)}]; require(${JSON.stringify(cli)});`
return spawnSync(process.execPath, ['-e', source], { encoding: 'utf8', timeout: 5000, env: {
...process.env, REWARDFUL_API_KEY: 'fake-key', RESEND_API_KEY: 'fake-key', POSTMARK_API_KEY: 'fake-key', BEEHIIV_API_KEY: 'fake-key', KLAVIYO_API_KEY: 'fake-key',
diff --git a/tests/clis/typeform-update.test.cjs b/tests/clis/typeform-update.test.cjs
index 50c4d63..3cc4733 100644
--- a/tests/clis/typeform-update.test.cjs
+++ b/tests/clis/typeform-update.test.cjs
@@ -18,7 +18,7 @@ test('renaming uses a title-only JSON patch, preserving fields and responses', (
if (JSON.stringify(patches) !== JSON.stringify([{op: 'replace', path: '/title', value: '新しい title'}])) throw new Error('Invalid patch');
form.title = patches[0].value;
} else throw new Error('Unexpected method');
- return { status: 204, text: async () => JSON.stringify(form) };
+ return { ok: true, status: 204, text: async () => JSON.stringify(form) };
`)
assert.equal(result.status, 0, result.stderr)
const form = JSON.parse(result.stdout)
diff --git a/tools/clis/activecampaign.js b/tools/clis/activecampaign.js
index 98c77b3..805e2c6 100755
--- a/tools/clis/activecampaign.js
+++ b/tools/clis/activecampaign.js
@@ -29,6 +29,7 @@ async function api(method, path, body) {
},
body: body ? JSON.stringify(body) : undefined,
})
+ if (!res.ok) process.exitCode = 1
const text = await res.text()
try {
return JSON.parse(text)
diff --git a/tools/clis/adobe-analytics.js b/tools/clis/adobe-analytics.js
index 1edd71c..2134ebd 100755
--- a/tools/clis/adobe-analytics.js
+++ b/tools/clis/adobe-analytics.js
@@ -26,6 +26,7 @@ async function api(method, path, body) {
},
body: body ? JSON.stringify(body) : undefined,
})
+ if (!res.ok) process.exitCode = 1
const text = await res.text()
try {
return JSON.parse(text)
diff --git a/tools/clis/calendly.js b/tools/clis/calendly.js
index 0c1a5eb..4224716 100755
--- a/tools/clis/calendly.js
+++ b/tools/clis/calendly.js
@@ -22,6 +22,7 @@ async function api(method, path, body) {
},
body: body ? JSON.stringify(body) : undefined,
})
+ if (!res.ok) process.exitCode = 1
const text = await res.text()
try {
return JSON.parse(text)
diff --git a/tools/clis/clay.js b/tools/clis/clay.js
index 0946267..b27adc5 100755
--- a/tools/clis/clay.js
+++ b/tools/clis/clay.js
@@ -22,6 +22,7 @@ async function api(method, path, body) {
}
if (body) options.body = JSON.stringify(body)
const res = await fetch(`${BASE_URL}${path}`, options)
+ if (!res.ok) process.exitCode = 1
const text = await res.text()
try {
return JSON.parse(text)
diff --git a/tools/clis/clearbit.js b/tools/clis/clearbit.js
index fa3b835..94f200a 100755
--- a/tools/clis/clearbit.js
+++ b/tools/clis/clearbit.js
@@ -22,6 +22,7 @@ async function api(method, baseUrl, path, body) {
},
body: body ? JSON.stringify(body) : undefined,
})
+ if (!res.ok) process.exitCode = 1
const text = await res.text()
try {
return JSON.parse(text)
diff --git a/tools/clis/coupler.js b/tools/clis/coupler.js
index 4b943f2..c73ff98 100755
--- a/tools/clis/coupler.js
+++ b/tools/clis/coupler.js
@@ -21,6 +21,7 @@ async function api(method, path, body) {
},
body: body ? JSON.stringify(body) : undefined,
})
+ if (!res.ok) process.exitCode = 1
const text = await res.text()
try {
return JSON.parse(text)
diff --git a/tools/clis/exa.js b/tools/clis/exa.js
index 87b7629..d6cc170 100755
--- a/tools/clis/exa.js
+++ b/tools/clis/exa.js
@@ -22,6 +22,7 @@ async function api(method, path, body) {
},
body: body ? JSON.stringify(body) : undefined,
})
+ if (!res.ok) process.exitCode = 1
const text = await res.text()
try {
return JSON.parse(text)
diff --git a/tools/clis/firecrawl.js b/tools/clis/firecrawl.js
index e152047..24987f2 100755
--- a/tools/clis/firecrawl.js
+++ b/tools/clis/firecrawl.js
@@ -21,6 +21,7 @@ async function api(method, path, body) {
},
body: body ? JSON.stringify(body) : undefined,
})
+ if (!res.ok) process.exitCode = 1
const text = await res.text()
try {
return JSON.parse(text)
diff --git a/tools/clis/g2.js b/tools/clis/g2.js
index a3d2c29..bd527fa 100755
--- a/tools/clis/g2.js
+++ b/tools/clis/g2.js
@@ -22,6 +22,7 @@ async function api(method, path, body) {
},
body: body ? JSON.stringify(body) : undefined,
})
+ if (!res.ok) process.exitCode = 1
const text = await res.text()
try {
return JSON.parse(text)
diff --git a/tools/clis/google-ads.js b/tools/clis/google-ads.js
index 2dd87b8..38d00f1 100755
--- a/tools/clis/google-ads.js
+++ b/tools/clis/google-ads.js
@@ -27,6 +27,7 @@ async function api(method, path, body) {
headers,
body: body ? JSON.stringify(body) : undefined,
})
+ if (!res.ok) process.exitCode = 1
const text = await res.text()
try {
return JSON.parse(text)
diff --git a/tools/clis/mention-me.js b/tools/clis/mention-me.js
index 6f26d6f..93353b1 100755
--- a/tools/clis/mention-me.js
+++ b/tools/clis/mention-me.js
@@ -22,6 +22,7 @@ async function api(method, path, body) {
headers,
body: body ? JSON.stringify(body) : undefined,
})
+ if (!res.ok) process.exitCode = 1
const text = await res.text()
try {
return JSON.parse(text)
diff --git a/tools/clis/meta-ads.js b/tools/clis/meta-ads.js
index 6c08ea3..089cba5 100755
--- a/tools/clis/meta-ads.js
+++ b/tools/clis/meta-ads.js
@@ -24,6 +24,7 @@ async function api(method, path, body) {
return { _dry_run: true, method, url, headers: { ...opts.headers, Authorization: '***' }, body: body || undefined }
}
const res = await fetch(url, opts)
+ if (!res.ok) process.exitCode = 1
const text = await res.text()
try {
return JSON.parse(text)
diff --git a/tools/clis/outreach.js b/tools/clis/outreach.js
index 63266bc..05c8b45 100755
--- a/tools/clis/outreach.js
+++ b/tools/clis/outreach.js
@@ -23,6 +23,7 @@ async function api(method, path, body) {
},
body: body ? JSON.stringify(body) : undefined,
})
+ if (!res.ok) process.exitCode = 1
const text = await res.text()
try {
return JSON.parse(text)
diff --git a/tools/clis/paddle.js b/tools/clis/paddle.js
index d414a2a..d226fb3 100755
--- a/tools/clis/paddle.js
+++ b/tools/clis/paddle.js
@@ -24,6 +24,7 @@ async function api(method, path, body) {
},
body: body ? JSON.stringify(body) : undefined,
})
+ if (!res.ok) process.exitCode = 1
const text = await res.text()
try {
return JSON.parse(text)
diff --git a/tools/clis/partnerstack.js b/tools/clis/partnerstack.js
index de60ac7..a3bc01d 100755
--- a/tools/clis/partnerstack.js
+++ b/tools/clis/partnerstack.js
@@ -25,6 +25,7 @@ async function api(method, path, body) {
},
body: body ? JSON.stringify(body) : undefined,
})
+ if (!res.ok) process.exitCode = 1
const text = await res.text()
try {
return JSON.parse(text)
diff --git a/tools/clis/plausible.js b/tools/clis/plausible.js
index 3d2037c..d7da2d7 100755
--- a/tools/clis/plausible.js
+++ b/tools/clis/plausible.js
@@ -22,6 +22,7 @@ async function api(method, path, body) {
},
body: body ? JSON.stringify(body) : undefined,
})
+ if (!res.ok) process.exitCode = 1
const text = await res.text()
try {
return JSON.parse(text)
diff --git a/tools/clis/postmark.js b/tools/clis/postmark.js
index 5fee027..db82bec 100755
--- a/tools/clis/postmark.js
+++ b/tools/clis/postmark.js
@@ -33,6 +33,7 @@ async function api(method, path, body, useAccountToken) {
headers,
body: body ? JSON.stringify(body) : undefined,
})
+ if (!res.ok) process.exitCode = 1
const text = await res.text()
try {
return JSON.parse(text)
diff --git a/tools/clis/rankparse.js b/tools/clis/rankparse.js
index 35e435f..b09f033 100755
--- a/tools/clis/rankparse.js
+++ b/tools/clis/rankparse.js
@@ -22,6 +22,7 @@ async function api(method, path, body) {
}
if (body) init.body = JSON.stringify(body)
const res = await fetch(`${BASE_URL}${path}`, init)
+ if (!res.ok) process.exitCode = 1
const text = await res.text()
try {
return JSON.parse(text)
diff --git a/tools/clis/segment.js b/tools/clis/segment.js
index ea2efed..bbb13ce 100755
--- a/tools/clis/segment.js
+++ b/tools/clis/segment.js
@@ -27,6 +27,7 @@ async function trackApi(method, path, body) {
},
body: body ? JSON.stringify(body) : undefined,
})
+ if (!res.ok) process.exitCode = 1
const text = await res.text()
try {
return JSON.parse(text)
@@ -50,6 +51,7 @@ async function profileApi(method, path) {
'Content-Type': 'application/json',
},
})
+ if (!res.ok) process.exitCode = 1
const text = await res.text()
try {
return JSON.parse(text)
diff --git a/tools/clis/typeform.js b/tools/clis/typeform.js
index 893e4a8..74382b6 100755
--- a/tools/clis/typeform.js
+++ b/tools/clis/typeform.js
@@ -22,6 +22,7 @@ async function api(method, path, body) {
},
body: body ? JSON.stringify(body) : undefined,
})
+ if (!res.ok) process.exitCode = 1
const text = await res.text()
try {
return JSON.parse(text)