Skip to content

Commit 1a2f468

Browse files
committed
Give better error message when manifest.toml does not exist
1 parent c082aff commit 1a2f468

File tree

2 files changed

+29
-40
lines changed

2 files changed

+29
-40
lines changed

backend/appInfo.test.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ test("url app info with manifest without name", async () => {
181181

182182
expect(appInfo.location).toEqual(location);
183183
expect(appInfo.manifest).toEqual({
184-
name: "No entry in manifest.toml (running from URL)",
184+
name: "Missing name entry in manifest.toml",
185185
sourceCodeUrl: "http://example.com",
186186
manifestFound: true,
187187
});
@@ -205,18 +205,12 @@ test("url app info with broken manifest", async () => {
205205
fetch.isRedirect = () => false;
206206

207207
const location = getLocation("http://localhost:3000") as UrlLocation;
208-
try {
209-
await getAppInfoUrl(location, fetch);
210-
} catch (e) {
211-
if (e instanceof AppInfoError) {
212-
expect(e.message).toEqual(
213-
"Invalid manifest.toml, please check the format",
214-
);
215-
} else {
216-
throw e;
217-
}
218-
}
219-
expect.assertions(1);
208+
const appInfo = await getAppInfoUrl(location, fetch);
209+
expect(appInfo.manifest).toEqual({
210+
name: undefined,
211+
sourceCodeUrl: undefined,
212+
manifestFound: false,
213+
});
220214
});
221215

222216
test("url app info with manifest and source code URL", async () => {

backend/appInfo.ts

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ export type AppInfo = {
3030

3131
export class AppInfoError extends Error {}
3232

33+
const MISSING_MANIFEST = {
34+
name: undefined,
35+
sourceCodeUrl: undefined,
36+
manifestFound: false,
37+
};
38+
3339
export async function getAppInfo(location: Location): Promise<AppInfo> {
3440
if (location.type === "url") {
3541
try {
@@ -72,19 +78,10 @@ async function getManifestInfoFromUrl(
7278
}
7379
const response = await fetch(url + "manifest.toml");
7480
if (!response.ok) {
75-
return {
76-
name: "Unknown (running from URL)",
77-
sourceCodeUrl: undefined,
78-
manifestFound: false,
79-
};
81+
console.error("Missing manifest.toml (from URL)");
82+
return { ...MISSING_MANIFEST, name: "Unknown (running from URL)" };
8083
}
81-
const body = await response.text();
82-
const parsed = tomlParse(body);
83-
return {
84-
name: parsed.name || "No entry in manifest.toml (running from URL)",
85-
sourceCodeUrl: parsed.source_code_url,
86-
manifestFound: true,
87-
};
84+
return tomlParse(await response.text());
8885
}
8986

9087
async function getIconInfoFromUrl(
@@ -117,26 +114,24 @@ function getManifestInfoFromDir(
117114
): ManifestInfo {
118115
const tomlBuffer = readFileBuffer(path.join(dir, "manifest.toml"));
119116
if (tomlBuffer === null) {
120-
return {
121-
name: fallbackName,
122-
sourceCodeUrl: undefined,
123-
manifestFound: false,
124-
};
117+
console.error("Missing manifest.toml (from DIR)");
118+
return { ...MISSING_MANIFEST, name: fallbackName };
125119
}
126-
const parsed = tomlParse(tomlBuffer.toString());
127-
const name = parsed.name || fallbackName;
128-
return {
129-
name,
130-
sourceCodeUrl: parsed.source_code_url,
131-
manifestFound: true,
132-
};
120+
return tomlParse(tomlBuffer.toString(), fallbackName);
133121
}
134122

135-
function tomlParse(s: string): any {
123+
function tomlParse(s: string, fallbackName: string): any {
136124
try {
137-
return toml.parse(s);
125+
const parsed = toml.parse(s);
126+
return {
127+
name:
128+
parsed.name || fallbackName || "Missing name entry in manifest.toml",
129+
sourceCodeUrl: parsed.source_code_url || undefined,
130+
manifestFound: true,
131+
};
138132
} catch (e) {
139-
throw new AppInfoError("Invalid manifest.toml, please check the format");
133+
console.error("Failed to parse manifest.toml, please check the format!");
134+
return { ...MISSING_MANIFEST, name: fallbackName };
140135
}
141136
}
142137

0 commit comments

Comments
 (0)