Skip to content

Commit 7069764

Browse files
IMB11Prospector
andauthored
fix: error handling improvements (#3797)
* fix: error handling improvements * refactor: error info cards * refactor: PyroError -> ModrinthError * fix: lint * fix: idiot --------- Co-authored-by: Prospector <6166773+Prospector@users.noreply.github.com>
1 parent 0a9ffd3 commit 7069764

File tree

7 files changed

+399
-199
lines changed

7 files changed

+399
-199
lines changed

apps/frontend/src/composables/servers/modrinth-servers.ts

Lines changed: 64 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -124,58 +124,63 @@ export class ModrinthServer {
124124
return dataURL;
125125
}
126126
} catch (error) {
127-
if (error instanceof ModrinthServerError && error.statusCode === 404 && iconUrl) {
128-
// Handle external icon processing
129-
try {
130-
const response = await fetch(iconUrl);
131-
if (!response.ok) throw new Error("Failed to fetch icon");
132-
const file = await response.blob();
133-
const originalFile = new File([file], "server-icon-original.png", {
134-
type: "image/png",
135-
});
136-
137-
if (import.meta.client) {
138-
const dataURL = await new Promise<string>((resolve) => {
139-
const canvas = document.createElement("canvas");
140-
const ctx = canvas.getContext("2d");
141-
const img = new Image();
142-
img.onload = () => {
143-
canvas.width = 64;
144-
canvas.height = 64;
145-
ctx?.drawImage(img, 0, 0, 64, 64);
146-
canvas.toBlob(async (blob) => {
147-
if (blob) {
148-
const scaledFile = new File([blob], "server-icon.png", { type: "image/png" });
149-
await useServersFetch(`/create?path=/server-icon.png&type=file`, {
150-
method: "POST",
151-
contentType: "application/octet-stream",
152-
body: scaledFile,
153-
override: auth,
154-
});
155-
await useServersFetch(`/create?path=/server-icon-original.png&type=file`, {
156-
method: "POST",
157-
contentType: "application/octet-stream",
158-
body: originalFile,
159-
override: auth,
160-
});
161-
}
162-
}, "image/png");
163-
const dataURL = canvas.toDataURL("image/png");
164-
sharedImage.value = dataURL;
165-
resolve(dataURL);
166-
URL.revokeObjectURL(img.src);
167-
};
168-
img.src = URL.createObjectURL(file);
127+
if (error instanceof ModrinthServerError && error.statusCode === 404) {
128+
if (iconUrl) {
129+
try {
130+
const response = await fetch(iconUrl);
131+
if (!response.ok) throw new Error("Failed to fetch icon");
132+
const file = await response.blob();
133+
const originalFile = new File([file], "server-icon-original.png", {
134+
type: "image/png",
169135
});
170-
return dataURL;
136+
137+
if (import.meta.client) {
138+
const dataURL = await new Promise<string>((resolve) => {
139+
const canvas = document.createElement("canvas");
140+
const ctx = canvas.getContext("2d");
141+
const img = new Image();
142+
img.onload = () => {
143+
canvas.width = 64;
144+
canvas.height = 64;
145+
ctx?.drawImage(img, 0, 0, 64, 64);
146+
canvas.toBlob(async (blob) => {
147+
if (blob) {
148+
const scaledFile = new File([blob], "server-icon.png", {
149+
type: "image/png",
150+
});
151+
await useServersFetch(`/create?path=/server-icon.png&type=file`, {
152+
method: "POST",
153+
contentType: "application/octet-stream",
154+
body: scaledFile,
155+
override: auth,
156+
});
157+
await useServersFetch(`/create?path=/server-icon-original.png&type=file`, {
158+
method: "POST",
159+
contentType: "application/octet-stream",
160+
body: originalFile,
161+
override: auth,
162+
});
163+
}
164+
}, "image/png");
165+
const dataURL = canvas.toDataURL("image/png");
166+
sharedImage.value = dataURL;
167+
resolve(dataURL);
168+
URL.revokeObjectURL(img.src);
169+
};
170+
img.src = URL.createObjectURL(file);
171+
});
172+
return dataURL;
173+
}
174+
} catch (externalError: any) {
175+
console.debug("Could not process external icon:", externalError.message);
171176
}
172-
} catch (error) {
173-
console.error("Failed to process external icon:", error);
174177
}
178+
} else {
179+
throw error;
175180
}
176181
}
177-
} catch (error) {
178-
console.error("Failed to process server icon:", error);
182+
} catch (error: any) {
183+
console.debug("Icon processing failed:", error.message);
179184
}
180185

181186
sharedImage.value = undefined;
@@ -239,6 +244,18 @@ export class ModrinthServer {
239244
break;
240245
}
241246
} catch (error) {
247+
if (error instanceof ModrinthServerError) {
248+
if (error.statusCode === 404 && ["fs", "content"].includes(module)) {
249+
console.debug(`Optional ${module} resource not found:`, error.message);
250+
continue;
251+
}
252+
253+
if (error.statusCode === 503) {
254+
console.debug(`Temporary ${module} unavailable:`, error.message);
255+
continue;
256+
}
257+
}
258+
242259
this.errors[module] = {
243260
error:
244261
error instanceof ModrinthServerError

apps/frontend/src/composables/servers/modules/general.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -155,19 +155,25 @@ export class GeneralModule extends ServerModule implements ServerGeneral {
155155
}
156156

157157
async setMotd(motd: string): Promise<void> {
158-
const props = (await this.server.fetchConfigFile("ServerProperties")) as any;
159-
if (props) {
160-
props.motd = motd;
161-
const newProps = this.server.constructServerProperties(props);
162-
const octetStream = new Blob([newProps], { type: "application/octet-stream" });
163-
const auth = await useServersFetch<JWTAuth>(`servers/${this.serverId}/fs`);
164-
165-
await useServersFetch(`/update?path=/server.properties`, {
166-
method: "PUT",
167-
contentType: "application/octet-stream",
168-
body: octetStream,
169-
override: auth,
170-
});
158+
try {
159+
const props = (await this.server.fetchConfigFile("ServerProperties")) as any;
160+
if (props) {
161+
props.motd = motd;
162+
const newProps = this.server.constructServerProperties(props);
163+
const octetStream = new Blob([newProps], { type: "application/octet-stream" });
164+
const auth = await useServersFetch<JWTAuth>(`servers/${this.serverId}/fs`);
165+
166+
await useServersFetch(`/update?path=/server.properties`, {
167+
method: "PUT",
168+
contentType: "application/octet-stream",
169+
body: octetStream,
170+
override: auth,
171+
});
172+
}
173+
} catch {
174+
console.error(
175+
"[Modrinth Servers] [General] Failed to set MOTD due to lack of server properties file.",
176+
);
171177
}
172178
}
173179
}

0 commit comments

Comments
 (0)