« get me outta code hell

data, test: wrap property value errors with proper class & cause - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/things/cacheable-object.js
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-10-18 14:26:49 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-10-18 14:26:49 -0300
commit645a127bef38c3a7a2ef1b94d23b25fb7bdc4191 (patch)
tree6f9cce852c51dc5f74482b0fba89cfad877437f8 /src/data/things/cacheable-object.js
parent4e2dae523e7bf8b49272bd6afcba86a8157af4a1 (diff)
data, test: wrap property value errors with proper class & cause
Diffstat (limited to 'src/data/things/cacheable-object.js')
-rw-r--r--src/data/things/cacheable-object.js19
1 files changed, 12 insertions, 7 deletions
diff --git a/src/data/things/cacheable-object.js b/src/data/things/cacheable-object.js
index 4bc3668..9fda865 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;
+  }
+}