chore: improve logging

This commit is contained in:
JovannMC 2025-02-12 07:57:48 +03:00
parent ff2e5b1866
commit 8e3e8242b7
No known key found for this signature in database
3 changed files with 75 additions and 63 deletions

View File

@ -173,7 +173,7 @@ class Files {
const file = files.files[i]; const file = files.files[i];
const result = file.result; const result = file.result;
if (!result) { if (!result) {
console.error("No result found"); error(["files"], "No result found");
continue; continue;
} }
dlFiles.push({ dlFiles.push({

View File

@ -1,4 +1,5 @@
import type { Converter } from "$lib/converters/converter.svelte"; import type { Converter } from "$lib/converters/converter.svelte";
import { error } from "$lib/logger";
import { addToast } from "$lib/store/ToastProvider"; import { addToast } from "$lib/store/ToastProvider";
export class VertFile { export class VertFile {
@ -46,7 +47,7 @@ export class VertFile {
res = await this.converter.convert(this, this.to); res = await this.converter.convert(this, this.to);
this.result = res; this.result = res;
} catch (err) { } catch (err) {
console.error(err); error(["files"], err);
addToast("error", `Error converting file: ${this.file.name}`); addToast("error", `Error converting file: ${this.file.name}`);
this.result = null; this.result = null;
} }

View File

@ -1,84 +1,95 @@
/// <reference types="@sveltejs/kit" /> /// <reference types="@sveltejs/kit" />
import { build, files, version } from '$service-worker'; // code modified from https://svelte.dev/docs/kit/service-workers
import { build, files, version } from "$service-worker";
// Create a unique cache name for this deployment // create a unique cache name for this deployment
const CACHE = `cache-${version}`; const CACHE = `cache-${version}`;
const ASSETS = [ const ASSETS = [
...build, // the app itself ...build, // the app itself
...files, // everything in `static` ...files, // everything in `static`
]; ];
self.addEventListener('install', (event) => { self.addEventListener("install", (event) => {
// Create a new cache and add all files to it // create a new cache and add all files to it
async function addFilesToCache() { async function addFilesToCache() {
const cache = await caches.open(CACHE); try {
await cache.addAll(ASSETS); const cache = await caches.open(CACHE);
} await cache.addAll(ASSETS);
console.log(`assets cached successfully: ${ASSETS}`);
} catch (err) {
console.error(`failed to cache assets: ${err}`);
}
}
console.log('installing service worker for version', version); console.log(`installing service worker for version ${version}`);
console.log('caching assets', ASSETS); event.waitUntil(addFilesToCache());
event.waitUntil(addFilesToCache());
}); });
self.addEventListener('activate', (event) => { self.addEventListener("activate", (event) => {
// Remove previous cached data from disk // remove previous cached data from disk
async function deleteOldCaches() { async function deleteOldCaches() {
for (const key of await caches.keys()) { try {
if (key !== CACHE) await caches.delete(key); const keys = await caches.keys();
} for (const key of keys) {
} if (key !== CACHE) {
await caches.delete(key);
console.log(`deleted old cache: ${key}`);
}
}
} catch (error) {
console.error(`failed to delete old caches: ${error}`);
}
}
event.waitUntil(deleteOldCaches()); event.waitUntil(deleteOldCaches());
}); });
self.addEventListener('fetch', (event) => { self.addEventListener("fetch", (event) => {
// ignore POST requests etc // ignore requests other than GET
if (event.request.method !== 'GET') return; if (event.request.method !== "GET") return;
async function respond() { async function respond() {
const url = new URL(event.request.url); const url = new URL(event.request.url);
const cache = await caches.open(CACHE); const cache = await caches.open(CACHE);
// `build`/`files` can always be served from the cache // assets can always be served from the cache
if (ASSETS.includes(url.pathname)) { if (ASSETS.includes(url.pathname)) {
const response = await cache.match(url.pathname); const response = await cache.match(url.pathname);
if (response) {
console.log(`serving ${url.pathname} from cache`);
return response;
}
}
if (response) { // for everything else, try the network first, but
return response; // fall back to the cache if we're offline
} try {
} const response = await fetch(event.request);
// for everything else, try the network first, but // if we're offline, fetch can return a value that is not a Response instead
// fall back to the cache if we're offline // of throwing, and we can't pass this non-Response to respondWith
try { if (!(response instanceof Response)) {
const response = await fetch(event.request); throw new Error("invalid response from fetch");
}
// if we're offline, fetch can return a value that is not a Response if (response.status === 200)
// instead of throwing - and we can't pass this non-Response to respondWith cache.put(event.request, response.clone());
if (!(response instanceof Response)) {
throw new Error('invalid response from fetch');
}
if (response.status === 200) { return response;
cache.put(event.request, response.clone()); } catch (err) {
} const response = await cache.match(event.request);
return response; if (response) {
} catch (err) { console.log(`Returning ${event.request.url} from cache`);
const response = await cache.match(event.request); return response;
}
if (response) { // if there's no cache, then just error out
console.log(`Returning from Cache`, event.request.url); // as there is nothing we can do to respond to this request
return response; throw err;
} }
}
// if there's no cache, then just error out event.respondWith(respond());
// as there is nothing we can do to respond to this request
throw err;
}
}
event.respondWith(respond());
}); });