« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/data/things/cacheable-object.js19
-rw-r--r--src/data/things/index.js6
-rw-r--r--test/unit/data/cacheable-object.js30
-rw-r--r--test/unit/data/things/track.js3
4 files changed, 29 insertions, 29 deletions
diff --git a/src/data/things/cacheable-object.js b/src/data/things/cacheable-object.js
index 4bc3668d..9fda865e 100644
--- a/src/data/things/cacheable-object.js
+++ b/src/data/things/cacheable-object.js
@@ -179,13 +179,8 @@ export default class CacheableObject {
           } else if (result !== true) {
             throw new TypeError(`Validation failed for value ${newValue}`);
           }
-        } catch (error) {
-          error.message = [
-            `Property ${colors.green(property)}`,
-            `(${inspect(this[property])} -> ${inspect(newValue)}):`,
-            error.message
-          ].join(' ');
-          throw error;
+        } catch (caughtError) {
+          throw new CacheableObjectPropertyValueError(property, this[property], newValue, caughtError);
         }
       }
 
@@ -359,3 +354,13 @@ export default class CacheableObject {
     return object.#propertyUpdateValues[key] ?? null;
   }
 }
+
+export class CacheableObjectPropertyValueError extends Error {
+  constructor(property, oldValue, newValue, error) {
+    super(
+      `Error setting ${colors.green(property)} (${inspect(oldValue)} -> ${inspect(newValue)})`,
+      {cause: error});
+
+    this.property = property;
+  }
+}
diff --git a/src/data/things/index.js b/src/data/things/index.js
index 77e5fa76..4ea1f007 100644
--- a/src/data/things/index.js
+++ b/src/data/things/index.js
@@ -21,7 +21,11 @@ import * as trackClasses from './track.js';
 import * as wikiInfoClasses from './wiki-info.js';
 
 export {default as Thing} from './thing.js';
-export {default as CacheableObject} from './cacheable-object.js';
+
+export {
+  default as CacheableObject,
+  CacheableObjectPropertyValueError,
+} from './cacheable-object.js';
 
 const allClassLists = {
   'album.js': albumClasses,
diff --git a/test/unit/data/cacheable-object.js b/test/unit/data/cacheable-object.js
index 2e82af08..57e562d8 100644
--- a/test/unit/data/cacheable-object.js
+++ b/test/unit/data/cacheable-object.js
@@ -195,13 +195,10 @@ t.test(`CacheableObject validate on update`, t => {
   obj.directory = 'megalovania';
   t.equal(obj.directory, 'megalovania');
 
-  try {
-    obj.directory = 25;
-  } catch (err) {
-    thrownError = err;
-  }
+  t.throws(
+    () => { obj.directory = 25; },
+    {cause: mockError});
 
-  t.equal(thrownError, mockError);
   t.equal(obj.directory, 'megalovania');
 
   const date = new Date(`25 December 2009`);
@@ -209,13 +206,10 @@ t.test(`CacheableObject validate on update`, t => {
   obj.date = date;
   t.equal(obj.date, date);
 
-  try {
-    obj.date = `TWELFTH PERIGEE'S EVE`;
-  } catch (err) {
-    thrownError = err;
-  }
+  t.throws(
+    () => { obj.date = `TWELFTH PERIGEE'S EVE`; },
+    {cause: TypeError});
 
-  t.equal(thrownError?.constructor, TypeError);
   t.equal(obj.date, date);
 });
 
@@ -244,8 +238,8 @@ t.test(`CacheableObject default property throws if invalid`, t => {
 
   let thrownError;
 
-  try {
-    newCacheableObject({
+  t.throws(
+    () => newCacheableObject({
       string: {
         flags: {
           update: true
@@ -261,10 +255,6 @@ t.test(`CacheableObject default property throws if invalid`, t => {
           }
         }
       }
-    });
-  } catch (err) {
-    thrownError = err;
-  }
-
-  t.equal(thrownError, mockError);
+    }),
+    {cause: mockError});
 });
diff --git a/test/unit/data/things/track.js b/test/unit/data/things/track.js
index 06a4041d..806efbf1 100644
--- a/test/unit/data/things/track.js
+++ b/test/unit/data/things/track.js
@@ -208,7 +208,8 @@ t.test(`Track.color`, t => {
   t.equal(track.color, '#123456',
     `color #4: is own value`);
 
-  t.throws(() => { track.color = '#aeiouw'; }, TypeError,
+  t.throws(() => { track.color = '#aeiouw'; },
+    {cause: TypeError},
     `color #5: must be set to valid color`);
 });