Skip to content

Commit 6f9abd9

Browse files
authored
refactor: add error handling (#427)
1 parent 75d27f1 commit 6f9abd9

File tree

1 file changed

+52
-20
lines changed

1 file changed

+52
-20
lines changed

packages/cli/index.ts

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -191,17 +191,26 @@ const main = defineCommand({
191191

192192
const key = hash(metadata);
193193

194-
const checkResponse = await fetch(new URL("/check", apiUrl), {
195-
method: "POST",
196-
body: JSON.stringify({
197-
owner,
198-
repo,
199-
key,
200-
}),
201-
});
194+
let checkResponse;
195+
try {
196+
checkResponse = await fetch(new URL("/check", apiUrl), {
197+
method: "POST",
198+
body: JSON.stringify({
199+
owner,
200+
repo,
201+
key,
202+
}),
203+
});
204+
} catch (error) {
205+
console.error(`Failed to connect to server: ${error}`);
206+
process.exit(1);
207+
}
202208

203209
if (!checkResponse.ok) {
204-
console.error(await checkResponse.text());
210+
const errorText = await checkResponse.text();
211+
console.error(
212+
`Check failed (${checkResponse.status}): ${errorText}`,
213+
);
205214
process.exit(1);
206215
}
207216

@@ -247,12 +256,26 @@ const main = defineCommand({
247256
realDeps?.set(pJson.name, pJson.version ?? longDepUrl);
248257

249258
const controller = new AbortController();
250-
const resource = await fetch(longDepUrl, {
251-
signal: controller.signal,
252-
});
253-
if (resource.ok) {
259+
try {
260+
const resource = await fetch(longDepUrl, {
261+
signal: controller.signal,
262+
});
263+
if (resource.ok) {
264+
console.warn(
265+
`${pJson.name}@${formattedSha} was already published on ${longDepUrl}`,
266+
);
267+
} else if (resource.status >= 500) {
268+
console.warn(
269+
`Server error checking ${longDepUrl} (${resource.status}), proceeding with publish`,
270+
);
271+
} else {
272+
console.warn(
273+
`Unexpected response checking ${longDepUrl} (${resource.status})`,
274+
);
275+
}
276+
} catch (error) {
254277
console.warn(
255-
`${pJson.name}@${formattedSha} was already published on ${longDepUrl}`,
278+
`Failed to check if package exists at ${longDepUrl}: ${error}`,
256279
);
257280
}
258281
controller.abort();
@@ -522,12 +545,21 @@ const main = defineCommand({
522545
},
523546
body: formData,
524547
});
525-
const laterRes = await res.clone().json();
526-
assert.equal(
527-
res.status,
528-
200,
529-
`publishing failed: ${await res.text()}`,
530-
);
548+
549+
if (!res.ok) {
550+
const errorText = await res.text();
551+
console.error(`Publishing failed (${res.status}): ${errorText}`);
552+
process.exit(1);
553+
}
554+
555+
let laterRes;
556+
try {
557+
laterRes = await res.json();
558+
} catch (error) {
559+
console.error(`Failed to parse server response as JSON: ${error}`);
560+
console.error(`Raw response: ${await res.text()}`);
561+
process.exit(1);
562+
}
531563

532564
const debug = laterRes.debug;
533565

0 commit comments

Comments
 (0)