Skip to content

armgit / VERT

Public Imported from GitHub
0

The next-generation file converter. Open source, fully local* and free forever.

chore: improve logging

JovannMC8e3e8242b7e3
diff --git a/src/lib/store/index.svelte.ts b/src/lib/store/index.svelte.ts
index 3011321..9cddb26 100644
--- a/src/lib/store/index.svelte.ts
+++ b/src/lib/store/index.svelte.ts
@@ -173,7 +173,7 @@ class Files {
const file = files.files[i];
const result = file.result;
if (!result) {
- console.error("No result found");
+ error(["files"], "No result found");
continue;
}
dlFiles.push({
diff --git a/src/lib/types/file.svelte.ts b/src/lib/types/file.svelte.ts
index 28e1068..6e2d03c 100644
--- a/src/lib/types/file.svelte.ts
+++ b/src/lib/types/file.svelte.ts
@@ -1,4 +1,5 @@
import type { Converter } from "$lib/converters/converter.svelte";
+import { error } from "$lib/logger";
import { addToast } from "$lib/store/ToastProvider";
export class VertFile {
@@ -46,7 +47,7 @@ export class VertFile {
res = await this.converter.convert(this, this.to);
this.result = res;
} catch (err) {
- console.error(err);
+ error(["files"], err);
addToast("error", `Error converting file: ${this.file.name}`);
this.result = null;
}
diff --git a/src/service-worker.js b/src/service-worker.js
index c731472..0cd3a5e 100644
--- a/src/service-worker.js
+++ b/src/service-worker.js
@@ -1,84 +1,95 @@
/// <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 ASSETS = [
- ...build, // the app itself
- ...files, // everything in `static`
+ ...build, // the app itself
+ ...files, // everything in `static`
];
-self.addEventListener('install', (event) => {
- // Create a new cache and add all files to it
- async function addFilesToCache() {
- const cache = await caches.open(CACHE);
- await cache.addAll(ASSETS);
- }
-
- console.log('installing service worker for version', version);
- console.log('caching assets', ASSETS);
-
- event.waitUntil(addFilesToCache());
+self.addEventListener("install", (event) => {
+ // create a new cache and add all files to it
+ async function addFilesToCache() {
+ try {
+ 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}`);
+ event.waitUntil(addFilesToCache());
});
-self.addEventListener('activate', (event) => {
- // Remove previous cached data from disk
- async function deleteOldCaches() {
- for (const key of await caches.keys()) {
- if (key !== CACHE) await caches.delete(key);
- }
- }
-
- event.waitUntil(deleteOldCaches());
+self.addEventListener("activate", (event) => {
+ // remove previous cached data from disk
+ async function deleteOldCaches() {
+ try {
+ 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());
});
-self.addEventListener('fetch', (event) => {
- // ignore POST requests etc
- if (event.request.method !== 'GET') return;
-
- async function respond() {
- const url = new URL(event.request.url);
- const cache = await caches.open(CACHE);
-
- // `build`/`files` can always be served from the cache
- if (ASSETS.includes(url.pathname)) {
- const response = await cache.match(url.pathname);
-
- if (response) {
- return response;
- }
- }
-
- // for everything else, try the network first, but
- // fall back to the cache if we're offline
- try {
- const response = await fetch(event.request);
-
- // if we're offline, fetch can return a value that is not a Response
- // instead of throwing - and we can't pass this non-Response to respondWith
- if (!(response instanceof Response)) {
- throw new Error('invalid response from fetch');
- }
-
- if (response.status === 200) {
- cache.put(event.request, response.clone());
- }
-
- return response;
- } catch (err) {
- const response = await cache.match(event.request);
-
- if (response) {
- console.log(`Returning from Cache`, event.request.url);
- return response;
- }
-
- // if there's no cache, then just error out
- // as there is nothing we can do to respond to this request
- throw err;
- }
- }
-
- event.respondWith(respond());
+self.addEventListener("fetch", (event) => {
+ // ignore requests other than GET
+ if (event.request.method !== "GET") return;
+
+ async function respond() {
+ const url = new URL(event.request.url);
+ const cache = await caches.open(CACHE);
+
+ // assets can always be served from the cache
+ if (ASSETS.includes(url.pathname)) {
+ const response = await cache.match(url.pathname);
+ if (response) {
+ console.log(`serving ${url.pathname} from cache`);
+ return response;
+ }
+ }
+
+ // for everything else, try the network first, but
+ // fall back to the cache if we're offline
+ try {
+ const response = await fetch(event.request);
+
+ // if we're offline, fetch can return a value that is not a Response instead
+ // of throwing, and we can't pass this non-Response to respondWith
+ if (!(response instanceof Response)) {
+ throw new Error("invalid response from fetch");
+ }
+
+ if (response.status === 200)
+ cache.put(event.request, response.clone());
+
+ return response;
+ } catch (err) {
+ const response = await cache.match(event.request);
+
+ if (response) {
+ console.log(`Returning ${event.request.url} from cache`);
+ return response;
+ }
+
+ // if there's no cache, then just error out
+ // as there is nothing we can do to respond to this request
+ throw err;
+ }
+ }
+
+ event.respondWith(respond());
});