[gnome-sound-recorder/wip/cdavis/typescript: 2/5] general: Use narrowing for error types




commit 88d141f7f1a7f9d8e665de49d916e94f55f604e9
Author: Christopher Davis <christopherdavis gnome org>
Date:   Tue Aug 9 02:16:56 2022 -0400

    general: Use narrowing for error types
    
    Errors in catch blocks can either be labeled `any` or `unknown`.
    The semantics make a big difference. `any` here means
    that a value can be anything, thus any method call or field
    access is valid - essentially it turns off type checking for
    the value. `unknown` however means that it's *something*,
    but we need to figure out what.
    
    We pair `unknown` with the `instanceof` operator in order
    to narrow down the type to something specific. This allows
    us to preserve TypeScript's type checking for error values
    and their fields and methods.
    
    See
    https://www.typescriptlang.org/docs/handbook/2/narrowing.html#instanceof-narrowing

 src/application.ts   | 10 +++++-----
 src/recordingList.ts | 40 +++++++++++++++++++++-------------------
 2 files changed, 26 insertions(+), 24 deletions(-)
---
diff --git a/src/application.ts b/src/application.ts
index e6b4b27..64acfaf 100644
--- a/src/application.ts
+++ b/src/application.ts
@@ -105,11 +105,11 @@ export const Application = GObject.registerClass(class Application extends Adw.A
         try {
             CacheDir.make_directory_with_parents(null);
             RecordingsDir.make_directory_with_parents(null);
-        // eslint-disable-next-line @typescript-eslint/no-explicit-any
-        } catch (e: any) {
-            if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.EXISTS))
-                console.error(`Failed to create directory ${e}`);
-
+        } catch (e: unknown) {
+            if (e instanceof GLib.Error) {
+                if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.EXISTS))
+                    console.error(`Failed to create directory ${e}`);
+            }
         }
         this._initAppMenu();
     }
diff --git a/src/recordingList.ts b/src/recordingList.ts
index 854cc86..d12edff 100644
--- a/src/recordingList.ts
+++ b/src/recordingList.ts
@@ -78,11 +78,12 @@ export const RecordingList = GObject.registerClass(class RecordingList extends G
                             objCopy.copy_finish(resCopy);
                             objCopy.trash_async(GLib.PRIORITY_LOW, this.cancellable, null);
                             this.dirMonitor.emit_event(dest, src, Gio.FileMonitorEvent.MOVED_IN);
-                        // eslint-disable-next-line @typescript-eslint/no-explicit-any
-                        } catch (e: any) {
-                            if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED)) {
-                                console.error(`Failed to copy recording ${name} to the new location`);
-                                log(e);
+                        } catch (e: unknown) {
+                            if (e instanceof GLib.Error) {
+                                if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED)) {
+                                    console.error(`Failed to copy recording ${name} to the new location`);
+                                    log(e);
+                                }
                             }
                             allCopied = false;
                         }
@@ -98,19 +99,20 @@ export const RecordingList = GObject.registerClass(class RecordingList extends G
                     oldDir?.delete_async(GLib.PRIORITY_LOW, this.cancellable, (objDelete: Gio.File | null, 
resDelete: Gio.AsyncResult) => {
                         try {
                             objDelete?.delete_finish(resDelete);
-                        // eslint-disable-next-line @typescript-eslint/no-explicit-any
-                        } catch (e: any) {
-                            log('Failed to remove the old Recordings directory. Ignore if you\'re using 
flatpak');
-                            log(e);
+                        } catch (e: unknown) {
+                            if (e instanceof GLib.Error) {
+                                log('Failed to remove the old Recordings directory. Ignore if you\'re using 
flatpak');
+                                log(e);
+                            }
                         }
                     });
                 }
             }
-        // eslint-disable-next-line @typescript-eslint/no-explicit-any
-        } catch (e: any) {
-            if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED))
-                console.error(`Failed to copy old  recordings ${e}`);
-
+        } catch (e: unknown) {
+            if (e instanceof GLib.Error) {
+                if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED))
+                    console.error(`Failed to copy old  recordings ${e}`);
+            }
         }
     }
 
@@ -136,11 +138,11 @@ export const RecordingList = GObject.registerClass(class RecordingList extends G
             } else {
                 this._enumerator?.close(this.cancellable);
             }
-        // eslint-disable-next-line @typescript-eslint/no-explicit-any
-        } catch (e: any) {
-            if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED))
-                console.error(`Failed to load recordings ${e}`);
-
+        } catch (e: unknown) {
+            if (e instanceof GLib.Error) {
+                if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED))
+                    console.error(`Failed to load recordings ${e}`);
+            }
         }
     }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]