Skip to content

Commit ac92fa2

Browse files
juliusmarmingecodex
andcommitted
refactor(release): structure update manifest failures
Co-authored-by: codex <[email protected]>
1 parent 97e5cd3 commit ac92fa2

3 files changed

Lines changed: 264 additions & 49 deletions

File tree

‎scripts/lib/update-manifest.ts‎

Lines changed: 186 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,120 @@
1+
import * as Effect from "effect/Effect";
2+
import * as Schema from "effect/Schema";
3+
14
export interface UpdateManifestFile {
25
readonly url: string;
36
readonly sha512: string;
47
readonly size: number;
58
}
69

7-
export type UpdateManifestScalar = string | number | boolean;
10+
export const UpdateManifestScalar = Schema.Union([Schema.String, Schema.Number, Schema.Boolean]);
11+
export type UpdateManifestScalar = typeof UpdateManifestScalar.Type;
12+
13+
export const UpdateManifestParseReason = Schema.Literals([
14+
"incomplete file entry",
15+
"sha512 without a file entry",
16+
"size without a file entry",
17+
"unsupported line",
18+
"version must be a string",
19+
"releaseDate must be a string",
20+
"missing version",
21+
"missing releaseDate",
22+
"missing files",
23+
]);
24+
export type UpdateManifestParseReason = typeof UpdateManifestParseReason.Type;
25+
26+
export class UpdateManifestParseError extends Schema.TaggedErrorClass<UpdateManifestParseError>()(
27+
"UpdateManifestParseError",
28+
{
29+
platformLabel: Schema.String,
30+
sourcePath: Schema.String,
31+
lineNumber: Schema.optionalKey(Schema.Number),
32+
reason: UpdateManifestParseReason,
33+
offendingLine: Schema.optionalKey(Schema.String),
34+
},
35+
) {
36+
override get message(): string {
37+
const location =
38+
this.lineNumber === undefined ? this.sourcePath : `${this.sourcePath}:${this.lineNumber}`;
39+
return `Invalid ${this.platformLabel} update manifest at ${location}: ${this.reason}.`;
40+
}
41+
}
42+
43+
export class UpdateManifestVersionConflictError extends Schema.TaggedErrorClass<UpdateManifestVersionConflictError>()(
44+
"UpdateManifestVersionConflictError",
45+
{
46+
platformLabel: Schema.String,
47+
primaryVersion: Schema.String,
48+
secondaryVersion: Schema.String,
49+
},
50+
) {
51+
override get message(): string {
52+
return `Cannot merge ${this.platformLabel} update manifests with different versions (${this.primaryVersion} vs ${this.secondaryVersion}).`;
53+
}
54+
}
55+
56+
export class UpdateManifestExtraConflictError extends Schema.TaggedErrorClass<UpdateManifestExtraConflictError>()(
57+
"UpdateManifestExtraConflictError",
58+
{
59+
platformLabel: Schema.String,
60+
key: Schema.String,
61+
primaryValue: UpdateManifestScalar,
62+
secondaryValue: UpdateManifestScalar,
63+
},
64+
) {
65+
override get message(): string {
66+
return `Cannot merge ${this.platformLabel} update manifests: conflicting '${this.key}' values ('${this.primaryValue}' vs '${this.secondaryValue}').`;
67+
}
68+
}
69+
70+
export class UpdateManifestFileConflictError extends Schema.TaggedErrorClass<UpdateManifestFileConflictError>()(
71+
"UpdateManifestFileConflictError",
72+
{
73+
platformLabel: Schema.String,
74+
url: Schema.String,
75+
primarySha512: Schema.String,
76+
primarySize: Schema.Number,
77+
secondarySha512: Schema.String,
78+
secondarySize: Schema.Number,
79+
},
80+
) {
81+
override get message(): string {
82+
return `Cannot merge ${this.platformLabel} update manifests: conflicting file entry for ${this.url}.`;
83+
}
84+
}
85+
86+
export class UpdateManifestSerializationError extends Schema.TaggedErrorClass<UpdateManifestSerializationError>()(
87+
"UpdateManifestSerializationError",
88+
{
89+
platformLabel: Schema.String,
90+
key: Schema.String,
91+
},
92+
) {
93+
override get message(): string {
94+
return `Cannot serialize ${this.platformLabel} update manifest: missing value for '${this.key}'.`;
95+
}
96+
}
97+
98+
export const UpdateManifestError = Schema.Union([
99+
UpdateManifestParseError,
100+
UpdateManifestVersionConflictError,
101+
UpdateManifestExtraConflictError,
102+
UpdateManifestFileConflictError,
103+
UpdateManifestSerializationError,
104+
]);
105+
export type UpdateManifestError = typeof UpdateManifestError.Type;
106+
export const isUpdateManifestError = Schema.is(UpdateManifestError);
107+
108+
export const attemptUpdateManifest = <A>(
109+
evaluate: () => A,
110+
): Effect.Effect<A, UpdateManifestError> =>
111+
Effect.suspend(() => {
112+
try {
113+
return Effect.succeed(evaluate());
114+
} catch (error) {
115+
return isUpdateManifestError(error) ? Effect.fail(error) : Effect.die(error);
116+
}
117+
});
8118

9119
export interface UpdateManifest {
10120
readonly version: string;
@@ -40,9 +150,12 @@ function parseFileRecord(
40150
typeof currentFile.sha512 !== "string" ||
41151
typeof currentFile.size !== "number"
42152
) {
43-
throw new Error(
44-
`Invalid ${platformLabel} update manifest at ${sourcePath}:${lineNumber}: incomplete file entry.`,
45-
);
153+
throw new UpdateManifestParseError({
154+
platformLabel,
155+
sourcePath,
156+
lineNumber,
157+
reason: "incomplete file entry",
158+
});
46159
}
47160
return {
48161
url: currentFile.url,
@@ -94,9 +207,12 @@ export function parseUpdateManifest(
94207
const fileShaMatch = line.match(/^ sha512:\s*(.+)$/);
95208
if (fileShaMatch?.[1]) {
96209
if (currentFile === null) {
97-
throw new Error(
98-
`Invalid ${platformLabel} update manifest at ${sourcePath}:${lineNumber}: sha512 without a file entry.`,
99-
);
210+
throw new UpdateManifestParseError({
211+
platformLabel,
212+
sourcePath,
213+
lineNumber,
214+
reason: "sha512 without a file entry",
215+
});
100216
}
101217
currentFile.sha512 = stripSingleQuotes(fileShaMatch[1].trim());
102218
continue;
@@ -105,9 +221,12 @@ export function parseUpdateManifest(
105221
const fileSizeMatch = line.match(/^ size:\s*(\d+)$/);
106222
if (fileSizeMatch?.[1]) {
107223
if (currentFile === null) {
108-
throw new Error(
109-
`Invalid ${platformLabel} update manifest at ${sourcePath}:${lineNumber}: size without a file entry.`,
110-
);
224+
throw new UpdateManifestParseError({
225+
platformLabel,
226+
sourcePath,
227+
lineNumber,
228+
reason: "size without a file entry",
229+
});
111230
}
112231
currentFile.size = Number(fileSizeMatch[1]);
113232
continue;
@@ -127,29 +246,39 @@ export function parseUpdateManifest(
127246

128247
const topLevelMatch = line.match(/^([A-Za-z][A-Za-z0-9]*):\s*(.+)$/);
129248
if (!topLevelMatch?.[1] || topLevelMatch[2] === undefined) {
130-
throw new Error(
131-
`Invalid ${platformLabel} update manifest at ${sourcePath}:${lineNumber}: unsupported line '${line}'.`,
132-
);
249+
throw new UpdateManifestParseError({
250+
platformLabel,
251+
sourcePath,
252+
lineNumber,
253+
reason: "unsupported line",
254+
offendingLine: line,
255+
});
133256
}
134257

135258
const [, key, rawValue] = topLevelMatch;
136259
const value = parseScalarValue(rawValue);
137260

138261
if (key === "version") {
139262
if (typeof value !== "string") {
140-
throw new Error(
141-
`Invalid ${platformLabel} update manifest at ${sourcePath}:${lineNumber}: version must be a string.`,
142-
);
263+
throw new UpdateManifestParseError({
264+
platformLabel,
265+
sourcePath,
266+
lineNumber,
267+
reason: "version must be a string",
268+
});
143269
}
144270
version = value;
145271
continue;
146272
}
147273

148274
if (key === "releaseDate") {
149275
if (typeof value !== "string") {
150-
throw new Error(
151-
`Invalid ${platformLabel} update manifest at ${sourcePath}:${lineNumber}: releaseDate must be a string.`,
152-
);
276+
throw new UpdateManifestParseError({
277+
platformLabel,
278+
sourcePath,
279+
lineNumber,
280+
reason: "releaseDate must be a string",
281+
});
153282
}
154283
releaseDate = value;
155284
continue;
@@ -166,15 +295,25 @@ export function parseUpdateManifest(
166295
if (finalized) files.push(finalized);
167296

168297
if (!version) {
169-
throw new Error(`Invalid ${platformLabel} update manifest at ${sourcePath}: missing version.`);
298+
throw new UpdateManifestParseError({
299+
platformLabel,
300+
sourcePath,
301+
reason: "missing version",
302+
});
170303
}
171304
if (!releaseDate) {
172-
throw new Error(
173-
`Invalid ${platformLabel} update manifest at ${sourcePath}: missing releaseDate.`,
174-
);
305+
throw new UpdateManifestParseError({
306+
platformLabel,
307+
sourcePath,
308+
reason: "missing releaseDate",
309+
});
175310
}
176311
if (files.length === 0) {
177-
throw new Error(`Invalid ${platformLabel} update manifest at ${sourcePath}: missing files.`);
312+
throw new UpdateManifestParseError({
313+
platformLabel,
314+
sourcePath,
315+
reason: "missing files",
316+
});
178317
}
179318

180319
return {
@@ -195,9 +334,12 @@ function mergeExtras(
195334
for (const [key, value] of Object.entries(secondary)) {
196335
const existing = merged[key];
197336
if (existing !== undefined && existing !== value) {
198-
throw new Error(
199-
`Cannot merge ${platformLabel} update manifests: conflicting '${key}' values ('${existing}' vs '${value}').`,
200-
);
337+
throw new UpdateManifestExtraConflictError({
338+
platformLabel,
339+
key,
340+
primaryValue: existing,
341+
secondaryValue: value,
342+
});
201343
}
202344
merged[key] = value;
203345
}
@@ -211,18 +353,25 @@ export function mergeUpdateManifests(
211353
platformLabel: string,
212354
): UpdateManifest {
213355
if (primary.version !== secondary.version) {
214-
throw new Error(
215-
`Cannot merge ${platformLabel} update manifests with different versions (${primary.version} vs ${secondary.version}).`,
216-
);
356+
throw new UpdateManifestVersionConflictError({
357+
platformLabel,
358+
primaryVersion: primary.version,
359+
secondaryVersion: secondary.version,
360+
});
217361
}
218362

219363
const filesByUrl = new Map<string, UpdateManifestFile>();
220364
for (const file of [...primary.files, ...secondary.files]) {
221365
const existing = filesByUrl.get(file.url);
222366
if (existing && (existing.sha512 !== file.sha512 || existing.size !== file.size)) {
223-
throw new Error(
224-
`Cannot merge ${platformLabel} update manifests: conflicting file entry for ${file.url}.`,
225-
);
367+
throw new UpdateManifestFileConflictError({
368+
platformLabel,
369+
url: file.url,
370+
primarySha512: existing.sha512,
371+
primarySize: existing.size,
372+
secondarySha512: file.sha512,
373+
secondarySize: file.size,
374+
});
226375
}
227376
filesByUrl.set(file.url, file);
228377
}
@@ -264,9 +413,10 @@ export function serializeUpdateManifest(
264413
for (const key of Object.keys(manifest.extras).toSorted()) {
265414
const value = manifest.extras[key];
266415
if (value === undefined) {
267-
throw new Error(
268-
`Cannot serialize ${options.platformLabel} update manifest: missing value for '${key}'.`,
269-
);
416+
throw new UpdateManifestSerializationError({
417+
platformLabel: options.platformLabel,
418+
key,
419+
});
270420
}
271421
lines.push(`${key}: ${serializeScalarValue(value)}`);
272422
}

0 commit comments

Comments
 (0)